blob: 0eb4553e6e9d25b428885b5eef15bd060c441bc5 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
Skyler Grey75ea9172022-08-06 10:22:23 +01002import Discord, {
3 CommandInteraction,
pineafan3a02ea32022-08-11 21:35:04 +01004 Interaction,
Skyler Grey75ea9172022-08-06 10:22:23 +01005 Message,
6 MessageActionRow,
7 MessageActionRowComponent,
8 MessageButton,
9 MessageComponentInteraction,
Skyler Grey75ea9172022-08-06 10:22:23 +010010 MessageSelectMenu,
pineafan3a02ea32022-08-11 21:35:04 +010011 ModalSubmitInteraction,
Skyler Grey75ea9172022-08-06 10:22:23 +010012 Role,
13 SelectMenuInteraction,
14 TextInputComponent
15} from "discord.js";
pineafanda6e5342022-07-03 10:03:16 +010016import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
17import confirmationMessage from "../../utils/confirmationMessage.js";
18import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan3a02ea32022-08-11 21:35:04 +010019import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafanda6e5342022-07-03 10:03:16 +010020import client from "../../utils/client.js";
pineafan63fc5e22022-08-04 22:04:10 +010021import { modalInteractionCollector } from "../../utils/dualCollector.js";
pineafan4f164f32022-02-26 22:07:12 +000022
23const command = (builder: SlashCommandSubcommandBuilder) =>
24 builder
pineafan63fc5e22022-08-04 22:04:10 +010025 .setName("verify")
26 .setDescription("Manage the role given after typing /verify")
Skyler Grey75ea9172022-08-06 10:22:23 +010027 .addRoleOption((option) =>
Skyler Grey11236ba2022-08-08 21:13:33 +010028 option.setName("role").setDescription("The role to give after verifying").setRequired(false)
Skyler Grey75ea9172022-08-06 10:22:23 +010029 );
pineafan4f164f32022-02-26 22:07:12 +000030
pineafan3a02ea32022-08-11 21:35:04 +010031const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Grey75ea9172022-08-06 10:22:23 +010032 const m = (await interaction.reply({
33 embeds: LoadingEmbed,
34 ephemeral: true,
35 fetchReply: true
36 })) as Message;
pineafan6702cef2022-06-13 17:52:37 +010037 if (interaction.options.getRole("role")) {
Skyler Grey1a67e182022-08-04 23:05:44 +010038 let role: Role;
pineafan6702cef2022-06-13 17:52:37 +010039 try {
Skyler Grey1a67e182022-08-04 23:05:44 +010040 role = interaction.options.getRole("role") as Role;
pineafan6702cef2022-06-13 17:52:37 +010041 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010042 return await interaction.editReply({
43 embeds: [
44 new EmojiEmbed()
45 .setEmoji("GUILD.ROLES.DELETE")
46 .setTitle("Verify Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010047 .setDescription("The role you provided is not a valid role")
Skyler Grey75ea9172022-08-06 10:22:23 +010048 .setStatus("Danger")
49 ]
50 });
pineafan6702cef2022-06-13 17:52:37 +010051 }
pineafan63fc5e22022-08-04 22:04:10 +010052 role = role as Discord.Role;
pineafan3a02ea32022-08-11 21:35:04 +010053 if (role.guild.id !== interaction.guild!.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010054 return interaction.editReply({
55 embeds: [
56 new EmojiEmbed()
57 .setTitle("Verify Role")
58 .setDescription("You must choose a role in this server")
59 .setStatus("Danger")
60 .setEmoji("GUILD.ROLES.DELETE")
61 ]
62 });
pineafan6702cef2022-06-13 17:52:37 +010063 }
pineafan63fc5e22022-08-04 22:04:10 +010064 const confirmation = await new confirmationMessage(interaction)
pineafan6702cef2022-06-13 17:52:37 +010065 .setEmoji("GUILD.ROLES.EDIT")
66 .setTitle("Verify Role")
Skyler Grey11236ba2022-08-08 21:13:33 +010067 .setDescription(`Are you sure you want to set the verify role to <@&${role.id}>?`)
pineafan6702cef2022-06-13 17:52:37 +010068 .setColor("Warning")
69 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010070 .send(true);
71 if (confirmation.cancelled) return;
pineafan6702cef2022-06-13 17:52:37 +010072 if (confirmation.success) {
73 try {
pineafan3a02ea32022-08-11 21:35:04 +010074 await client.database.guilds.write(interaction.guild!.id, {
Skyler Grey75ea9172022-08-06 10:22:23 +010075 "verify.role": role.id,
76 "verify.enabled": true
77 });
Skyler Grey11236ba2022-08-08 21:13:33 +010078 const { log, NucleusColors, entry, renderUser, renderRole } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010079 const data = {
Skyler Grey75ea9172022-08-06 10:22:23 +010080 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010081 type: "verifyRoleChanged",
82 displayName: "Verify Role Changed",
83 calculateType: "nucleusSettingsUpdated",
84 color: NucleusColors.green,
85 emoji: "CONTROL.BLOCKTICK",
86 timestamp: new Date().getTime()
87 },
88 list: {
Skyler Grey11236ba2022-08-08 21:13:33 +010089 memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
90 changedBy: entry(interaction.user.id, renderUser(interaction.user)),
pineafan63fc5e22022-08-04 22:04:10 +010091 role: entry(role.id, renderRole(role))
92 },
93 hidden: {
pineafan3a02ea32022-08-11 21:35:04 +010094 guild: interaction.guild!.id
pineafanda6e5342022-07-03 10:03:16 +010095 }
pineafan63fc5e22022-08-04 22:04:10 +010096 };
97 log(data);
pineafan6702cef2022-06-13 17:52:37 +010098 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +010099 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 return interaction.editReply({
101 embeds: [
102 new EmojiEmbed()
103 .setTitle("Verify Role")
Skyler Grey11236ba2022-08-08 21:13:33 +0100104 .setDescription("Something went wrong while setting the verify role")
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 .setStatus("Danger")
106 .setEmoji("GUILD.ROLES.DELETE")
107 ],
108 components: []
109 });
pineafan6702cef2022-06-13 17:52:37 +0100110 }
111 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 return interaction.editReply({
113 embeds: [
114 new EmojiEmbed()
115 .setTitle("Verify Role")
116 .setDescription("No changes were made")
117 .setStatus("Success")
118 .setEmoji("GUILD.ROLES.CREATE")
119 ],
120 components: []
121 });
pineafan6702cef2022-06-13 17:52:37 +0100122 }
123 }
124 let clicks = 0;
pineafan3a02ea32022-08-11 21:35:04 +0100125 const data = await client.database.guilds.read(interaction.guild!.id);
pineafan6702cef2022-06-13 17:52:37 +0100126 let role = data.verify.role;
127 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 await interaction.editReply({
129 embeds: [
130 new EmojiEmbed()
131 .setTitle("Verify Role")
132 .setDescription(
Skyler Grey11236ba2022-08-08 21:13:33 +0100133 role ? `Your verify role is currently set to <@&${role}>` : "You have not set a verify role"
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 )
135 .setStatus("Success")
136 .setEmoji("GUILD.ROLES.CREATE")
137 ],
138 components: [
139 new MessageActionRow().addComponents([
140 new MessageButton()
141 .setCustomId("clear")
Skyler Grey11236ba2022-08-08 21:13:33 +0100142 .setLabel(clicks ? "Click again to confirm" : "Reset role")
143 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 .setStyle("DANGER")
145 .setDisabled(!role),
146 new MessageButton()
147 .setCustomId("send")
148 .setLabel("Add verify button")
149 .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id"))
150 .setStyle("PRIMARY")
151 ])
152 ]
153 });
Skyler Grey1a67e182022-08-04 23:05:44 +0100154 let i: MessageComponentInteraction;
pineafan6702cef2022-06-13 17:52:37 +0100155 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100156 i = await m.awaitMessageComponent({ time: 300000 });
157 } catch (e) {
158 break;
159 }
pineafan63fc5e22022-08-04 22:04:10 +0100160 i.deferUpdate();
Skyler Grey1a67e182022-08-04 23:05:44 +0100161 if ((i.component as MessageActionRowComponent).customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100162 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100163 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100164 clicks = 0;
pineafan3a02ea32022-08-11 21:35:04 +0100165 await client.database.guilds.write(interaction.guild!.id, null, ["verify.role", "verify.enabled"]);
pineafan6702cef2022-06-13 17:52:37 +0100166 role = undefined;
167 }
Skyler Grey11236ba2022-08-08 21:13:33 +0100168 } else if ((i.component as MessageActionRowComponent).customId === "send") {
pineafan41d93562022-07-30 22:10:15 +0100169 const verifyMessages = [
Skyler Grey75ea9172022-08-06 10:22:23 +0100170 {
171 label: "Verify",
172 description: "Click the button below to get verified"
173 },
174 {
175 label: "Get verified",
Skyler Grey11236ba2022-08-08 21:13:33 +0100176 description: "To get access to the rest of the server, click the button below"
Skyler Grey75ea9172022-08-06 10:22:23 +0100177 },
178 {
179 label: "Ready to verify?",
180 description: "Click the button below to verify yourself"
181 }
pineafan63fc5e22022-08-04 22:04:10 +0100182 ];
pineafan41d93562022-07-30 22:10:15 +0100183 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 await interaction.editReply({
185 embeds: [
186 new EmojiEmbed()
187 .setTitle("Verify Button")
Skyler Grey11236ba2022-08-08 21:13:33 +0100188 .setDescription("Select a message template to send in this channel")
Skyler Grey75ea9172022-08-06 10:22:23 +0100189 .setFooter({
Skyler Grey11236ba2022-08-08 21:13:33 +0100190 text: role ? "" : "You do no have a verify role set so the button will not work."
Skyler Grey75ea9172022-08-06 10:22:23 +0100191 })
192 .setStatus(role ? "Success" : "Warning")
193 .setEmoji("GUILD.ROLES.CREATE")
194 ],
195 components: [
196 new MessageActionRow().addComponents([
197 new MessageSelectMenu()
198 .setOptions(
199 verifyMessages.map(
200 (
201 t: {
202 label: string;
203 description: string;
204 value?: string;
205 },
206 index
207 ) => {
208 t.value = index.toString();
209 return t as {
210 value: string;
211 label: string;
212 description: string;
213 };
214 }
215 )
216 )
217 .setCustomId("template")
218 .setMaxValues(1)
219 .setMinValues(1)
220 .setPlaceholder("Select a message template")
221 ]),
222 new MessageActionRow().addComponents([
223 new MessageButton()
224 .setCustomId("back")
225 .setLabel("Back")
226 .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
227 .setStyle("DANGER"),
Skyler Grey11236ba2022-08-08 21:13:33 +0100228 new MessageButton().setCustomId("blank").setLabel("Empty").setStyle("SECONDARY"),
Skyler Grey75ea9172022-08-06 10:22:23 +0100229 new MessageButton()
230 .setCustomId("custom")
231 .setLabel("Custom")
232 .setEmoji(getEmojiByName("TICKETS.OTHER", "id"))
233 .setStyle("PRIMARY")
234 ])
235 ]
236 });
Skyler Grey1a67e182022-08-04 23:05:44 +0100237 let i: MessageComponentInteraction;
pineafan41d93562022-07-30 22:10:15 +0100238 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100239 i = await m.awaitMessageComponent({ time: 300000 });
240 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100241 break;
Skyler Grey75ea9172022-08-06 10:22:23 +0100242 }
Skyler Grey11236ba2022-08-08 21:13:33 +0100243 if ((i.component as MessageActionRowComponent).customId === "template") {
pineafan63fc5e22022-08-04 22:04:10 +0100244 i.deferUpdate();
pineafan3a02ea32022-08-11 21:35:04 +0100245 await interaction.channel!.send({
Skyler Grey75ea9172022-08-06 10:22:23 +0100246 embeds: [
247 new EmojiEmbed()
pineafan3a02ea32022-08-11 21:35:04 +0100248 .setTitle(verifyMessages[parseInt((i as SelectMenuInteraction).values[0]!)]!.label)
Skyler Grey75ea9172022-08-06 10:22:23 +0100249 .setDescription(
pineafan3a02ea32022-08-11 21:35:04 +0100250 verifyMessages[parseInt((i as SelectMenuInteraction).values[0]!)]!.description
Skyler Grey75ea9172022-08-06 10:22:23 +0100251 )
252 .setStatus("Success")
253 .setEmoji("CONTROL.BLOCKTICK")
254 ],
255 components: [
256 new MessageActionRow().addComponents([
257 new MessageButton()
258 .setLabel("Verify")
Skyler Grey11236ba2022-08-08 21:13:33 +0100259 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100260 .setStyle("SUCCESS")
261 .setCustomId("verifybutton")
262 ])
263 ]
264 });
pineafan63fc5e22022-08-04 22:04:10 +0100265 break;
Skyler Grey11236ba2022-08-08 21:13:33 +0100266 } else if ((i.component as MessageActionRowComponent).customId === "blank") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100267 i.deferUpdate();
pineafan3a02ea32022-08-11 21:35:04 +0100268 await interaction.channel!.send({
Skyler Grey75ea9172022-08-06 10:22:23 +0100269 components: [
270 new MessageActionRow().addComponents([
271 new MessageButton()
272 .setLabel("Verify")
Skyler Grey11236ba2022-08-08 21:13:33 +0100273 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100274 .setStyle("SUCCESS")
275 .setCustomId("verifybutton")
276 ])
277 ]
278 });
279 break;
Skyler Grey11236ba2022-08-08 21:13:33 +0100280 } else if ((i.component as MessageActionRowComponent).customId === "custom") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100281 await i.showModal(
282 new Discord.Modal()
283 .setCustomId("modal")
284 .setTitle("Enter embed details")
285 .addComponents(
286 new MessageActionRow<TextInputComponent>().addComponents(
287 new TextInputComponent()
288 .setCustomId("title")
289 .setLabel("Title")
290 .setMaxLength(256)
291 .setRequired(true)
292 .setStyle("SHORT")
293 ),
294 new MessageActionRow<TextInputComponent>().addComponents(
295 new TextInputComponent()
296 .setCustomId("description")
297 .setLabel("Description")
298 .setMaxLength(4000)
299 .setRequired(true)
300 .setStyle("PARAGRAPH")
301 )
302 )
303 );
pineafan41d93562022-07-30 22:10:15 +0100304 await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100305 embeds: [
306 new EmojiEmbed()
307 .setTitle("Verify Button")
Skyler Grey11236ba2022-08-08 21:13:33 +0100308 .setDescription("Modal opened. If you can't see it, click back and try again.")
Skyler Grey75ea9172022-08-06 10:22:23 +0100309 .setStatus("Success")
310 .setEmoji("GUILD.TICKET.OPEN")
311 ],
312 components: [
313 new MessageActionRow().addComponents([
314 new MessageButton()
315 .setLabel("Back")
Skyler Grey11236ba2022-08-08 21:13:33 +0100316 .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100317 .setStyle("PRIMARY")
318 .setCustomId("back")
319 ])
320 ]
pineafan41d93562022-07-30 22:10:15 +0100321 });
322 let out;
323 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100324 out = await modalInteractionCollector(
325 m,
pineafan3a02ea32022-08-11 21:35:04 +0100326 (m: Interaction) =>
327 (m as MessageComponentInteraction | ModalSubmitInteraction).channelId ===
328 interaction.channelId,
Skyler Grey75ea9172022-08-06 10:22:23 +0100329 (m) => m.customId === "modify"
330 );
331 } catch (e) {
332 break;
333 }
pineafan3a02ea32022-08-11 21:35:04 +0100334 if (out === null) {
335 continue;
336 } else if (out instanceof ModalSubmitInteraction) {
pineafan63fc5e22022-08-04 22:04:10 +0100337 const title = out.fields.getTextInputValue("title");
Skyler Grey11236ba2022-08-08 21:13:33 +0100338 const description = out.fields.getTextInputValue("description");
pineafan3a02ea32022-08-11 21:35:04 +0100339 await interaction.channel!.send({
Skyler Grey75ea9172022-08-06 10:22:23 +0100340 embeds: [
341 new EmojiEmbed()
342 .setTitle(title)
343 .setDescription(description)
344 .setStatus("Success")
345 .setEmoji("CONTROL.BLOCKTICK")
346 ],
347 components: [
348 new MessageActionRow().addComponents([
349 new MessageButton()
350 .setLabel("Verify")
Skyler Grey11236ba2022-08-08 21:13:33 +0100351 .setEmoji(getEmojiByName("CONTROL.TICK", "id"))
Skyler Grey75ea9172022-08-06 10:22:23 +0100352 .setStyle("SUCCESS")
353 .setCustomId("verifybutton")
354 ])
355 ]
356 });
pineafan63fc5e22022-08-04 22:04:10 +0100357 break;
Skyler Grey75ea9172022-08-06 10:22:23 +0100358 } else {
359 continue;
360 }
pineafan41d93562022-07-30 22:10:15 +0100361 }
362 }
pineafan6702cef2022-06-13 17:52:37 +0100363 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100364 i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100365 break;
pineafan6702cef2022-06-13 17:52:37 +0100366 }
367 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100368 await interaction.editReply({
369 embeds: [m.embeds[0]!.setFooter({ text: "Message closed" })],
370 components: []
371 });
pineafan63fc5e22022-08-04 22:04:10 +0100372};
pineafan4f164f32022-02-26 22:07:12 +0000373
Skyler Grey1a67e182022-08-04 23:05:44 +0100374const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100375 const member = interaction.member as Discord.GuildMember;
376 if (!member.permissions.has("MANAGE_GUILD"))
pineafan3a02ea32022-08-11 21:35:04 +0100377 throw new Error("You must have the *Manage Server* permission to use this command");
pineafan4f164f32022-02-26 22:07:12 +0000378 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100379};
pineafan4f164f32022-02-26 22:07:12 +0000380
381export { command };
382export { callback };
pineafan6702cef2022-06-13 17:52:37 +0100383export { check };