blob: 68b4bf9a476be1de6bfd453d1f21cdcd5fcfc548 [file] [log] [blame]
pineafan6702cef2022-06-13 17:52:37 +01001import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
2import generateEmojiEmbed from "../../../utils/generateEmojiEmbed.js";
3import confirmationMessage from "../../../utils/confirmationMessage.js";
4import getEmojiByName from "../../../utils/getEmojiByName.js";
pineafan4f164f32022-02-26 22:07:12 +00005import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
6import { WrappedCheck } from "jshaiku";
pineafan6702cef2022-06-13 17:52:37 +01007import client from "../../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
11 .setName("role")
pineafan6702cef2022-06-13 17:52:37 +010012 .setDescription("Sets or shows the role given to users after using /verify")
13 .addRoleOption(option => option.setName("role").setDescription("The role to give after verifying"))
pineafan4f164f32022-02-26 22:07:12 +000014
pineafan6702cef2022-06-13 17:52:37 +010015const callback = async (interaction: CommandInteraction): Promise<any> => {
16 let m;
17 m = await interaction.reply({embeds: [new generateEmojiEmbed()
18 .setTitle("Loading")
19 .setStatus("Danger")
20 .setEmoji("NUCLEUS.LOADING")
21 ], ephemeral: true, fetchReply: true});
22 if (interaction.options.getRole("role")) {
23 let role
24 try {
25 role = interaction.options.getRole("role")
26 } catch {
27 return await interaction.editReply({embeds: [new generateEmojiEmbed()
28 .setEmoji("GUILD.ROLES.DELETE")
29 .setTitle("Verify Role")
30 .setDescription("The role you provided is not a valid role")
31 .setStatus("Danger")
32 ]})
33 }
34 role = role as Discord.Role
35 if (role.guild.id != interaction.guild.id) {
36 return interaction.editReply({embeds: [new generateEmojiEmbed()
37 .setTitle("Verify Role")
38 .setDescription(`You must choose a role in this server`)
39 .setStatus("Danger")
40 .setEmoji("GUILD.ROLES.DELETE")
41 ]});
42 }
43 let confirmation = await new confirmationMessage(interaction)
44 .setEmoji("GUILD.ROLES.EDIT")
45 .setTitle("Verify Role")
46 .setDescription(`Are you sure you want to set the verify role to <@&${role.id}>?`)
47 .setColor("Warning")
48 .setInverted(true)
49 .send(true)
50 if (confirmation.success) {
51 try {
52 await client.database.write(interaction.guild.id, {"verify.role": role.id, "verify.enabled": true});
53 } catch (e) {
54 console.log(e)
55 return interaction.editReply({embeds: [new generateEmojiEmbed()
56 .setTitle("Verify Role")
57 .setDescription(`Something went wrong while setting the verify role`)
58 .setStatus("Danger")
59 .setEmoji("GUILD.ROLES.DELETE")
60 ], components: []});
61 }
62 } else {
63 return interaction.editReply({embeds: [new generateEmojiEmbed()
64 .setTitle("Verify Role")
65 .setDescription(`No changes were made`)
66 .setStatus("Success")
67 .setEmoji("GUILD.ROLES.CREATE")
68 ], components: []});
69 }
70 }
71 let clicks = 0;
72 let data = await client.database.read(interaction.guild.id);
73 let role = data.verify.role;
74 while (true) {
75 await interaction.editReply({embeds: [new generateEmojiEmbed()
76 .setTitle("Verify Role")
77 .setDescription(role ? `Your verify role is currently set to <@&${role}>` : `You have not set a verify role`)
78 .setStatus("Success")
79 .setEmoji("GUILD.ROLES.CREATE")
80 ], components: [new MessageActionRow().addComponents([new MessageButton()
81 .setCustomId("clear")
82 .setLabel(clicks ? "Click again to confirm" : "Reset role")
83 .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id"))
84 .setStyle("DANGER")
85 .setDisabled(!role)
86 ])]});
87 let i;
88 try {
pineafanc6158ab2022-06-17 16:34:07 +010089 i = await m.awaitMessageComponent({time: 300000});
pineafan6702cef2022-06-13 17:52:37 +010090 } catch(e) { break }
91 i.deferUpdate()
92 if (i.component.customId == "clear") {
93 clicks += 1;
94 if (clicks == 2) {
95 clicks = 0;
96 await client.database.write(interaction.guild.id, {}, ["verify.role", "verify.enabled"])
97 role = undefined;
98 }
99 } else {
100 break
101 }
102 }
103 await interaction.editReply({embeds: [new generateEmojiEmbed()
104 .setTitle("Verify Role")
105 .setDescription(role ? `Your verify role is currently set to <@&${role}}>` : `You have not set a verify role`)
106 .setStatus("Success")
107 .setEmoji("GUILD.ROLE.CREATE")
108 .setFooter({text: "Message closed"})
109 ], components: [new MessageActionRow().addComponents([new MessageButton()
110 .setCustomId("clear")
111 .setLabel("Clear")
112 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
113 .setStyle("SECONDARY")
114 .setDisabled(true)
115 ])]});
pineafan4f164f32022-02-26 22:07:12 +0000116}
117
118const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan6702cef2022-06-13 17:52:37 +0100119 let member = (interaction.member as Discord.GuildMember)
120 if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the `manage_server` permission to use this command"
pineafan4f164f32022-02-26 22:07:12 +0000121 return true;
122}
123
124export { command };
125export { callback };
pineafan6702cef2022-06-13 17:52:37 +0100126export { check };