Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 1 | import { |
| 2 | ActionRowBuilder, |
| 3 | APIMessageComponentEmoji, |
| 4 | ButtonBuilder, |
| 5 | ButtonStyle, |
| 6 | ChannelSelectMenuBuilder, |
| 7 | ChannelType, |
| 8 | CommandInteraction, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 9 | ModalBuilder, |
| 10 | SlashCommandSubcommandBuilder, |
| 11 | StringSelectMenuBuilder, |
| 12 | StringSelectMenuOptionBuilder, |
| 13 | TextInputBuilder, |
| 14 | TextInputStyle |
| 15 | } from "discord.js"; |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 16 | import type Discord from "discord.js"; |
| 17 | import { LoadingEmbed } from "../../utils/defaults.js"; |
| 18 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 19 | import lodash from "lodash"; |
| 20 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
| 21 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 22 | import _ from "lodash"; |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 23 | |
| 24 | export const command = new SlashCommandSubcommandBuilder() |
| 25 | .setName("buttons") |
| 26 | .setDescription("Create clickable buttons for verifying, role menus etc."); |
| 27 | |
| 28 | interface Data { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 29 | buttons: string[]; |
| 30 | title: string | null; |
| 31 | description: string | null; |
| 32 | color: number; |
| 33 | channel: string | null; |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 36 | const colors: Record<string, number> = { |
| 37 | RED: 0xf27878, |
| 38 | ORANGE: 0xe5ab71, |
| 39 | YELLOW: 0xf2d478, |
| 40 | GREEN: 0x65cc76, |
| 41 | BLUE: 0x72aef5, |
| 42 | PURPLE: 0xa358b2, |
| 43 | PINK: 0xd46899, |
| 44 | GRAY: 0x999999 |
| 45 | }; |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 46 | |
| 47 | const buttonNames: Record<string, string> = { |
| 48 | verifybutton: "Verify", |
| 49 | rolemenu: "Role Menu", |
TheCodedProf | 9c51a7e | 2023-02-27 17:11:13 -0500 | [diff] [blame] | 50 | createticket: "Create Ticket" |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 51 | }; |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 52 | |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 53 | const presetButtons = [ |
| 54 | { |
| 55 | title: "Verify", |
| 56 | description: "Click the button below to get verified in the server.", |
| 57 | buttons: ["verifybutton"], |
| 58 | color: "GREEN" |
| 59 | }, |
| 60 | { |
| 61 | title: "Get Roles", |
| 62 | description: "Click the button to choose which roles you would like in the server", |
| 63 | buttons: ["rolemenu"], |
| 64 | color: "BLUE" |
| 65 | }, |
| 66 | { |
| 67 | title: "Create Ticket", |
| 68 | description: "Click the button below to create a ticket", |
| 69 | buttons: ["createticket"], |
| 70 | color: "RED" |
| 71 | } |
TheCodedProf | 1cfa1ae | 2023-03-11 16:07:37 -0500 | [diff] [blame] | 72 | ]; |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 73 | |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 74 | export const callback = async (interaction: CommandInteraction): Promise<void> => { |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 75 | const m = await interaction.reply({ |
| 76 | embeds: LoadingEmbed, |
| 77 | fetchReply: true, |
| 78 | ephemeral: true |
| 79 | }); |
| 80 | |
| 81 | let closed = false; |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 82 | let data: Data = { |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 83 | buttons: [], |
| 84 | title: null, |
| 85 | description: null, |
| 86 | color: colors["RED"]!, |
| 87 | channel: interaction.channelId |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 88 | }; |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 89 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 90 | const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents( |
TheCodedProf | 80ad854 | 2023-03-10 12:52:33 -0500 | [diff] [blame] | 91 | new ButtonBuilder() |
| 92 | .setCustomId("edit") |
| 93 | .setLabel("Edit Embed") |
| 94 | .setStyle(ButtonStyle.Secondary) |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 95 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 96 | new ButtonBuilder() |
| 97 | .setCustomId("send") |
| 98 | .setLabel("Send") |
| 99 | .setStyle(ButtonStyle.Primary) |
TheCodedProf | 35e1877 | 2023-04-23 16:17:24 -0400 | [diff] [blame^] | 100 | .setDisabled(!(data.channel && (data.title ?? data.description ?? data.buttons.length))) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 101 | ); |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 102 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 103 | const colorSelect = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 104 | new StringSelectMenuBuilder() |
| 105 | .setCustomId("color") |
| 106 | .setPlaceholder("Select a color") |
| 107 | .setMinValues(1) |
| 108 | .addOptions( |
| 109 | Object.keys(colors).map((color: string) => { |
| 110 | return new StringSelectMenuOptionBuilder() |
| 111 | .setLabel(lodash.capitalize(color)) |
| 112 | .setValue(color) |
| 113 | .setEmoji(getEmojiByName("COLORS." + color, "id") as APIMessageComponentEmoji) |
| 114 | .setDefault(data.color === colors[color]); |
| 115 | }) |
| 116 | ) |
| 117 | ); |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 118 | |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 119 | const presetSelect = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 120 | new StringSelectMenuBuilder() |
| 121 | .setCustomId("preset") |
| 122 | .setPlaceholder("Select a preset") |
| 123 | .setMaxValues(1) |
| 124 | .addOptions( |
| 125 | presetButtons.map((preset, i) => { |
| 126 | return new StringSelectMenuOptionBuilder() |
| 127 | .setLabel(preset.title) |
| 128 | .setValue(i.toString()) |
| 129 | .setDescription(preset.description) |
TheCodedProf | 1cfa1ae | 2023-03-11 16:07:37 -0500 | [diff] [blame] | 130 | .setEmoji(getEmojiByName("COLORS." + preset.color, "id") as APIMessageComponentEmoji); |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 131 | }) |
| 132 | ) |
TheCodedProf | 1cfa1ae | 2023-03-11 16:07:37 -0500 | [diff] [blame] | 133 | ); |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 134 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 135 | const buttonSelect = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 136 | new StringSelectMenuBuilder() |
| 137 | .setCustomId("button") |
| 138 | .setPlaceholder("Select buttons to add") |
TheCodedProf | 35e1877 | 2023-04-23 16:17:24 -0400 | [diff] [blame^] | 139 | .setMinValues(0) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 140 | .setMaxValues(3) |
| 141 | .addOptions( |
| 142 | new StringSelectMenuOptionBuilder() |
| 143 | .setLabel("Verify") |
| 144 | .setValue("verifybutton") |
| 145 | .setDescription("Click to get verified in the server") |
| 146 | .setDefault(data.buttons.includes("verifybutton")), |
| 147 | new StringSelectMenuOptionBuilder() |
| 148 | .setLabel("Role Menu") |
| 149 | .setValue("rolemenu") |
| 150 | .setDescription("Click to customize your roles") |
| 151 | .setDefault(data.buttons.includes("rolemenu")), |
| 152 | new StringSelectMenuOptionBuilder() |
| 153 | .setLabel("Ticket") |
| 154 | .setValue("createticket") |
| 155 | .setDescription("Click to create a support ticket") |
| 156 | .setDefault(data.buttons.includes("createticket")) |
| 157 | ) |
| 158 | ); |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 159 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 160 | const channelMenu = new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents( |
| 161 | new ChannelSelectMenuBuilder() |
| 162 | .setCustomId("channel") |
| 163 | .setPlaceholder("Select a channel") |
| 164 | .setChannelTypes( |
| 165 | ChannelType.GuildText, |
| 166 | ChannelType.GuildAnnouncement, |
| 167 | ChannelType.PublicThread, |
| 168 | ChannelType.AnnouncementThread |
| 169 | ) |
| 170 | ); |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 171 | let channelName = interaction.guild!.channels.cache.get(data.channel!)?.name; |
| 172 | if (data.channel === interaction.channelId) channelName = "this channel"; |
| 173 | const embed = new EmojiEmbed() |
| 174 | .setTitle(data.title ?? "No title set") |
| 175 | .setDescription(data.description ?? "*No description set*") |
| 176 | .setColor(data.color) |
TheCodedProf | 35e1877 | 2023-04-23 16:17:24 -0400 | [diff] [blame^] | 177 | .setFooter({ text: `The embed will be sent in ${channelName} | Click the button below to edit the embed` }); |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 178 | |
| 179 | await interaction.editReply({ |
| 180 | embeds: [embed], |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 181 | components: [presetSelect, colorSelect, buttonSelect, channelMenu, buttons] |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 182 | }); |
| 183 | |
| 184 | let i: Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction | Discord.StringSelectMenuInteraction; |
| 185 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 186 | i = (await interaction.channel!.awaitMessageComponent({ |
Skyler Grey | 21f5229 | 2023-03-10 17:58:30 +0000 | [diff] [blame] | 187 | filter: (i: Discord.Interaction) => |
| 188 | i.user.id === interaction.user.id && i.isMessageComponent() && i.message.id === m.id, |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 189 | time: 300000 |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 190 | })) as |
| 191 | | Discord.ButtonInteraction |
| 192 | | Discord.ChannelSelectMenuInteraction |
| 193 | | Discord.StringSelectMenuInteraction; |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 194 | } catch (e) { |
| 195 | closed = true; |
| 196 | break; |
| 197 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 198 | if (i.isButton()) { |
| 199 | switch (i.customId) { |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 200 | case "edit": { |
| 201 | await i.showModal( |
| 202 | new ModalBuilder() |
| 203 | .setCustomId("modal") |
| 204 | .setTitle(`Options for ${i.customId}`) |
| 205 | .addComponents( |
| 206 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 207 | new TextInputBuilder() |
| 208 | .setCustomId("title") |
| 209 | .setLabel("Title") |
| 210 | .setMaxLength(256) |
| 211 | .setRequired(false) |
| 212 | .setStyle(TextInputStyle.Short) |
| 213 | .setValue(data.title ?? "") |
| 214 | ), |
| 215 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 216 | new TextInputBuilder() |
| 217 | .setCustomId("description") |
| 218 | .setLabel("The text to display below the title") |
| 219 | .setMaxLength(4000) |
| 220 | .setRequired(false) |
| 221 | .setStyle(TextInputStyle.Paragraph) |
| 222 | .setValue(data.description ?? "") |
| 223 | ) |
| 224 | ) |
| 225 | ); |
| 226 | await interaction.editReply({ |
| 227 | embeds: [ |
| 228 | new EmojiEmbed() |
| 229 | .setTitle("Button Editor") |
| 230 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 231 | .setStatus("Success") |
| 232 | .setEmoji("GUILD.TICKET.OPEN") |
| 233 | ], |
| 234 | components: [ |
| 235 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
| 236 | new ButtonBuilder() |
| 237 | .setLabel("Back") |
| 238 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 239 | .setStyle(ButtonStyle.Primary) |
| 240 | .setCustomId("back") |
| 241 | ]) |
| 242 | ] |
| 243 | }); |
| 244 | let out: Discord.ModalSubmitInteraction | null; |
| 245 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 246 | out = (await modalInteractionCollector( |
| 247 | m, |
| 248 | interaction.user |
| 249 | )) as Discord.ModalSubmitInteraction | null; |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 250 | } catch (e) { |
| 251 | closed = true; |
| 252 | continue; |
| 253 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 254 | if (!out || out.isButton()) continue; |
| 255 | data.title = |
| 256 | out.fields.getTextInputValue("title").length === 0 |
| 257 | ? null |
| 258 | : out.fields.getTextInputValue("title"); |
| 259 | data.description = |
| 260 | out.fields.getTextInputValue("description").length === 0 |
| 261 | ? null |
| 262 | : out.fields.getTextInputValue("description"); |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | case "send": { |
| 266 | await i.deferUpdate(); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 267 | const channel = interaction.guild!.channels.cache.get(data.channel!) as Discord.TextChannel; |
TheCodedProf | 35e1877 | 2023-04-23 16:17:24 -0400 | [diff] [blame^] | 268 | let components: ActionRowBuilder<ButtonBuilder>[] = [] |
| 269 | let embeds: EmojiEmbed[] = []; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 270 | for (const button of data.buttons) { |
TheCodedProf | 35e1877 | 2023-04-23 16:17:24 -0400 | [diff] [blame^] | 271 | components = [ |
Skyler Grey | 21f5229 | 2023-03-10 17:58:30 +0000 | [diff] [blame] | 272 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 273 | new ButtonBuilder() |
| 274 | .setCustomId(button) |
| 275 | .setLabel(buttonNames[button]!) |
| 276 | .setStyle(ButtonStyle.Primary) |
| 277 | ) |
| 278 | ]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 279 | } |
TheCodedProf | 35e1877 | 2023-04-23 16:17:24 -0400 | [diff] [blame^] | 280 | if (data.title || data.description) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 281 | const e = new EmojiEmbed(); |
| 282 | if (data.title) e.setTitle(data.title); |
| 283 | if (data.description) e.setDescription(data.description); |
| 284 | if (data.color) e.setColor(data.color); |
TheCodedProf | 35e1877 | 2023-04-23 16:17:24 -0400 | [diff] [blame^] | 285 | embeds = [e] |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 286 | } |
TheCodedProf | 35e1877 | 2023-04-23 16:17:24 -0400 | [diff] [blame^] | 287 | await channel.send({ embeds, components }) |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 291 | } else if (i.isStringSelectMenu()) { |
| 292 | try { |
| 293 | await i.deferUpdate(); |
| 294 | } catch (err) { |
| 295 | console.log(err); |
| 296 | } |
| 297 | switch (i.customId) { |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 298 | case "preset": { |
| 299 | const chosen = presetButtons[parseInt(i.values[0]!)]!; |
TheCodedProf | 1cfa1ae | 2023-03-11 16:07:37 -0500 | [diff] [blame] | 300 | const newColor = colors[chosen.color!]!; |
TheCodedProf | 764e6c2 | 2023-03-11 16:07:09 -0500 | [diff] [blame] | 301 | data = _.assign(data, chosen, { color: newColor }); |
| 302 | break; |
| 303 | } |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 304 | case "color": { |
| 305 | data.color = colors[i.values[0]!]!; |
| 306 | break; |
| 307 | } |
| 308 | case "button": { |
| 309 | data.buttons = i.values; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | } else { |
| 314 | await i.deferUpdate(); |
| 315 | data.channel = i.values[0]!; |
| 316 | } |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 317 | } while (!closed); |
| 318 | await interaction.deleteReply(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 319 | }; |
TheCodedProf | 46518a4 | 2023-02-18 17:08:23 -0500 | [diff] [blame] | 320 | |
| 321 | export const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
| 322 | const member = interaction.member as Discord.GuildMember; |
| 323 | if (!member.permissions.has("ManageMessages")) |
| 324 | return "You must have the *Manage Messages* permission to use this command"; |
| 325 | return true; |
| 326 | }; |