blob: 718d13b4ce60fe6e4c95a1daf79ac785d2b1e6d3 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js";
2import { ChannelType } from "discord-api-types";
Skyler Grey75ea9172022-08-06 10:22:23 +01003import Discord, {
4 CommandInteraction,
5 MessageActionRow,
6 MessageButton
7} from "discord.js";
pineafan708692b2022-07-24 22:16:22 +01008import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
9import confirmationMessage from "../../../utils/confirmationMessage.js";
10import getEmojiByName from "../../../utils/getEmojiByName.js";
pineafan63fc5e22022-08-04 22:04:10 +010011import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
12// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Skyler Grey75ea9172022-08-06 10:22:23 +010013// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +010014import type { WrappedCheck } from "jshaiku";
pineafan708692b2022-07-24 22:16:22 +010015import client from "../../../utils/client.js";
16
17const command = (builder: SlashCommandSubcommandBuilder) =>
18 builder
pineafan63fc5e22022-08-04 22:04:10 +010019 .setName("staff")
20 .setDescription("Settings for the staff notifications channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010021 .addChannelOption((option) =>
22 option
23 .setName("channel")
24 .setDescription(
25 "The channel to set the staff notifications channel to"
26 )
27 .addChannelTypes([ChannelType.GuildNews, ChannelType.GuildText])
28 .setRequired(false)
29 );
pineafan708692b2022-07-24 22:16:22 +010030
Skyler Grey75ea9172022-08-06 10:22:23 +010031const callback = async (
32 interaction: CommandInteraction
33): Promise<unknown | void> => {
pineafan63fc5e22022-08-04 22:04:10 +010034 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010035 const m = (await interaction.reply({
36 embeds: LoadingEmbed,
37 ephemeral: true,
38 fetchReply: true
39 })) as Discord.Message;
pineafan708692b2022-07-24 22:16:22 +010040 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010041 let channel;
pineafan708692b2022-07-24 22:16:22 +010042 try {
pineafan63fc5e22022-08-04 22:04:10 +010043 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010044 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010045 return await interaction.editReply({
46 embeds: [
47 new EmojiEmbed()
48 .setEmoji("CHANNEL.TEXT.DELETE")
49 .setTitle("Staff Notifications Channel")
50 .setDescription(
51 "The channel you provided is not a valid channel"
52 )
53 .setStatus("Danger")
54 ]
55 });
pineafan708692b2022-07-24 22:16:22 +010056 }
pineafan63fc5e22022-08-04 22:04:10 +010057 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010058 if (channel.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010059 return interaction.editReply({
60 embeds: [
61 new EmojiEmbed()
62 .setTitle("Staff Notifications Channel")
63 .setDescription(
64 "You must choose a channel in this server"
65 )
66 .setStatus("Danger")
67 .setEmoji("CHANNEL.TEXT.DELETE")
68 ]
69 });
pineafan708692b2022-07-24 22:16:22 +010070 }
pineafan63fc5e22022-08-04 22:04:10 +010071 const confirmation = await new confirmationMessage(interaction)
pineafan708692b2022-07-24 22:16:22 +010072 .setEmoji("CHANNEL.TEXT.EDIT")
73 .setTitle("Staff Notifications Channel")
74 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010075 "This will be the channel all notifications, updates, user reports etc. will be sent to.\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +010076 `Are you sure you want to set the staff notifications channel to <#${channel.id}>?`
pineafan708692b2022-07-24 22:16:22 +010077 )
78 .setColor("Warning")
79 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010080 .send(true);
81 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010082 if (confirmation.success) {
83 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010084 await client.database.guilds.write(interaction.guild.id, {
85 "logging.staff.channel": channel.id
86 });
87 const { log, NucleusColors, entry, renderUser, renderChannel } =
88 client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010089 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010090 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010091 type: "staffChannelUpdate",
92 displayName: "Staff Notifications Channel Updated",
93 calculateType: "nucleusSettingsUpdated",
94 color: NucleusColors.yellow,
95 emoji: "CHANNEL.TEXT.EDIT",
96 timestamp: new Date().getTime()
97 },
98 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +010099 memberId: entry(
100 interaction.user.id,
101 `\`${interaction.user.id}\``
102 ),
103 changedBy: entry(
104 interaction.user.id,
105 renderUser(interaction.user)
106 ),
pineafan63fc5e22022-08-04 22:04:10 +0100107 channel: entry(channel.id, renderChannel(channel))
108 },
109 hidden: {
110 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +0100111 }
pineafan63fc5e22022-08-04 22:04:10 +0100112 };
113 log(data);
pineafan708692b2022-07-24 22:16:22 +0100114 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 return interaction.editReply({
116 embeds: [
117 new EmojiEmbed()
118 .setTitle("Staff Notifications Channel")
119 .setDescription(
120 "Something went wrong and the staff notifications channel could not be set"
121 )
122 .setStatus("Danger")
123 .setEmoji("CHANNEL.TEXT.DELETE")
124 ],
125 components: []
126 });
pineafan708692b2022-07-24 22:16:22 +0100127 }
128 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 return interaction.editReply({
130 embeds: [
131 new EmojiEmbed()
132 .setTitle("Staff Notifications Channel")
133 .setDescription("No changes were made")
134 .setStatus("Success")
135 .setEmoji("CHANNEL.TEXT.CREATE")
136 ],
137 components: []
138 });
pineafan708692b2022-07-24 22:16:22 +0100139 }
140 }
141 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100142 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +0100143 let channel = data.logging.staff.channel;
144 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 await interaction.editReply({
146 embeds: [
147 new EmojiEmbed()
148 .setTitle("Staff Notifications channel")
149 .setDescription(
150 channel
151 ? `Your staff notifications channel is currently set to <#${channel}>`
152 : "This server does not have a staff notifications channel"
153 )
154 .setStatus("Success")
155 .setEmoji("CHANNEL.TEXT.CREATE")
156 ],
157 components: [
158 new MessageActionRow().addComponents([
159 new MessageButton()
160 .setCustomId("clear")
161 .setLabel(
162 clicks ? "Click again to confirm" : "Reset channel"
163 )
164 .setEmoji(
165 getEmojiByName(
166 clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS",
167 "id"
168 )
169 )
170 .setStyle("DANGER")
171 .setDisabled(!channel)
172 ])
173 ]
174 });
pineafan708692b2022-07-24 22:16:22 +0100175 let i;
176 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100177 i = await m.awaitMessageComponent({ time: 300000 });
178 } catch (e) {
179 break;
180 }
pineafan63fc5e22022-08-04 22:04:10 +0100181 i.deferUpdate();
182 if ((i.component as MessageButton).customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100183 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100184 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100185 clicks = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 await client.database.guilds.write(interaction.guild.id, null, [
187 "logging.staff.channel"
188 ]);
pineafan708692b2022-07-24 22:16:22 +0100189 channel = undefined;
190 }
191 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100192 break;
pineafan708692b2022-07-24 22:16:22 +0100193 }
194 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100195 await interaction.editReply({
196 embeds: [
197 new EmojiEmbed()
198 .setTitle("Staff Notifications channel")
199 .setDescription(
200 channel
201 ? `Your staff notifications channel is currently set to <#${channel}>`
202 : "This server does not have a staff notifications channel"
203 )
204 .setStatus("Success")
205 .setEmoji("CHANNEL.TEXT.CREATE")
206 .setFooter({ text: "Message closed" })
207 ],
208 components: [
209 new MessageActionRow().addComponents([
210 new MessageButton()
211 .setCustomId("clear")
212 .setLabel("Clear")
213 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
214 .setStyle("SECONDARY")
215 .setDisabled(true)
216 ])
217 ]
218 });
pineafan63fc5e22022-08-04 22:04:10 +0100219};
pineafan708692b2022-07-24 22:16:22 +0100220
Skyler Grey75ea9172022-08-06 10:22:23 +0100221const check = (
222 interaction: CommandInteraction,
223 _defaultCheck: WrappedCheck
224) => {
225 const member = interaction.member as Discord.GuildMember;
226 if (!member.permissions.has("MANAGE_GUILD"))
227 throw "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100228 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100229};
pineafan708692b2022-07-24 22:16:22 +0100230
231export { command };
232export { callback };
233export { check };