blob: 44406e94d43ad7111492409781adb36ea185584c [file] [log] [blame]
pineafan6702cef2022-06-13 17:52:37 +01001import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +01002import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
pineafan6702cef2022-06-13 17:52:37 +01003import 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;
pineafan4edb7762022-06-26 19:21:04 +010017 m = await interaction.reply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010018 .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 {
pineafan4edb7762022-06-26 19:21:04 +010027 return await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010028 .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) {
pineafan4edb7762022-06-26 19:21:04 +010036 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010037 .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 {
pineafan4edb7762022-06-26 19:21:04 +010052 await client.database.guilds.write(interaction.guild.id, {"verify.role": role.id, "verify.enabled": true});
pineafan6702cef2022-06-13 17:52:37 +010053 } catch (e) {
54 console.log(e)
pineafan4edb7762022-06-26 19:21:04 +010055 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010056 .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 {
pineafan4edb7762022-06-26 19:21:04 +010063 return interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010064 .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;
pineafan4edb7762022-06-26 19:21:04 +010072 let data = await client.database.guilds.read(interaction.guild.id);
pineafan6702cef2022-06-13 17:52:37 +010073 let role = data.verify.role;
74 while (true) {
pineafan4edb7762022-06-26 19:21:04 +010075 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +010076 .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;
pineafan4edb7762022-06-26 19:21:04 +010096 await client.database.guilds.write(interaction.guild.id, {}, ["verify.role", "verify.enabled"])
pineafan6702cef2022-06-13 17:52:37 +010097 role = undefined;
98 }
99 } else {
100 break
101 }
102 }
pineafan4edb7762022-06-26 19:21:04 +0100103 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan6702cef2022-06-13 17:52:37 +0100104 .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 };