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