blob: 9c091a1e7d09d96054644b84b55441b2553543f4 [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) =>
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 {
TheCodedProf01cba762023-02-18 15:55:05 -050036 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")
TheCodedProf8a8dbdf2023-02-18 20:39:55 -050047 .setLabel(data.enabled ? "Enabled" : "Disabled")
48 .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger)
TheCodedProf01cba762023-02-18 15:55:05 -050049 .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)
TheCodedProf8a8dbdf2023-02-18 20:39:55 -050055 .setDisabled(lodash.isEqual(config.verify, data))
TheCodedProf01cba762023-02-18 15:55:05 -050056 );
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({
TheCodedProf01cba762023-02-18 15:55:05 -050068 embeds: [embed],
69 components: [selectMenu, buttons]
Skyler Grey75ea9172022-08-06 10:22:23 +010070 });
TheCodedProf01cba762023-02-18 15:55:05 -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,
TheCodedProf01cba762023-02-18 15:55:05 -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) {
TheCodedProf01cba762023-02-18 15:55:05 -050079 closed = true;
Skyler Greyad002172022-08-16 18:48:26 +010080 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +010081 }
TheCodedProf01cba762023-02-18 15:55:05 -050082
TheCodedProf267563a2023-01-21 17:00:57 -050083 await i.deferUpdate();
TheCodedProf01cba762023-02-18 15:55:05 -050084
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);
TheCodedProf8a8dbdf2023-02-18 20:39:55 -050090 data = Object.assign({}, config.verify);
Skyler Grey16ecb172023-03-05 07:30:32 +000091 await client.memory.forceUpdate(interaction.guild.id);
TheCodedProf01cba762023-02-18 15:55:05 -050092 break
93 }
94 case "switch": {
95 data.enabled = !data.enabled;
96 break
97 }
pineafan6702cef2022-06-13 17:52:37 +010098 }
99 } else {
TheCodedProf01cba762023-02-18 15:55:05 -0500100 data.role = i.values[0]!;
pineafan6702cef2022-06-13 17:52:37 +0100101 }
TheCodedProf01cba762023-02-18 15:55:05 -0500102
103 } while (!closed);
104 await interaction.deleteReply()
pineafan63fc5e22022-08-04 22:04:10 +0100105};
pineafan4f164f32022-02-26 22:07:12 +0000106
TheCodedProff86ba092023-01-27 17:10:07 -0500107const check = (interaction: CommandInteraction, _partial: boolean = false) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 const member = interaction.member as Discord.GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000109 if (!member.permissions.has("ManageGuild"))
PineaFan0d06edc2023-01-17 22:10:31 +0000110 return "You must have the *Manage Server* permission to use this command";
pineafan4f164f32022-02-26 22:07:12 +0000111 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100112};
pineafan4f164f32022-02-26 22:07:12 +0000113
114export { command };
115export { callback };
pineafan6702cef2022-06-13 17:52:37 +0100116export { check };