blob: a0df97cd8019b937195d428f13ce2c5136e5aba1 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../../utils/defaultEmbeds.js";
Skyler Greyc634e2b2022-08-06 17:50:48 +01002import { ChannelType } from "discord-api-types/v9";
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";
Skyler Grey75ea9172022-08-06 10:22:23 +010012// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +010013import type { WrappedCheck } from "jshaiku";
pineafan708692b2022-07-24 22:16:22 +010014import client from "../../../utils/client.js";
15
16const command = (builder: SlashCommandSubcommandBuilder) =>
17 builder
pineafan63fc5e22022-08-04 22:04:10 +010018 .setName("staff")
19 .setDescription("Settings for the staff notifications channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010020 .addChannelOption((option) =>
21 option
22 .setName("channel")
23 .setDescription(
24 "The channel to set the staff notifications channel to"
25 )
26 .addChannelTypes([ChannelType.GuildNews, ChannelType.GuildText])
27 .setRequired(false)
28 );
pineafan708692b2022-07-24 22:16:22 +010029
Skyler Grey75ea9172022-08-06 10:22:23 +010030const callback = async (
31 interaction: CommandInteraction
32): Promise<unknown | void> => {
pineafan63fc5e22022-08-04 22:04:10 +010033 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010034 const m = (await interaction.reply({
35 embeds: LoadingEmbed,
36 ephemeral: true,
37 fetchReply: true
38 })) as Discord.Message;
pineafan708692b2022-07-24 22:16:22 +010039 if (interaction.options.getChannel("channel")) {
pineafan63fc5e22022-08-04 22:04:10 +010040 let channel;
pineafan708692b2022-07-24 22:16:22 +010041 try {
pineafan63fc5e22022-08-04 22:04:10 +010042 channel = interaction.options.getChannel("channel");
pineafan708692b2022-07-24 22:16:22 +010043 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010044 return await interaction.editReply({
45 embeds: [
46 new EmojiEmbed()
47 .setEmoji("CHANNEL.TEXT.DELETE")
48 .setTitle("Staff Notifications Channel")
49 .setDescription(
50 "The channel you provided is not a valid channel"
51 )
52 .setStatus("Danger")
53 ]
54 });
pineafan708692b2022-07-24 22:16:22 +010055 }
pineafan63fc5e22022-08-04 22:04:10 +010056 channel = channel as Discord.TextChannel;
pineafane23c4ec2022-07-27 21:56:27 +010057 if (channel.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010058 return interaction.editReply({
59 embeds: [
60 new EmojiEmbed()
61 .setTitle("Staff Notifications Channel")
62 .setDescription(
63 "You must choose a channel in this server"
64 )
65 .setStatus("Danger")
66 .setEmoji("CHANNEL.TEXT.DELETE")
67 ]
68 });
pineafan708692b2022-07-24 22:16:22 +010069 }
pineafan63fc5e22022-08-04 22:04:10 +010070 const confirmation = await new confirmationMessage(interaction)
pineafan708692b2022-07-24 22:16:22 +010071 .setEmoji("CHANNEL.TEXT.EDIT")
72 .setTitle("Staff Notifications Channel")
73 .setDescription(
pineafan63fc5e22022-08-04 22:04:10 +010074 "This will be the channel all notifications, updates, user reports etc. will be sent to.\n\n" +
Skyler Grey75ea9172022-08-06 10:22:23 +010075 `Are you sure you want to set the staff notifications channel to <#${channel.id}>?`
pineafan708692b2022-07-24 22:16:22 +010076 )
77 .setColor("Warning")
78 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010079 .send(true);
80 if (confirmation.cancelled) return;
pineafan708692b2022-07-24 22:16:22 +010081 if (confirmation.success) {
82 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010083 await client.database.guilds.write(interaction.guild.id, {
84 "logging.staff.channel": channel.id
85 });
86 const { log, NucleusColors, entry, renderUser, renderChannel } =
87 client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010088 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010089 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010090 type: "staffChannelUpdate",
91 displayName: "Staff Notifications Channel Updated",
92 calculateType: "nucleusSettingsUpdated",
93 color: NucleusColors.yellow,
94 emoji: "CHANNEL.TEXT.EDIT",
95 timestamp: new Date().getTime()
96 },
97 list: {
Skyler Grey75ea9172022-08-06 10:22:23 +010098 memberId: entry(
99 interaction.user.id,
100 `\`${interaction.user.id}\``
101 ),
102 changedBy: entry(
103 interaction.user.id,
104 renderUser(interaction.user)
105 ),
pineafan63fc5e22022-08-04 22:04:10 +0100106 channel: entry(channel.id, renderChannel(channel))
107 },
108 hidden: {
109 guild: interaction.guild.id
pineafan708692b2022-07-24 22:16:22 +0100110 }
pineafan63fc5e22022-08-04 22:04:10 +0100111 };
112 log(data);
pineafan708692b2022-07-24 22:16:22 +0100113 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100114 return interaction.editReply({
115 embeds: [
116 new EmojiEmbed()
117 .setTitle("Staff Notifications Channel")
118 .setDescription(
119 "Something went wrong and the staff notifications channel could not be set"
120 )
121 .setStatus("Danger")
122 .setEmoji("CHANNEL.TEXT.DELETE")
123 ],
124 components: []
125 });
pineafan708692b2022-07-24 22:16:22 +0100126 }
127 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 return interaction.editReply({
129 embeds: [
130 new EmojiEmbed()
131 .setTitle("Staff Notifications Channel")
132 .setDescription("No changes were made")
133 .setStatus("Success")
134 .setEmoji("CHANNEL.TEXT.CREATE")
135 ],
136 components: []
137 });
pineafan708692b2022-07-24 22:16:22 +0100138 }
139 }
140 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100141 const data = await client.database.guilds.read(interaction.guild.id);
pineafan708692b2022-07-24 22:16:22 +0100142 let channel = data.logging.staff.channel;
143 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 await interaction.editReply({
145 embeds: [
146 new EmojiEmbed()
147 .setTitle("Staff Notifications channel")
148 .setDescription(
149 channel
150 ? `Your staff notifications channel is currently set to <#${channel}>`
151 : "This server does not have a staff notifications channel"
152 )
153 .setStatus("Success")
154 .setEmoji("CHANNEL.TEXT.CREATE")
155 ],
156 components: [
157 new MessageActionRow().addComponents([
158 new MessageButton()
159 .setCustomId("clear")
160 .setLabel(
161 clicks ? "Click again to confirm" : "Reset channel"
162 )
163 .setEmoji(
164 getEmojiByName(
165 clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS",
166 "id"
167 )
168 )
169 .setStyle("DANGER")
170 .setDisabled(!channel)
171 ])
172 ]
173 });
pineafan708692b2022-07-24 22:16:22 +0100174 let i;
175 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100176 i = await m.awaitMessageComponent({ time: 300000 });
177 } catch (e) {
178 break;
179 }
pineafan63fc5e22022-08-04 22:04:10 +0100180 i.deferUpdate();
181 if ((i.component as MessageButton).customId === "clear") {
pineafan708692b2022-07-24 22:16:22 +0100182 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100183 if (clicks === 2) {
pineafan708692b2022-07-24 22:16:22 +0100184 clicks = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +0100185 await client.database.guilds.write(interaction.guild.id, null, [
186 "logging.staff.channel"
187 ]);
pineafan708692b2022-07-24 22:16:22 +0100188 channel = undefined;
189 }
190 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100191 break;
pineafan708692b2022-07-24 22:16:22 +0100192 }
193 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100194 await interaction.editReply({
195 embeds: [
196 new EmojiEmbed()
197 .setTitle("Staff Notifications channel")
198 .setDescription(
199 channel
200 ? `Your staff notifications channel is currently set to <#${channel}>`
201 : "This server does not have a staff notifications channel"
202 )
203 .setStatus("Success")
204 .setEmoji("CHANNEL.TEXT.CREATE")
205 .setFooter({ text: "Message closed" })
206 ],
207 components: [
208 new MessageActionRow().addComponents([
209 new MessageButton()
210 .setCustomId("clear")
211 .setLabel("Clear")
212 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
213 .setStyle("SECONDARY")
214 .setDisabled(true)
215 ])
216 ]
217 });
pineafan63fc5e22022-08-04 22:04:10 +0100218};
pineafan708692b2022-07-24 22:16:22 +0100219
Skyler Grey75ea9172022-08-06 10:22:23 +0100220const check = (
221 interaction: CommandInteraction,
222 _defaultCheck: WrappedCheck
223) => {
224 const member = interaction.member as Discord.GuildMember;
225 if (!member.permissions.has("MANAGE_GUILD"))
226 throw "You must have the *Manage Server* permission to use this command";
pineafan708692b2022-07-24 22:16:22 +0100227 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100228};
pineafan708692b2022-07-24 22:16:22 +0100229
230export { command };
231export { callback };
232export { check };