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