blob: c440b75d72426c68b30a9a0e6cd4db68d7793731 [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,
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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";
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050013import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafanda6e5342022-07-03 10:03:16 +010014import client from "../../utils/client.js";
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050015import { getCommandMentionByName } from "../../utils/getCommandDataByName.js";
16import lodash from "lodash";
pineafan4f164f32022-02-26 22:07:12 +000017
18const command = (builder: SlashCommandSubcommandBuilder) =>
19 builder
pineafan63fc5e22022-08-04 22:04:10 +010020 .setName("verify")
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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 const selectMenu = new ActionRowBuilder<RoleSelectMenuBuilder>()
37 .addComponents(
38 new RoleSelectMenuBuilder()
39 .setCustomId("role")
40 .setPlaceholder("Select a role")
41 );
42
43 const buttons = new ActionRowBuilder<ButtonBuilder>()
44 .addComponents(
45 new ButtonBuilder()
46 .setCustomId("switch")
47 .setLabel(data.enabled ? "Enabled" : "Disabled")
48 .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger)
49 .setEmoji(getEmojiByName(data.enabled ? "CONTROL.TICK" : "CONTROL.CROSS", "id") as APIMessageComponentEmoji),
50 new ButtonBuilder()
51 .setCustomId("save")
52 .setLabel("Save")
53 .setStyle(ButtonStyle.Success)
54 .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji)
55 .setDisabled(lodash.isEqual(config.verify, data))
56 );
57
58 const embed = new EmojiEmbed()
59 .setTitle("Verify Role")
60 .setDescription(
61 `Select a role to be given to users after they run ${getCommandMentionByName("verify")}` +
62 `\n\nCurrent role: ${config.verify.role ? `<@&${config.verify.role}>` : "None"}`
63 )
64 .setStatus("Success")
65 .setEmoji("CHANNEL.TEXT.CREATE");
66
Skyler Grey75ea9172022-08-06 10:22:23 +010067 await interaction.editReply({
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050068 embeds: [embed],
69 components: [selectMenu, buttons]
Skyler Grey75ea9172022-08-06 10:22:23 +010070 });
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050071
72 let i;
pineafan6702cef2022-06-13 17:52:37 +010073 try {
PineaFan0d06edc2023-01-17 22:10:31 +000074 i = await m.awaitMessageComponent({
75 time: 300000,
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050076 filter: (i) => { return i.user.id === interaction.user.id }
PineaFan0d06edc2023-01-17 22:10:31 +000077 });
Skyler Grey75ea9172022-08-06 10:22:23 +010078 } catch (e) {
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050079 closed = true;
Skyler Greyad002172022-08-16 18:48:26 +010080 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +010081 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050082
83 await i.deferUpdate();
84
85 if(i.isButton()) {
86 switch (i.customId) {
87 case "save": {
88 client.database.guilds.write(interaction.guild.id, {"verify": data} )
89 config = await client.database.guilds.read(interaction.guild.id);
90 data = Object.assign({}, config.verify);
91 break
Skyler Grey75ea9172022-08-06 10:22:23 +010092 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050093 case "switch": {
94 data.enabled = !data.enabled;
95 break
pineafan41d93562022-07-30 22:10:15 +010096 }
97 }
pineafan6702cef2022-06-13 17:52:37 +010098 } else {
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050099 data.role = i.values[0]!;
pineafan6702cef2022-06-13 17:52:37 +0100100 }
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500101
102 } while (!closed);
103 await interaction.deleteReply()
pineafan63fc5e22022-08-04 22:04:10 +0100104};
pineafan4f164f32022-02-26 22:07:12 +0000105
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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 };