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, |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -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"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -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"; |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 15 | import { getCommandMentionByName } from "../../utils/getCommandDataByName.js"; |
TheCodedProf | 8a8dbdf | 2023-02-18 20:39:55 -0500 | [diff] [blame] | 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") |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -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 | |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -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 { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 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") |
TheCodedProf | 8a8dbdf | 2023-02-18 20:39:55 -0500 | [diff] [blame] | 47 | .setLabel(data.enabled ? "Enabled" : "Disabled") |
| 48 | .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger) |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 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) |
TheCodedProf | 8a8dbdf | 2023-02-18 20:39:55 -0500 | [diff] [blame] | 55 | .setDisabled(lodash.isEqual(config.verify, data)) |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 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({ |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 68 | embeds: [embed], |
| 69 | components: [selectMenu, buttons] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 70 | }); |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -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, |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -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) { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -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 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 82 | |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 83 | await i.deferUpdate(); |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 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); |
TheCodedProf | 8a8dbdf | 2023-02-18 20:39:55 -0500 | [diff] [blame] | 90 | data = Object.assign({}, config.verify); |
Skyler Grey | 16ecb17 | 2023-03-05 07:30:32 +0000 | [diff] [blame^] | 91 | await client.memory.forceUpdate(interaction.guild.id); |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 92 | break |
| 93 | } |
| 94 | case "switch": { |
| 95 | data.enabled = !data.enabled; |
| 96 | break |
| 97 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 98 | } |
| 99 | } else { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 100 | data.role = i.values[0]!; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 101 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 102 | |
| 103 | } while (!closed); |
| 104 | await interaction.deleteReply() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 105 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 106 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 107 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 108 | const member = interaction.member as Discord.GuildMember; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 109 | if (!member.permissions.has("ManageGuild")) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 110 | return "You must have the *Manage Server* permission to use this command"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 111 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 112 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 113 | |
| 114 | export { command }; |
| 115 | export { callback }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 116 | export { check }; |