blob: e0af802d797e8c0ae8c37fcb49cc86d3facdacfd [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,
4 Message,
5 MessageActionRow,
6 MessageActionRowComponent,
7 MessageButton,
8 MessageComponentInteraction,
9 MessageEmbed,
10 MessageSelectMenu,
11 Role,
12 SelectMenuInteraction,
13 TextInputComponent
14} from "discord.js";
pineafanda6e5342022-07-03 10:03:16 +010015import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
16import confirmationMessage from "../../utils/confirmationMessage.js";
17import getEmojiByName from "../../utils/getEmojiByName.js";
pineafanbd02b4a2022-08-05 22:01:38 +010018import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafanda6e5342022-07-03 10:03:16 +010019import client from "../../utils/client.js";
pineafan63fc5e22022-08-04 22:04:10 +010020import { modalInteractionCollector } from "../../utils/dualCollector.js";
pineafan4f164f32022-02-26 22:07:12 +000021
22const command = (builder: SlashCommandSubcommandBuilder) =>
23 builder
pineafan63fc5e22022-08-04 22:04:10 +010024 .setName("verify")
25 .setDescription("Manage the role given after typing /verify")
Skyler Grey75ea9172022-08-06 10:22:23 +010026 .addRoleOption((option) =>
27 option
28 .setName("role")
29 .setDescription("The role to give after verifying")
30 .setRequired(false)
31 );
pineafan4f164f32022-02-26 22:07:12 +000032
Skyler Grey75ea9172022-08-06 10:22:23 +010033const callback = async (
34 interaction: CommandInteraction
35): Promise<void | unknown> => {
36 const m = (await interaction.reply({
37 embeds: LoadingEmbed,
38 ephemeral: true,
39 fetchReply: true
40 })) as Message;
pineafan6702cef2022-06-13 17:52:37 +010041 if (interaction.options.getRole("role")) {
Skyler Grey1a67e182022-08-04 23:05:44 +010042 let role: Role;
pineafan6702cef2022-06-13 17:52:37 +010043 try {
Skyler Grey1a67e182022-08-04 23:05:44 +010044 role = interaction.options.getRole("role") as Role;
pineafan6702cef2022-06-13 17:52:37 +010045 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010046 return await interaction.editReply({
47 embeds: [
48 new EmojiEmbed()
49 .setEmoji("GUILD.ROLES.DELETE")
50 .setTitle("Verify Role")
51 .setDescription(
52 "The role you provided is not a valid role"
53 )
54 .setStatus("Danger")
55 ]
56 });
pineafan6702cef2022-06-13 17:52:37 +010057 }
pineafan63fc5e22022-08-04 22:04:10 +010058 role = role as Discord.Role;
pineafane23c4ec2022-07-27 21:56:27 +010059 if (role.guild.id !== interaction.guild.id) {
Skyler Grey75ea9172022-08-06 10:22:23 +010060 return interaction.editReply({
61 embeds: [
62 new EmojiEmbed()
63 .setTitle("Verify Role")
64 .setDescription("You must choose a role in this server")
65 .setStatus("Danger")
66 .setEmoji("GUILD.ROLES.DELETE")
67 ]
68 });
pineafan6702cef2022-06-13 17:52:37 +010069 }
pineafan63fc5e22022-08-04 22:04:10 +010070 const confirmation = await new confirmationMessage(interaction)
pineafan6702cef2022-06-13 17:52:37 +010071 .setEmoji("GUILD.ROLES.EDIT")
72 .setTitle("Verify Role")
Skyler Grey75ea9172022-08-06 10:22:23 +010073 .setDescription(
74 `Are you sure you want to set the verify role to <@&${role.id}>?`
75 )
pineafan6702cef2022-06-13 17:52:37 +010076 .setColor("Warning")
77 .setInverted(true)
pineafan63fc5e22022-08-04 22:04:10 +010078 .send(true);
79 if (confirmation.cancelled) return;
pineafan6702cef2022-06-13 17:52:37 +010080 if (confirmation.success) {
81 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010082 await client.database.guilds.write(interaction.guild.id, {
83 "verify.role": role.id,
84 "verify.enabled": true
85 });
86 const { log, NucleusColors, entry, renderUser, renderRole } =
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: "verifyRoleChanged",
91 displayName: "Verify Role Changed",
92 calculateType: "nucleusSettingsUpdated",
93 color: NucleusColors.green,
94 emoji: "CONTROL.BLOCKTICK",
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 role: entry(role.id, renderRole(role))
107 },
108 hidden: {
109 guild: interaction.guild.id
pineafanda6e5342022-07-03 10:03:16 +0100110 }
pineafan63fc5e22022-08-04 22:04:10 +0100111 };
112 log(data);
pineafan6702cef2022-06-13 17:52:37 +0100113 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100114 console.log(e);
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 return interaction.editReply({
116 embeds: [
117 new EmojiEmbed()
118 .setTitle("Verify Role")
119 .setDescription(
120 "Something went wrong while setting the verify role"
121 )
122 .setStatus("Danger")
123 .setEmoji("GUILD.ROLES.DELETE")
124 ],
125 components: []
126 });
pineafan6702cef2022-06-13 17:52:37 +0100127 }
128 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 return interaction.editReply({
130 embeds: [
131 new EmojiEmbed()
132 .setTitle("Verify Role")
133 .setDescription("No changes were made")
134 .setStatus("Success")
135 .setEmoji("GUILD.ROLES.CREATE")
136 ],
137 components: []
138 });
pineafan6702cef2022-06-13 17:52:37 +0100139 }
140 }
141 let clicks = 0;
pineafan63fc5e22022-08-04 22:04:10 +0100142 const data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +0100143 let role = data.verify.role;
144 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 await interaction.editReply({
146 embeds: [
147 new EmojiEmbed()
148 .setTitle("Verify Role")
149 .setDescription(
150 role
151 ? `Your verify role is currently set to <@&${role}>`
152 : "You have not set a verify role"
153 )
154 .setStatus("Success")
155 .setEmoji("GUILD.ROLES.CREATE")
156 ],
157 components: [
158 new MessageActionRow().addComponents([
159 new MessageButton()
160 .setCustomId("clear")
161 .setLabel(
162 clicks ? "Click again to confirm" : "Reset role"
163 )
164 .setEmoji(
165 getEmojiByName(
166 clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS",
167 "id"
168 )
169 )
170 .setStyle("DANGER")
171 .setDisabled(!role),
172 new MessageButton()
173 .setCustomId("send")
174 .setLabel("Add verify button")
175 .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id"))
176 .setStyle("PRIMARY")
177 ])
178 ]
179 });
Skyler Grey1a67e182022-08-04 23:05:44 +0100180 let i: MessageComponentInteraction;
pineafan6702cef2022-06-13 17:52:37 +0100181 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100182 i = await m.awaitMessageComponent({ time: 300000 });
183 } catch (e) {
184 break;
185 }
pineafan63fc5e22022-08-04 22:04:10 +0100186 i.deferUpdate();
Skyler Grey1a67e182022-08-04 23:05:44 +0100187 if ((i.component as MessageActionRowComponent).customId === "clear") {
pineafan6702cef2022-06-13 17:52:37 +0100188 clicks += 1;
pineafane23c4ec2022-07-27 21:56:27 +0100189 if (clicks === 2) {
pineafan6702cef2022-06-13 17:52:37 +0100190 clicks = 0;
Skyler Grey75ea9172022-08-06 10:22:23 +0100191 await client.database.guilds.write(interaction.guild.id, null, [
192 "verify.role",
193 "verify.enabled"
194 ]);
pineafan6702cef2022-06-13 17:52:37 +0100195 role = undefined;
196 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100197 } else if (
198 (i.component as MessageActionRowComponent).customId === "send"
199 ) {
pineafan41d93562022-07-30 22:10:15 +0100200 const verifyMessages = [
Skyler Grey75ea9172022-08-06 10:22:23 +0100201 {
202 label: "Verify",
203 description: "Click the button below to get verified"
204 },
205 {
206 label: "Get verified",
207 description:
208 "To get access to the rest of the server, click the button below"
209 },
210 {
211 label: "Ready to verify?",
212 description: "Click the button below to verify yourself"
213 }
pineafan63fc5e22022-08-04 22:04:10 +0100214 ];
pineafan41d93562022-07-30 22:10:15 +0100215 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100216 await interaction.editReply({
217 embeds: [
218 new EmojiEmbed()
219 .setTitle("Verify Button")
220 .setDescription(
221 "Select a message template to send in this channel"
222 )
223 .setFooter({
224 text: role
225 ? ""
226 : "You do no have a verify role set so the button will not work."
227 })
228 .setStatus(role ? "Success" : "Warning")
229 .setEmoji("GUILD.ROLES.CREATE")
230 ],
231 components: [
232 new MessageActionRow().addComponents([
233 new MessageSelectMenu()
234 .setOptions(
235 verifyMessages.map(
236 (
237 t: {
238 label: string;
239 description: string;
240 value?: string;
241 },
242 index
243 ) => {
244 t.value = index.toString();
245 return t as {
246 value: string;
247 label: string;
248 description: string;
249 };
250 }
251 )
252 )
253 .setCustomId("template")
254 .setMaxValues(1)
255 .setMinValues(1)
256 .setPlaceholder("Select a message template")
257 ]),
258 new MessageActionRow().addComponents([
259 new MessageButton()
260 .setCustomId("back")
261 .setLabel("Back")
262 .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
263 .setStyle("DANGER"),
264 new MessageButton()
265 .setCustomId("blank")
266 .setLabel("Empty")
267 .setStyle("SECONDARY"),
268 new MessageButton()
269 .setCustomId("custom")
270 .setLabel("Custom")
271 .setEmoji(getEmojiByName("TICKETS.OTHER", "id"))
272 .setStyle("PRIMARY")
273 ])
274 ]
275 });
Skyler Grey1a67e182022-08-04 23:05:44 +0100276 let i: MessageComponentInteraction;
pineafan41d93562022-07-30 22:10:15 +0100277 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100278 i = await m.awaitMessageComponent({ time: 300000 });
279 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100280 break;
Skyler Grey75ea9172022-08-06 10:22:23 +0100281 }
282 if (
283 (i.component as MessageActionRowComponent).customId ===
284 "template"
285 ) {
pineafan63fc5e22022-08-04 22:04:10 +0100286 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100287 await interaction.channel.send({
288 embeds: [
289 new EmojiEmbed()
290 .setTitle(
291 verifyMessages[
292 parseInt(
293 (i as SelectMenuInteraction)
294 .values[0]
295 )
296 ].label
297 )
298 .setDescription(
299 verifyMessages[
300 parseInt(
301 (i as SelectMenuInteraction)
302 .values[0]
303 )
304 ].description
305 )
306 .setStatus("Success")
307 .setEmoji("CONTROL.BLOCKTICK")
308 ],
309 components: [
310 new MessageActionRow().addComponents([
311 new MessageButton()
312 .setLabel("Verify")
313 .setEmoji(
314 getEmojiByName("CONTROL.TICK", "id")
315 )
316 .setStyle("SUCCESS")
317 .setCustomId("verifybutton")
318 ])
319 ]
320 });
pineafan63fc5e22022-08-04 22:04:10 +0100321 break;
Skyler Grey75ea9172022-08-06 10:22:23 +0100322 } else if (
323 (i.component as MessageActionRowComponent).customId ===
324 "blank"
325 ) {
326 i.deferUpdate();
327 await interaction.channel.send({
328 components: [
329 new MessageActionRow().addComponents([
330 new MessageButton()
331 .setLabel("Verify")
332 .setEmoji(
333 getEmojiByName("CONTROL.TICK", "id")
334 )
335 .setStyle("SUCCESS")
336 .setCustomId("verifybutton")
337 ])
338 ]
339 });
340 break;
341 } else if (
342 (i.component as MessageActionRowComponent).customId ===
343 "custom"
344 ) {
345 await i.showModal(
346 new Discord.Modal()
347 .setCustomId("modal")
348 .setTitle("Enter embed details")
349 .addComponents(
350 new MessageActionRow<TextInputComponent>().addComponents(
351 new TextInputComponent()
352 .setCustomId("title")
353 .setLabel("Title")
354 .setMaxLength(256)
355 .setRequired(true)
356 .setStyle("SHORT")
357 ),
358 new MessageActionRow<TextInputComponent>().addComponents(
359 new TextInputComponent()
360 .setCustomId("description")
361 .setLabel("Description")
362 .setMaxLength(4000)
363 .setRequired(true)
364 .setStyle("PARAGRAPH")
365 )
366 )
367 );
pineafan41d93562022-07-30 22:10:15 +0100368 await interaction.editReply({
Skyler Grey75ea9172022-08-06 10:22:23 +0100369 embeds: [
370 new EmojiEmbed()
371 .setTitle("Verify Button")
372 .setDescription(
373 "Modal opened. If you can't see it, click back and try again."
374 )
375 .setStatus("Success")
376 .setEmoji("GUILD.TICKET.OPEN")
377 ],
378 components: [
379 new MessageActionRow().addComponents([
380 new MessageButton()
381 .setLabel("Back")
382 .setEmoji(
383 getEmojiByName("CONTROL.LEFT", "id")
384 )
385 .setStyle("PRIMARY")
386 .setCustomId("back")
387 ])
388 ]
pineafan41d93562022-07-30 22:10:15 +0100389 });
390 let out;
391 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100392 out = await modalInteractionCollector(
393 m,
394 (m) => m.channel.id === interaction.channel.id,
395 (m) => m.customId === "modify"
396 );
397 } catch (e) {
398 break;
399 }
pineafan41d93562022-07-30 22:10:15 +0100400 if (out.fields) {
pineafan63fc5e22022-08-04 22:04:10 +0100401 const title = out.fields.getTextInputValue("title");
Skyler Grey75ea9172022-08-06 10:22:23 +0100402 const description =
403 out.fields.getTextInputValue("description");
404 await interaction.channel.send({
405 embeds: [
406 new EmojiEmbed()
407 .setTitle(title)
408 .setDescription(description)
409 .setStatus("Success")
410 .setEmoji("CONTROL.BLOCKTICK")
411 ],
412 components: [
413 new MessageActionRow().addComponents([
414 new MessageButton()
415 .setLabel("Verify")
416 .setEmoji(
417 getEmojiByName("CONTROL.TICK", "id")
418 )
419 .setStyle("SUCCESS")
420 .setCustomId("verifybutton")
421 ])
422 ]
423 });
pineafan63fc5e22022-08-04 22:04:10 +0100424 break;
Skyler Grey75ea9172022-08-06 10:22:23 +0100425 } else {
426 continue;
427 }
pineafan41d93562022-07-30 22:10:15 +0100428 }
429 }
pineafan6702cef2022-06-13 17:52:37 +0100430 } else {
pineafan63fc5e22022-08-04 22:04:10 +0100431 i.deferUpdate();
pineafan41d93562022-07-30 22:10:15 +0100432 break;
pineafan6702cef2022-06-13 17:52:37 +0100433 }
434 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100435 await interaction.editReply({
436 embeds: [m.embeds[0]!.setFooter({ text: "Message closed" })],
437 components: []
438 });
pineafan63fc5e22022-08-04 22:04:10 +0100439};
pineafan4f164f32022-02-26 22:07:12 +0000440
Skyler Grey1a67e182022-08-04 23:05:44 +0100441const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100442 const member = interaction.member as Discord.GuildMember;
443 if (!member.permissions.has("MANAGE_GUILD"))
444 throw "You must have the *Manage Server* permission to use this command";
pineafan4f164f32022-02-26 22:07:12 +0000445 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100446};
pineafan4f164f32022-02-26 22:07:12 +0000447
448export { command };
449export { callback };
pineafan6702cef2022-06-13 17:52:37 +0100450export { check };