PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../utils/defaults.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | import Discord, { |
| 3 | CommandInteraction, |
| 4 | Message, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 5 | ActionRowBuilder, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 6 | ButtonBuilder, |
TheCodedProf | 59772f8 | 2023-01-18 22:17:16 -0500 | [diff] [blame] | 7 | ButtonStyle, |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 8 | RoleSelectMenuBuilder, |
| 9 | APIMessageComponentEmoji |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 10 | } from "discord.js"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 11 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 12 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 13 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 14 | import client from "../../utils/client.js"; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 15 | import { getCommandMentionByName } from "../../utils/getCommandDataByName.js"; |
| 16 | import lodash from "lodash"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 17 | |
| 18 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 19 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | .setName("verify") |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 21 | .setDescription("Manage the role given after a user runs /verify") |
| 22 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 23 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 24 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 25 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | const m = (await interaction.reply({ |
| 27 | embeds: LoadingEmbed, |
| 28 | ephemeral: true, |
| 29 | fetchReply: true |
| 30 | })) as Message; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 31 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 32 | 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 67 | await interaction.editReply({ |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 68 | embeds: [embed], |
| 69 | components: [selectMenu, buttons] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 70 | }); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 71 | |
| 72 | let i; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 73 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 74 | i = await m.awaitMessageComponent({ |
| 75 | time: 300000, |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 76 | filter: (i) => { return i.user.id === interaction.user.id } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 77 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 78 | } catch (e) { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 79 | closed = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 80 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 81 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 82 | |
| 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 Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 92 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 93 | case "switch": { |
| 94 | data.enabled = !data.enabled; |
| 95 | break |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 98 | } else { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 99 | data.role = i.values[0]!; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 100 | } |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 101 | |
| 102 | } while (!closed); |
| 103 | await interaction.deleteReply() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 104 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 105 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 106 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 107 | const member = interaction.member as Discord.GuildMember; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 108 | if (!member.permissions.has("ManageGuild")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 109 | return "You must have the *Manage Server* permission to use this command"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 110 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 111 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 112 | |
| 113 | export { command }; |
| 114 | export { callback }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 115 | export { check }; |