pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 2 | import Discord, { CommandInteraction, Message, MessageActionRow, MessageActionRowComponent, MessageButton, MessageComponentInteraction, MessageEmbed, MessageSelectMenu, Role, SelectMenuInteraction, TextInputComponent } 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 | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 6 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 7 | import client from "../../utils/client.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 8 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +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 | |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 16 | const callback = async (interaction: CommandInteraction): Promise<void | unknown> => { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 17 | const m = await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}) as Message; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 18 | if (interaction.options.getRole("role")) { |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 19 | let role: Role; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 20 | try { |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 21 | role = interaction.options.getRole("role") as Role; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 22 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 23 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 24 | .setEmoji("GUILD.ROLES.DELETE") |
| 25 | .setTitle("Verify Role") |
| 26 | .setDescription("The role you provided is not a valid role") |
| 27 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | ]}); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 29 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | role = role as Discord.Role; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 31 | if (role.guild.id !== interaction.guild.id) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 32 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 33 | .setTitle("Verify Role") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 34 | .setDescription("You must choose a role in this server") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 35 | .setStatus("Danger") |
| 36 | .setEmoji("GUILD.ROLES.DELETE") |
| 37 | ]}); |
| 38 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 40 | .setEmoji("GUILD.ROLES.EDIT") |
| 41 | .setTitle("Verify Role") |
| 42 | .setDescription(`Are you sure you want to set the verify role to <@&${role.id}>?`) |
| 43 | .setColor("Warning") |
| 44 | .setInverted(true) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 45 | .send(true); |
| 46 | if (confirmation.cancelled) return; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 47 | if (confirmation.success) { |
| 48 | try { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 49 | await client.database.guilds.write(interaction.guild.id, {"verify.role": role.id, "verify.enabled": true}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | const { log, NucleusColors, entry, renderUser, renderRole } = client.logger; |
| 51 | const data = { |
| 52 | meta:{ |
| 53 | type: "verifyRoleChanged", |
| 54 | displayName: "Verify Role Changed", |
| 55 | calculateType: "nucleusSettingsUpdated", |
| 56 | color: NucleusColors.green, |
| 57 | emoji: "CONTROL.BLOCKTICK", |
| 58 | timestamp: new Date().getTime() |
| 59 | }, |
| 60 | list: { |
| 61 | memberId: entry(interaction.user.id, `\`${interaction.user.id}\``), |
| 62 | changedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 63 | role: entry(role.id, renderRole(role)) |
| 64 | }, |
| 65 | hidden: { |
| 66 | guild: interaction.guild.id |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 67 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | }; |
| 69 | log(data); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 70 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 71 | console.log(e); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 72 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 73 | .setTitle("Verify Role") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 74 | .setDescription("Something went wrong while setting the verify role") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 75 | .setStatus("Danger") |
| 76 | .setEmoji("GUILD.ROLES.DELETE") |
| 77 | ], components: []}); |
| 78 | } |
| 79 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 80 | return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 81 | .setTitle("Verify Role") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 82 | .setDescription("No changes were made") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 83 | .setStatus("Success") |
| 84 | .setEmoji("GUILD.ROLES.CREATE") |
| 85 | ], components: []}); |
| 86 | } |
| 87 | } |
| 88 | let clicks = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 89 | const data = await client.database.guilds.read(interaction.guild.id); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 90 | let role = data.verify.role; |
| 91 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 92 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 93 | .setTitle("Verify Role") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 94 | .setDescription(role ? `Your verify role is currently set to <@&${role}>` : "You have not set a verify role") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 95 | .setStatus("Success") |
| 96 | .setEmoji("GUILD.ROLES.CREATE") |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 97 | ], components: [new MessageActionRow().addComponents([ |
| 98 | new MessageButton() |
| 99 | .setCustomId("clear") |
| 100 | .setLabel(clicks ? "Click again to confirm" : "Reset role") |
| 101 | .setEmoji(getEmojiByName(clicks ? "TICKETS.ISSUE" : "CONTROL.CROSS", "id")) |
| 102 | .setStyle("DANGER") |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 103 | .setDisabled(!role), |
| 104 | new MessageButton() |
| 105 | .setCustomId("send") |
| 106 | .setLabel("Add verify button") |
| 107 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id")) |
| 108 | .setStyle("PRIMARY") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 109 | ])]}); |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 110 | let i: MessageComponentInteraction; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 111 | try { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 112 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 113 | } catch(e) { break; } |
| 114 | i.deferUpdate(); |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 115 | if ((i.component as MessageActionRowComponent).customId === "clear") { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 116 | clicks += 1; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 117 | if (clicks === 2) { |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 118 | clicks = 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 119 | await client.database.guilds.write(interaction.guild.id, null, ["verify.role", "verify.enabled"]); |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 120 | role = undefined; |
| 121 | } |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 122 | } else if ((i.component as MessageActionRowComponent).customId === "send") { |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 123 | const verifyMessages = [ |
| 124 | {label: "Verify", description: "Click the button below to get verified"}, |
| 125 | {label: "Get verified", description: "To get access to the rest of the server, click the button below"}, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | {label: "Ready to verify?", description: "Click the button below to verify yourself"} |
| 127 | ]; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 128 | while (true) { |
| 129 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 130 | .setTitle("Verify Button") |
| 131 | .setDescription("Select a message template to send in this channel") |
| 132 | .setFooter({text: role ? "" : "You do no have a verify role set so the button will not work."}) |
| 133 | .setStatus(role ? "Success" : "Warning") |
| 134 | .setEmoji("GUILD.ROLES.CREATE") |
| 135 | ], components: [ |
| 136 | new MessageActionRow().addComponents([ |
| 137 | new MessageSelectMenu().setOptions(verifyMessages.map((t: {label: string, description: string, value?: string}, index) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 138 | t.value = index.toString(); return t as {value: string, label: string, description: string}; |
| 139 | })).setCustomId("template").setMaxValues(1).setMinValues(1).setPlaceholder("Select a message template") |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 140 | ]), |
| 141 | new MessageActionRow().addComponents([ |
| 142 | new MessageButton() |
| 143 | .setCustomId("back") |
| 144 | .setLabel("Back") |
| 145 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 146 | .setStyle("DANGER"), |
| 147 | new MessageButton() |
| 148 | .setCustomId("blank") |
| 149 | .setLabel("Empty") |
| 150 | .setStyle("SECONDARY"), |
| 151 | new MessageButton() |
| 152 | .setCustomId("custom") |
| 153 | .setLabel("Custom") |
| 154 | .setEmoji(getEmojiByName("TICKETS.OTHER", "id")) |
| 155 | .setStyle("PRIMARY") |
| 156 | ]) |
| 157 | ]}); |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 158 | let i: MessageComponentInteraction; |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 159 | try { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 160 | i = await m.awaitMessageComponent({time: 300000}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 161 | } catch(e) { break; } |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 162 | if ((i.component as MessageActionRowComponent).customId === "template") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 163 | i.deferUpdate(); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 164 | await interaction.channel.send({embeds: [new EmojiEmbed() |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 165 | .setTitle(verifyMessages[parseInt((i as SelectMenuInteraction).values[0])].label) |
| 166 | .setDescription(verifyMessages[parseInt((i as SelectMenuInteraction).values[0])].description) |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 167 | .setStatus("Success") |
| 168 | .setEmoji("CONTROL.BLOCKTICK") |
| 169 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 170 | .setLabel("Verify") |
| 171 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")) |
| 172 | .setStyle("SUCCESS") |
| 173 | .setCustomId("verifybutton") |
| 174 | ])]}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 175 | break; |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 176 | } else if ((i.component as MessageActionRowComponent).customId === "blank") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 177 | i.deferUpdate(); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 178 | await interaction.channel.send({components: [new MessageActionRow().addComponents([new MessageButton() |
| 179 | .setLabel("Verify") |
| 180 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")) |
| 181 | .setStyle("SUCCESS") |
| 182 | .setCustomId("verifybutton") |
| 183 | ])]}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 184 | break; |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 185 | } else if ((i.component as MessageActionRowComponent).customId === "custom") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 186 | await i.showModal(new Discord.Modal().setCustomId("modal").setTitle("Enter embed details").addComponents( |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 187 | new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent() |
| 188 | .setCustomId("title") |
| 189 | .setLabel("Title") |
| 190 | .setMaxLength(256) |
| 191 | .setRequired(true) |
| 192 | .setStyle("SHORT") |
| 193 | ), |
| 194 | new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent() |
| 195 | .setCustomId("description") |
| 196 | .setLabel("Description") |
| 197 | .setMaxLength(4000) |
| 198 | .setRequired(true) |
| 199 | .setStyle("PARAGRAPH") |
| 200 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 201 | )); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 202 | await interaction.editReply({ |
| 203 | embeds: [new EmojiEmbed() |
| 204 | .setTitle("Verify Button") |
| 205 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 206 | .setStatus("Success") |
| 207 | .setEmoji("GUILD.TICKET.OPEN") |
| 208 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 209 | .setLabel("Back") |
| 210 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 211 | .setStyle("PRIMARY") |
| 212 | .setCustomId("back") |
| 213 | ])] |
| 214 | }); |
| 215 | let out; |
| 216 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 217 | out = await modalInteractionCollector(m, (m) => m.channel.id === interaction.channel.id, (m) => m.customId === "modify"); |
| 218 | } catch (e) { break; } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 219 | if (out.fields) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 220 | const title = out.fields.getTextInputValue("title"); |
| 221 | const description = out.fields.getTextInputValue("description"); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 222 | await interaction.channel.send({embeds: [new EmojiEmbed() |
| 223 | .setTitle(title) |
| 224 | .setDescription(description) |
| 225 | .setStatus("Success") |
| 226 | .setEmoji("CONTROL.BLOCKTICK") |
| 227 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 228 | .setLabel("Verify") |
| 229 | .setEmoji(getEmojiByName("CONTROL.TICK", "id")) |
| 230 | .setStyle("SUCCESS") |
| 231 | .setCustomId("verifybutton") |
| 232 | ])]}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 233 | break; |
| 234 | } else { continue; } |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 235 | } |
| 236 | } |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 237 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 238 | i.deferUpdate(); |
pineafan | 41d9356 | 2022-07-30 22:10:15 +0100 | [diff] [blame] | 239 | break; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 240 | } |
| 241 | } |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 242 | await interaction.editReply({embeds: [(m.embeds[0] as MessageEmbed).setFooter({text: "Message closed"})], components: []}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 243 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 244 | |
Skyler Grey | 1a67e18 | 2022-08-04 23:05:44 +0100 | [diff] [blame] | 245 | const check = (interaction: CommandInteraction) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 246 | const member = (interaction.member as Discord.GuildMember); |
| 247 | 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] | 248 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 249 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 250 | |
| 251 | export { command }; |
| 252 | export { callback }; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 253 | export { check }; |