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) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame^] | 50 | if (confirmation.cancelled) return |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 51 | if (confirmation.success) { |
| 52 | try { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 53 | 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] | 54 | const { log, NucleusColors, entry, renderUser, renderRole } = client.logger |
| 55 | try { |
| 56 | let data = { |
| 57 | meta:{ |
| 58 | type: 'verifyRoleChanged', |
| 59 | displayName: 'Ignored Groups Changed', |
| 60 | calculateType: 'nucleusSettingsUpdated', |
| 61 | color: NucleusColors.green, |
| 62 | emoji: "CONTROL.BLOCKTICK", |
| 63 | timestamp: new Date().getTime() |
| 64 | }, |
| 65 | list: { |
| 66 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 67 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 68 | role: entry(role.id, renderRole(role)), |
| 69 | }, |
| 70 | hidden: { |
| 71 | guild: interaction.guild.id |
| 72 | } |
| 73 | } |
| 74 | log(data); |
| 75 | } catch {} |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 76 | } catch (e) { |
| 77 | console.log(e) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 78 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 79 | .setTitle("Verify Role") |
| 80 | .setDescription(`Something went wrong while setting the verify role`) |
| 81 | .setStatus("Danger") |
| 82 | .setEmoji("GUILD.ROLES.DELETE") |
| 83 | ], components: []}); |
| 84 | } |
| 85 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 86 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 87 | .setTitle("Verify Role") |
| 88 | .setDescription(`No changes were made`) |
| 89 | .setStatus("Success") |
| 90 | .setEmoji("GUILD.ROLES.CREATE") |
| 91 | ], components: []}); |
| 92 | } |
| 93 | } |
| 94 | let clicks = 0; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 95 | let data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 96 | let role = data.verify.role; |
| 97 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 98 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 99 | .setTitle("Verify Role") |
| 100 | .setDescription(role ? `Your verify role is currently set to <@&${role}>` : `You have not set a verify role`) |
| 101 | .setStatus("Success") |
| 102 | .setEmoji("GUILD.ROLES.CREATE") |
| 103 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 104 | .setCustomId("clear") |
| 105 | .setLabel(clicks ? "Click again to confirm" : "Reset role") |
| 106 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
| 107 | .setStyle("DANGER") |
| 108 | .setDisabled(!role) |
| 109 | ])]}); |
| 110 | let i; |
| 111 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 112 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 113 | } catch(e) { break } |
| 114 | i.deferUpdate() |
| 115 | if (i.component.customId == "clear") { |
| 116 | clicks += 1; |
| 117 | if (clicks == 2) { |
| 118 | clicks = 0; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 119 | await client.database.guilds.write(interaction.guild.id, {}, ["verify.role", "verify.enabled"]) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 120 | role = undefined; |
| 121 | } |
| 122 | } else { |
| 123 | break |
| 124 | } |
| 125 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 126 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 127 | .setTitle("Verify Role") |
| 128 | .setDescription(role ? `Your verify role is currently set to <@&${role}}>` : `You have not set a verify role`) |
| 129 | .setStatus("Success") |
| 130 | .setEmoji("GUILD.ROLE.CREATE") |
| 131 | .setFooter({text: "Message closed"}) |
| 132 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 133 | .setCustomId("clear") |
| 134 | .setLabel("Clear") |
| 135 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 136 | .setStyle("SECONDARY") |
| 137 | .setDisabled(true) |
| 138 | ])]}); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 142 | let member = (interaction.member as Discord.GuildMember) |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 143 | 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] | 144 | return true; |
| 145 | } |
| 146 | |
| 147 | export { command }; |
| 148 | export { callback }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 149 | export { check }; |