blob: 920beb48bf1cd815eb898a1544a2a425bf9f96bc [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LoadingEmbed } from "../../utils/defaults.js";
Skyler Grey75ea9172022-08-06 10:22:23 +01002import Discord, {
3 CommandInteraction,
4 Message,
TheCodedProf21c08592022-09-13 14:14:43 -04005 ActionRowBuilder,
TheCodedProf21c08592022-09-13 14:14:43 -04006 ButtonBuilder,
TheCodedProf59772f82023-01-18 22:17:16 -05007 ButtonStyle,
TheCodedProf01cba762023-02-18 15:55:05 -05008 RoleSelectMenuBuilder,
9 APIMessageComponentEmoji
Skyler Grey75ea9172022-08-06 10:22:23 +010010} from "discord.js";
pineafanda6e5342022-07-03 10:03:16 +010011import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafanda6e5342022-07-03 10:03:16 +010012import getEmojiByName from "../../utils/getEmojiByName.js";
TheCodedProff86ba092023-01-27 17:10:07 -050013import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafanda6e5342022-07-03 10:03:16 +010014import client from "../../utils/client.js";
TheCodedProf01cba762023-02-18 15:55:05 -050015import { getCommandMentionByName } from "../../utils/getCommandDataByName.js";
16
pineafan4f164f32022-02-26 22:07:12 +000017
18const command = (builder: SlashCommandSubcommandBuilder) =>
19 builder
pineafan63fc5e22022-08-04 22:04:10 +010020 .setName("verify")
TheCodedProf01cba762023-02-18 15:55:05 -050021 .setDescription("Manage the role given after a user runs /verify")
22
pineafan4f164f32022-02-26 22:07:12 +000023
pineafan3a02ea32022-08-11 21:35:04 +010024const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFana00db1b2023-01-02 15:32:54 +000025 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010026 const m = (await interaction.reply({
27 embeds: LoadingEmbed,
28 ephemeral: true,
29 fetchReply: true
30 })) as Message;
Skyler Greyad002172022-08-16 18:48:26 +010031
TheCodedProf01cba762023-02-18 15:55:05 -050032 let closed = false;
33 let config = await client.database.guilds.read(interaction.guild.id);
34 let data = Object.assign({}, config.verify);
35 do {
36 console.log(config.verify, data)
37 const selectMenu = new ActionRowBuilder<RoleSelectMenuBuilder>()
38 .addComponents(
39 new RoleSelectMenuBuilder()
40 .setCustomId("role")
41 .setPlaceholder("Select a role")
42 );
43
44 const buttons = new ActionRowBuilder<ButtonBuilder>()
45 .addComponents(
46 new ButtonBuilder()
47 .setCustomId("switch")
48 .setLabel(data.enabled ? "Disabled" : "Enabled")
49 .setStyle(data.enabled ? ButtonStyle.Danger : ButtonStyle.Success)
50 .setEmoji(getEmojiByName(data.enabled ? "CONTROL.TICK" : "CONTROL.CROSS", "id") as APIMessageComponentEmoji),
51 new ButtonBuilder()
52 .setCustomId("save")
53 .setLabel("Save")
54 .setStyle(ButtonStyle.Success)
55 .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji)
56 .setDisabled(data.role === config.verify.role && data.enabled === config.verify.enabled)
57 );
58
59 const embed = new EmojiEmbed()
60 .setTitle("Verify Role")
61 .setDescription(
62 `Select a role to be given to users after they run ${getCommandMentionByName("verify")}` +
63 `\n\nCurrent role: ${config.verify.role ? `<@&${config.verify.role}>` : "None"}`
64 )
65 .setStatus("Success")
66 .setEmoji("CHANNEL.TEXT.CREATE");
67
Skyler Grey75ea9172022-08-06 10:22:23 +010068 await interaction.editReply({
TheCodedProf01cba762023-02-18 15:55:05 -050069 embeds: [embed],
70 components: [selectMenu, buttons]
Skyler Grey75ea9172022-08-06 10:22:23 +010071 });
TheCodedProf01cba762023-02-18 15:55:05 -050072
73 let i;
pineafan6702cef2022-06-13 17:52:37 +010074 try {
PineaFan0d06edc2023-01-17 22:10:31 +000075 i = await m.awaitMessageComponent({
76 time: 300000,
TheCodedProf01cba762023-02-18 15:55:05 -050077 filter: (i) => { return i.user.id === interaction.user.id }
PineaFan0d06edc2023-01-17 22:10:31 +000078 });
Skyler Grey75ea9172022-08-06 10:22:23 +010079 } catch (e) {
TheCodedProf01cba762023-02-18 15:55:05 -050080 closed = true;
Skyler Greyad002172022-08-16 18:48:26 +010081 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +010082 }
TheCodedProf01cba762023-02-18 15:55:05 -050083
TheCodedProf267563a2023-01-21 17:00:57 -050084 await i.deferUpdate();
TheCodedProf01cba762023-02-18 15:55:05 -050085
86 if(i.isButton()) {
87 switch (i.customId) {
88 case "save": {
89 client.database.guilds.write(interaction.guild.id, {"verify": data} )
90 config = await client.database.guilds.read(interaction.guild.id);
91 break
92 }
93 case "switch": {
94 data.enabled = !data.enabled;
95 break
96 }
pineafan6702cef2022-06-13 17:52:37 +010097 }
98 } else {
TheCodedProf01cba762023-02-18 15:55:05 -050099 data.role = i.values[0]!;
pineafan6702cef2022-06-13 17:52:37 +0100100 }
TheCodedProf01cba762023-02-18 15:55:05 -0500101
102 } while (!closed);
103 await interaction.deleteReply()
pineafan63fc5e22022-08-04 22:04:10 +0100104};
pineafan4f164f32022-02-26 22:07:12 +0000105
TheCodedProff86ba092023-01-27 17:10:07 -0500106const check = (interaction: CommandInteraction, _partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 const member = interaction.member as Discord.GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000108 if (!member.permissions.has("ManageGuild"))
PineaFan0d06edc2023-01-17 22:10:31 +0000109 return "You must have the *Manage Server* permission to use this command";
pineafan4f164f32022-02-26 22:07:12 +0000110 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100111};
pineafan4f164f32022-02-26 22:07:12 +0000112
113export { command };
114export { callback };
pineafan6702cef2022-06-13 17:52:37 +0100115export { check };