pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 1 | import Discord, { CommandInteraction, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 2 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 3 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
| 4 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 5 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 6 | import { WrappedCheck } from "jshaiku"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 7 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 11 | .setName("verify") |
| 12 | .setDescription("Manage the role given after typing /verify") |
| 13 | .addRoleOption(option => option.setName("role").setDescription("The role to give after verifying").setRequired(false)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 14 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 15 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 16 | let m; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 17 | m = await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 18 | .setTitle("Loading") |
| 19 | .setStatus("Danger") |
| 20 | .setEmoji("NUCLEUS.LOADING") |
| 21 | ], ephemeral: true, fetchReply: true}); |
| 22 | if (interaction.options.getRole("role")) { |
| 23 | let role |
| 24 | try { |
| 25 | role = interaction.options.getRole("role") |
| 26 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 27 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 28 | .setEmoji("GUILD.ROLES.DELETE") |
| 29 | .setTitle("Verify Role") |
| 30 | .setDescription("The role you provided is not a valid role") |
| 31 | .setStatus("Danger") |
| 32 | ]}) |
| 33 | } |
| 34 | role = role as Discord.Role |
| 35 | if (role.guild.id != interaction.guild.id) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 36 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 37 | .setTitle("Verify Role") |
| 38 | .setDescription(`You must choose a role in this server`) |
| 39 | .setStatus("Danger") |
| 40 | .setEmoji("GUILD.ROLES.DELETE") |
| 41 | ]}); |
| 42 | } |
| 43 | let confirmation = await new confirmationMessage(interaction) |
| 44 | .setEmoji("GUILD.ROLES.EDIT") |
| 45 | .setTitle("Verify Role") |
| 46 | .setDescription(`Are you sure you want to set the verify role to <@&${role.id}>?`) |
| 47 | .setColor("Warning") |
| 48 | .setInverted(true) |
| 49 | .send(true) |
| 50 | if (confirmation.success) { |
| 51 | try { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 52 | await client.database.guilds.write(interaction.guild.id, {"verify.role": role.id, "verify.enabled": true}); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 53 | const { log, NucleusColors, entry, renderUser, renderRole } = client.logger |
| 54 | try { |
| 55 | let data = { |
| 56 | meta:{ |
| 57 | type: 'verifyRoleChanged', |
| 58 | displayName: 'Ignored Groups Changed', |
| 59 | calculateType: 'nucleusSettingsUpdated', |
| 60 | color: NucleusColors.green, |
| 61 | emoji: "CONTROL.BLOCKTICK", |
| 62 | timestamp: new Date().getTime() |
| 63 | }, |
| 64 | list: { |
| 65 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 66 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 67 | role: entry(role.id, renderRole(role)), |
| 68 | }, |
| 69 | hidden: { |
| 70 | guild: interaction.guild.id |
| 71 | } |
| 72 | } |
| 73 | log(data); |
| 74 | } catch {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 75 | } catch (e) { |
| 76 | console.log(e) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 77 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 78 | .setTitle("Verify Role") |
| 79 | .setDescription(`Something went wrong while setting the verify role`) |
| 80 | .setStatus("Danger") |
| 81 | .setEmoji("GUILD.ROLES.DELETE") |
| 82 | ], components: []}); |
| 83 | } |
| 84 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 85 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 86 | .setTitle("Verify Role") |
| 87 | .setDescription(`No changes were made`) |
| 88 | .setStatus("Success") |
| 89 | .setEmoji("GUILD.ROLES.CREATE") |
| 90 | ], components: []}); |
| 91 | } |
| 92 | } |
| 93 | let clicks = 0; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 94 | let data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 95 | let role = data.verify.role; |
| 96 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 97 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 98 | .setTitle("Verify Role") |
| 99 | .setDescription(role ? `Your verify role is currently set to <@&${role}>` : `You have not set a verify role`) |
| 100 | .setStatus("Success") |
| 101 | .setEmoji("GUILD.ROLES.CREATE") |
| 102 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 103 | .setCustomId("clear") |
| 104 | .setLabel(clicks ? "Click again to confirm" : "Reset role") |
| 105 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
| 106 | .setStyle("DANGER") |
| 107 | .setDisabled(!role) |
| 108 | ])]}); |
| 109 | let i; |
| 110 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 111 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 112 | } catch(e) { break } |
| 113 | i.deferUpdate() |
| 114 | if (i.component.customId == "clear") { |
| 115 | clicks += 1; |
| 116 | if (clicks == 2) { |
| 117 | clicks = 0; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 118 | await client.database.guilds.write(interaction.guild.id, {}, ["verify.role", "verify.enabled"]) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 119 | role = undefined; |
| 120 | } |
| 121 | } else { |
| 122 | break |
| 123 | } |
| 124 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 125 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 126 | .setTitle("Verify Role") |
| 127 | .setDescription(role ? `Your verify role is currently set to <@&${role}}>` : `You have not set a verify role`) |
| 128 | .setStatus("Success") |
| 129 | .setEmoji("GUILD.ROLE.CREATE") |
| 130 | .setFooter({text: "Message closed"}) |
| 131 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 132 | .setCustomId("clear") |
| 133 | .setLabel("Clear") |
| 134 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 135 | .setStyle("SECONDARY") |
| 136 | .setDisabled(true) |
| 137 | ])]}); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 141 | let member = (interaction.member as Discord.GuildMember) |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 142 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the Manage Server permission to use this command" |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | |
| 146 | export { command }; |
| 147 | export { callback }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 148 | export { check }; |