blob: e2318fc3f92c181c9973be236a3e1ac82faa78da [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";
TheCodedProf8a8dbdf2023-02-18 20:39:55 -050016import lodash from "lodash";
pineafan4f164f32022-02-26 22:07:12 +000017
18const command = (builder: SlashCommandSubcommandBuilder) =>
Skyler Greyda16adf2023-03-05 10:22:12 +000019 builder.setName("verify").setDescription("Manage the role given after a user runs /verify");
pineafan4f164f32022-02-26 22:07:12 +000020
pineafan3a02ea32022-08-11 21:35:04 +010021const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFana00db1b2023-01-02 15:32:54 +000022 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +010023 const m = (await interaction.reply({
24 embeds: LoadingEmbed,
25 ephemeral: true,
26 fetchReply: true
27 })) as Message;
Skyler Greyad002172022-08-16 18:48:26 +010028
TheCodedProf01cba762023-02-18 15:55:05 -050029 let closed = false;
30 let config = await client.database.guilds.read(interaction.guild.id);
31 let data = Object.assign({}, config.verify);
32 do {
Skyler Greyda16adf2023-03-05 10:22:12 +000033 const selectMenu = new ActionRowBuilder<RoleSelectMenuBuilder>().addComponents(
34 new RoleSelectMenuBuilder().setCustomId("role").setPlaceholder("Select a role")
TheCodedProf01cba762023-02-18 15:55:05 -050035 );
36
Skyler Greyda16adf2023-03-05 10:22:12 +000037 const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
38 new ButtonBuilder()
39 .setCustomId("switch")
40 .setLabel(data.enabled ? "Enabled" : "Disabled")
41 .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger)
42 .setEmoji(
43 getEmojiByName(data.enabled ? "CONTROL.TICK" : "CONTROL.CROSS", "id") as APIMessageComponentEmoji
44 ),
45 new ButtonBuilder()
46 .setCustomId("save")
47 .setLabel("Save")
48 .setStyle(ButtonStyle.Success)
49 .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji)
50 .setDisabled(lodash.isEqual(config.verify, data))
51 );
TheCodedProf01cba762023-02-18 15:55:05 -050052
53 const embed = new EmojiEmbed()
54 .setTitle("Verify Role")
55 .setDescription(
56 `Select a role to be given to users after they run ${getCommandMentionByName("verify")}` +
Skyler Greyda16adf2023-03-05 10:22:12 +000057 `\n\nCurrent role: ${config.verify.role ? `<@&${config.verify.role}>` : "None"}`
TheCodedProf01cba762023-02-18 15:55:05 -050058 )
59 .setStatus("Success")
60 .setEmoji("CHANNEL.TEXT.CREATE");
61
Skyler Grey75ea9172022-08-06 10:22:23 +010062 await interaction.editReply({
TheCodedProf01cba762023-02-18 15:55:05 -050063 embeds: [embed],
64 components: [selectMenu, buttons]
Skyler Grey75ea9172022-08-06 10:22:23 +010065 });
TheCodedProf01cba762023-02-18 15:55:05 -050066
67 let i;
pineafan6702cef2022-06-13 17:52:37 +010068 try {
PineaFan0d06edc2023-01-17 22:10:31 +000069 i = await m.awaitMessageComponent({
70 time: 300000,
Skyler Greyda16adf2023-03-05 10:22:12 +000071 filter: (i) => {
72 return i.user.id === interaction.user.id;
73 }
PineaFan0d06edc2023-01-17 22:10:31 +000074 });
Skyler Grey75ea9172022-08-06 10:22:23 +010075 } catch (e) {
TheCodedProf01cba762023-02-18 15:55:05 -050076 closed = true;
Skyler Greyad002172022-08-16 18:48:26 +010077 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +010078 }
TheCodedProf01cba762023-02-18 15:55:05 -050079
TheCodedProf267563a2023-01-21 17:00:57 -050080 await i.deferUpdate();
TheCodedProf01cba762023-02-18 15:55:05 -050081
Skyler Greyda16adf2023-03-05 10:22:12 +000082 if (i.isButton()) {
TheCodedProf01cba762023-02-18 15:55:05 -050083 switch (i.customId) {
84 case "save": {
Skyler Greyda16adf2023-03-05 10:22:12 +000085 client.database.guilds.write(interaction.guild.id, { verify: data });
TheCodedProf01cba762023-02-18 15:55:05 -050086 config = await client.database.guilds.read(interaction.guild.id);
TheCodedProf8a8dbdf2023-02-18 20:39:55 -050087 data = Object.assign({}, config.verify);
Skyler Grey16ecb172023-03-05 07:30:32 +000088 await client.memory.forceUpdate(interaction.guild.id);
Skyler Greyda16adf2023-03-05 10:22:12 +000089 break;
TheCodedProf01cba762023-02-18 15:55:05 -050090 }
91 case "switch": {
92 data.enabled = !data.enabled;
Skyler Greyda16adf2023-03-05 10:22:12 +000093 break;
TheCodedProf01cba762023-02-18 15:55:05 -050094 }
pineafan6702cef2022-06-13 17:52:37 +010095 }
96 } else {
TheCodedProf01cba762023-02-18 15:55:05 -050097 data.role = i.values[0]!;
pineafan6702cef2022-06-13 17:52:37 +010098 }
TheCodedProf01cba762023-02-18 15:55:05 -050099 } while (!closed);
Skyler Greyda16adf2023-03-05 10:22:12 +0000100 await interaction.deleteReply();
pineafan63fc5e22022-08-04 22:04:10 +0100101};
pineafan4f164f32022-02-26 22:07:12 +0000102
TheCodedProff86ba092023-01-27 17:10:07 -0500103const check = (interaction: CommandInteraction, _partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100104 const member = interaction.member as Discord.GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000105 if (!member.permissions.has("ManageGuild"))
PineaFan0d06edc2023-01-17 22:10:31 +0000106 return "You must have the *Manage Server* permission to use this command";
pineafan4f164f32022-02-26 22:07:12 +0000107 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100108};
pineafan4f164f32022-02-26 22:07:12 +0000109
110export { command };
111export { callback };
pineafan6702cef2022-06-13 17:52:37 +0100112export { check };