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