TheCodedProf | afca98b | 2023-01-17 22:25:43 -0500 | [diff] [blame] | 1 | import type Discord from "discord.js"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 2 | import { |
| 3 | ActionRowBuilder, |
| 4 | APIMessageComponentEmoji, |
| 5 | ButtonBuilder, |
| 6 | ButtonInteraction, |
| 7 | ButtonStyle, |
| 8 | CommandInteraction, |
| 9 | Message, |
| 10 | ModalBuilder, |
| 11 | RoleSelectMenuBuilder, |
| 12 | RoleSelectMenuInteraction, |
| 13 | StringSelectMenuBuilder, |
| 14 | StringSelectMenuInteraction, |
| 15 | StringSelectMenuOptionBuilder, |
| 16 | TextInputBuilder, |
| 17 | TextInputStyle |
| 18 | } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 19 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 20 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 21 | import { LoadingEmbed } from "../../utils/defaults.js"; |
| 22 | import client from "../../utils/client.js"; |
| 23 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
| 24 | import createPageIndicator from "../../utils/createPageIndicator.js"; |
| 25 | import { configToDropdown } from "../../actions/roleMenu.js"; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 26 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
TheCodedProf | b5e9d55 | 2023-01-29 15:43:26 -0500 | [diff] [blame] | 27 | import ellipsis from "../../utils/ellipsis.js"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 28 | import lodash from "lodash"; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 29 | |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 30 | const isEqual = lodash.isEqual; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 31 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 32 | const command = (builder: SlashCommandSubcommandBuilder) => builder.setName("rolemenu").setDescription("rolemenu"); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 33 | |
| 34 | interface ObjectSchema { |
| 35 | name: string; |
| 36 | description: string; |
| 37 | min: number; |
| 38 | max: number; |
| 39 | options: { |
| 40 | name: string; |
| 41 | description: string | null; |
| 42 | role: string; |
| 43 | }[]; |
| 44 | } |
| 45 | |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 46 | const defaultRolePageConfig = { |
| 47 | name: "Role Menu Page", |
| 48 | description: "A new role menu page", |
| 49 | min: 0, |
| 50 | max: 0, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 51 | options: [{ name: "Role 1", description: null, role: "No role set" }] |
| 52 | }; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 53 | |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 54 | const reorderRoleMenuPages = async (interaction: CommandInteraction, m: Message, currentObj: ObjectSchema[]) => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 55 | const reorderRow = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 56 | new StringSelectMenuBuilder() |
| 57 | .setCustomId("reorder") |
| 58 | .setPlaceholder("Select all pages in the order you want them to appear.") |
| 59 | .setMinValues(currentObj.length) |
| 60 | .setMaxValues(currentObj.length) |
| 61 | .addOptions( |
| 62 | currentObj.map((o, i) => new StringSelectMenuOptionBuilder().setLabel(o.name).setValue(i.toString())) |
| 63 | ) |
| 64 | ); |
| 65 | const buttonRow = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 66 | new ButtonBuilder() |
| 67 | .setCustomId("back") |
| 68 | .setLabel("Back") |
| 69 | .setStyle(ButtonStyle.Secondary) |
| 70 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 71 | ); |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 72 | await interaction.editReply({ |
| 73 | embeds: [ |
| 74 | new EmojiEmbed() |
| 75 | .setTitle("Role Menu") |
| 76 | .setDescription("Select pages in the order you want them to appear.") |
| 77 | .setStatus("Success") |
| 78 | ], |
| 79 | components: [reorderRow, buttonRow] |
| 80 | }); |
| 81 | let out: StringSelectMenuInteraction | ButtonInteraction | null; |
| 82 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 83 | out = (await m.awaitMessageComponent({ |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 84 | filter: (i) => i.channel!.id === interaction.channel!.id, |
| 85 | time: 300000 |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 86 | })) as StringSelectMenuInteraction | ButtonInteraction | null; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 87 | } catch (e) { |
| 88 | console.error(e); |
| 89 | out = null; |
| 90 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 91 | if (!out) return; |
TheCodedProf | b5e9d55 | 2023-01-29 15:43:26 -0500 | [diff] [blame] | 92 | out.deferUpdate(); |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 93 | if (out.isButton()) return; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 94 | const values = out.values; |
| 95 | |
| 96 | const newOrder: ObjectSchema[] = currentObj.map((_, i) => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 97 | const index = values.findIndex((v) => v === i.toString()); |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 98 | return currentObj[index]; |
| 99 | }) as ObjectSchema[]; |
| 100 | |
| 101 | return newOrder; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 102 | }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 103 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 104 | const editNameDescription = async ( |
| 105 | i: ButtonInteraction, |
| 106 | interaction: StringSelectMenuInteraction | ButtonInteraction, |
| 107 | m: Message, |
| 108 | data: { name?: string; description?: string } |
| 109 | ) => { |
| 110 | let { name, description } = data; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 111 | const modal = new ModalBuilder() |
| 112 | .setTitle("Edit Name and Description") |
| 113 | .setCustomId("editNameDescription") |
| 114 | .addComponents( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 115 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 116 | new TextInputBuilder() |
| 117 | .setLabel("Name") |
| 118 | .setCustomId("name") |
| 119 | .setPlaceholder("The name of the role (e.g. Programmer)") |
| 120 | .setStyle(TextInputStyle.Short) |
| 121 | .setValue(name ?? "") |
| 122 | .setRequired(true) |
| 123 | ), |
| 124 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 125 | new TextInputBuilder() |
| 126 | .setLabel("Description") |
| 127 | .setCustomId("description") |
| 128 | .setPlaceholder("A short description of the role (e.g. A role for people who code)") |
| 129 | .setStyle(TextInputStyle.Short) |
| 130 | .setValue(description ?? "") |
| 131 | ) |
| 132 | ); |
| 133 | const button = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 134 | new ButtonBuilder() |
| 135 | .setCustomId("back") |
| 136 | .setLabel("Back") |
| 137 | .setStyle(ButtonStyle.Secondary) |
| 138 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 139 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 140 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 141 | await i.showModal(modal); |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 142 | await interaction.editReply({ |
| 143 | embeds: [ |
| 144 | new EmojiEmbed() |
| 145 | .setTitle("Role Menu") |
| 146 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 147 | .setStatus("Success") |
| 148 | ], |
| 149 | components: [button] |
| 150 | }); |
| 151 | |
| 152 | let out: Discord.ModalSubmitInteraction | null; |
| 153 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 154 | out = (await modalInteractionCollector(m, interaction.user)) as Discord.ModalSubmitInteraction | null; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 155 | } catch (e) { |
| 156 | console.error(e); |
| 157 | out = null; |
| 158 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 159 | if (!out) return [name, description]; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 160 | if (out.isButton()) return [name, description]; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 161 | name = out.fields.fields.find((f) => f.customId === "name")?.value ?? name; |
| 162 | description = out.fields.fields.find((f) => f.customId === "description")?.value ?? description; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 163 | return [name, description]; |
| 164 | }; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 165 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 166 | const editRoleMenuPage = async ( |
| 167 | interaction: StringSelectMenuInteraction | ButtonInteraction, |
| 168 | m: Message, |
| 169 | data?: ObjectSchema |
| 170 | ): Promise<ObjectSchema | null> => { |
| 171 | if (!data) |
| 172 | data = { |
| 173 | name: "Role Menu Page", |
| 174 | description: "A new role menu page", |
| 175 | min: 0, |
| 176 | max: 0, |
| 177 | options: [] |
| 178 | }; |
| 179 | const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 180 | new ButtonBuilder() |
| 181 | .setCustomId("back") |
| 182 | .setLabel("Back") |
| 183 | .setStyle(ButtonStyle.Secondary) |
| 184 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji), |
| 185 | new ButtonBuilder() |
| 186 | .setCustomId("edit") |
| 187 | .setLabel("Edit") |
| 188 | .setStyle(ButtonStyle.Primary) |
| 189 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 190 | new ButtonBuilder() |
| 191 | .setCustomId("addRole") |
| 192 | .setLabel("Add Role") |
| 193 | .setStyle(ButtonStyle.Secondary) |
| 194 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id") as APIMessageComponentEmoji) |
| 195 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 196 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 197 | let back = false; |
| 198 | if (data.options.length === 0) { |
| 199 | data.options = [{ name: "Role 1", description: null, role: "No role set" }]; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 200 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 201 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 202 | const previewSelect = configToDropdown("Edit Roles", { |
| 203 | name: data.name, |
| 204 | description: data.description, |
| 205 | min: 1, |
| 206 | max: 1, |
| 207 | options: data.options |
| 208 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 209 | const embed = new EmojiEmbed() |
| 210 | .setTitle(`${data.name}`) |
| 211 | .setStatus("Success") |
| 212 | .setDescription( |
| 213 | `**Description:**\n> ${data.description}\n\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 214 | `**Min:** ${data.min}` + |
| 215 | (data.min === 0 ? " (Members will be given a skip button)" : "") + |
| 216 | "\n" + |
| 217 | `**Max:** ${data.max}\n` |
| 218 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 219 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 220 | interaction.editReply({ embeds: [embed], components: [previewSelect, buttons] }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 221 | let i: StringSelectMenuInteraction | ButtonInteraction; |
| 222 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 223 | i = (await m.awaitMessageComponent({ |
| 224 | time: 300000, |
| 225 | filter: (i) => |
| 226 | i.user.id === interaction.user.id && i.message.id === m.id && i.channelId === interaction.channelId |
| 227 | })) as ButtonInteraction | StringSelectMenuInteraction; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 228 | } catch (e) { |
| 229 | back = true; |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | if (i.isStringSelectMenu()) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 234 | if (i.customId === "roles") { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 235 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 236 | await createRoleMenuOptionPage( |
| 237 | interaction, |
| 238 | m, |
| 239 | data.options.find((o) => o.role === (i as StringSelectMenuInteraction).values[0]) |
| 240 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 241 | } |
| 242 | } else if (i.isButton()) { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 243 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 244 | case "back": { |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 245 | await i.deferUpdate(); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 246 | back = true; |
| 247 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 248 | } |
| 249 | case "edit": { |
| 250 | const [name, description] = await editNameDescription(i, interaction, m, data); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 251 | data.name = name ? name : data.name; |
| 252 | data.description = description ? description : data.description; |
| 253 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 254 | } |
| 255 | case "addRole": { |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 256 | await i.deferUpdate(); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 257 | data.options.push(await createRoleMenuOptionPage(interaction, m)); |
| 258 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 259 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 260 | } |
| 261 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 262 | } while (!back); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 263 | if (isEqual(data, defaultRolePageConfig)) return null; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 264 | return data; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 265 | }; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 266 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 267 | const createRoleMenuOptionPage = async ( |
| 268 | interaction: StringSelectMenuInteraction | ButtonInteraction, |
| 269 | m: Message, |
| 270 | data?: { name: string; description: string | null; role: string } |
| 271 | ) => { |
| 272 | const { renderRole } = client.logger; |
| 273 | if (!data) |
| 274 | data = { |
| 275 | name: "New role Menu Option", |
| 276 | description: null, |
| 277 | role: "" |
| 278 | }; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 279 | let back = false; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 280 | const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 281 | new ButtonBuilder() |
| 282 | .setCustomId("back") |
| 283 | .setLabel("Back") |
| 284 | .setStyle(ButtonStyle.Secondary) |
| 285 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji), |
| 286 | new ButtonBuilder() |
| 287 | .setCustomId("edit") |
| 288 | .setLabel("Edit Details") |
| 289 | .setStyle(ButtonStyle.Primary) |
| 290 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji) |
| 291 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 292 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 293 | const roleSelect = new RoleSelectMenuBuilder() |
| 294 | .setCustomId("role") |
| 295 | .setPlaceholder(data.role ? "Set role to" : "Set the role"); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 296 | const embed = new EmojiEmbed() |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 297 | .setTitle(`${data.name}`) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 298 | .setStatus("Success") |
| 299 | .setDescription( |
| 300 | `**Description:**\n> ${data.description ?? "No description set"}\n\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 301 | `**Role:** ${ |
| 302 | data.role ? renderRole((await interaction.guild!.roles.fetch(data.role))!) : "No role set" |
| 303 | }\n` |
| 304 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 305 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 306 | interaction.editReply({ |
| 307 | embeds: [embed], |
| 308 | components: [new ActionRowBuilder<RoleSelectMenuBuilder>().addComponents(roleSelect), buttons] |
| 309 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 310 | |
| 311 | let i: RoleSelectMenuInteraction | ButtonInteraction; |
| 312 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 313 | i = (await m.awaitMessageComponent({ |
| 314 | time: 300000, |
| 315 | filter: (i) => |
| 316 | i.user.id === interaction.user.id && i.message.id === m.id && i.channelId === interaction.channelId |
| 317 | })) as ButtonInteraction | RoleSelectMenuInteraction; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 318 | } catch (e) { |
| 319 | back = true; |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | if (i.isRoleSelectMenu()) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 324 | if (i.customId === "role") { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 325 | await i.deferUpdate(); |
| 326 | data.role = (i as RoleSelectMenuInteraction).values[0]!; |
| 327 | } |
| 328 | } else if (i.isButton()) { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 329 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 330 | case "back": { |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 331 | await i.deferUpdate(); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 332 | back = true; |
| 333 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 334 | } |
| 335 | case "edit": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 336 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 337 | const [name, description] = await editNameDescription( |
| 338 | i, |
| 339 | interaction, |
| 340 | m, |
| 341 | data as { name: string; description: string } |
| 342 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 343 | data.name = name ? name : data.name; |
| 344 | data.description = description ? description : data.description; |
| 345 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 346 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | } while (!back); |
| 350 | return data; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 351 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 352 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 353 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 354 | if (!interaction.guild) return; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 355 | const m = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 356 | |
| 357 | let page = 0; |
| 358 | let closed = false; |
| 359 | const config = await client.database.guilds.read(interaction.guild.id); |
| 360 | let currentObject: ObjectSchema[] = config.roleMenu.options; |
| 361 | let modified = false; |
| 362 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 363 | const embed = new EmojiEmbed().setTitle("Role Menu").setEmoji("GUILD.GREEN").setStatus("Success"); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 364 | const noRoleMenus = currentObject.length === 0; |
| 365 | let current: ObjectSchema; |
| 366 | |
| 367 | const pageSelect = new StringSelectMenuBuilder() |
| 368 | .setCustomId("page") |
| 369 | .setPlaceholder("Select a Role Menu page to manage"); |
| 370 | const actionSelect = new StringSelectMenuBuilder() |
| 371 | .setCustomId("action") |
| 372 | .setPlaceholder("Perform an action") |
| 373 | .addOptions( |
| 374 | new StringSelectMenuOptionBuilder() |
| 375 | .setLabel("Edit") |
| 376 | .setDescription("Edit this page") |
| 377 | .setValue("edit") |
| 378 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 379 | new StringSelectMenuOptionBuilder() |
| 380 | .setLabel("Delete") |
| 381 | .setDescription("Delete this page") |
| 382 | .setValue("delete") |
| 383 | .setEmoji(getEmojiByName("TICKETS.ISSUE", "id") as APIMessageComponentEmoji) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 384 | ); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 385 | const buttonRow = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 386 | new ButtonBuilder() |
| 387 | .setCustomId("back") |
| 388 | .setStyle(ButtonStyle.Primary) |
| 389 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 390 | .setDisabled(page === 0), |
| 391 | new ButtonBuilder() |
| 392 | .setCustomId("next") |
| 393 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id") as APIMessageComponentEmoji) |
| 394 | .setStyle(ButtonStyle.Primary) |
| 395 | .setDisabled(page === Object.keys(currentObject).length - 1), |
| 396 | new ButtonBuilder() |
| 397 | .setCustomId("add") |
| 398 | .setLabel("New Page") |
| 399 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id") as APIMessageComponentEmoji) |
| 400 | .setStyle(ButtonStyle.Secondary) |
| 401 | .setDisabled(Object.keys(currentObject).length >= 24), |
| 402 | new ButtonBuilder() |
| 403 | .setCustomId("reorder") |
| 404 | .setLabel("Reorder Pages") |
| 405 | .setEmoji(getEmojiByName("ICONS.REORDER", "id") as APIMessageComponentEmoji) |
| 406 | .setStyle(ButtonStyle.Secondary) |
| 407 | .setDisabled(Object.keys(currentObject).length <= 1), |
| 408 | new ButtonBuilder() |
| 409 | .setCustomId("save") |
| 410 | .setLabel("Save") |
| 411 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji) |
| 412 | .setStyle(ButtonStyle.Success) |
| 413 | .setDisabled(!modified) |
| 414 | ); |
| 415 | if (noRoleMenus) { |
| 416 | embed.setDescription( |
| 417 | "No role menu pages have been set up yet. Use the button below to add one.\n\n" + |
| 418 | createPageIndicator(1, 1, undefined, true) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 419 | ); |
| 420 | pageSelect.setDisabled(true); |
| 421 | actionSelect.setDisabled(true); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 422 | pageSelect.addOptions(new StringSelectMenuOptionBuilder().setLabel("No role menu pages").setValue("none")); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 423 | } else { |
| 424 | page = Math.min(page, Object.keys(currentObject).length - 1); |
| 425 | current = currentObject[page]!; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 426 | embed.setDescription( |
| 427 | `**Currently Editing:** ${current.name}\n\n` + |
| 428 | `**Description:**\n> ${current.description}\n` + |
| 429 | `\n\n${createPageIndicator(Object.keys(config.roleMenu.options).length, page)}` |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 430 | ); |
| 431 | |
| 432 | pageSelect.addOptions( |
| 433 | currentObject.map((key: ObjectSchema, index) => { |
| 434 | return new StringSelectMenuOptionBuilder() |
| 435 | .setLabel(ellipsis(key.name, 50)) |
| 436 | .setDescription(ellipsis(key.description, 50)) |
| 437 | .setValue(index.toString()); |
| 438 | }) |
| 439 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 440 | } |
| 441 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 442 | await interaction.editReply({ |
| 443 | embeds: [embed], |
| 444 | components: [ |
| 445 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(actionSelect), |
| 446 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(pageSelect), |
| 447 | buttonRow |
| 448 | ] |
| 449 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 450 | let i: StringSelectMenuInteraction | ButtonInteraction; |
| 451 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 452 | i = (await m.awaitMessageComponent({ |
| 453 | time: 300000, |
| 454 | filter: (i) => |
| 455 | i.user.id === interaction.user.id && i.message.id === m.id && i.channelId === interaction.channelId |
| 456 | })) as ButtonInteraction | StringSelectMenuInteraction; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 457 | } catch (e) { |
| 458 | closed = true; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 459 | continue; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | await i.deferUpdate(); |
| 463 | if (i.isButton()) { |
| 464 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 465 | case "back": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 466 | page--; |
| 467 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 468 | } |
| 469 | case "next": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 470 | page++; |
| 471 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 472 | } |
| 473 | case "add": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 474 | const newPage = await editRoleMenuPage(i, m); |
| 475 | if (!newPage) break; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 476 | currentObject.push(); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 477 | page = currentObject.length - 1; |
| 478 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 479 | } |
| 480 | case "reorder": { |
| 481 | const reordered = await reorderRoleMenuPages(interaction, m, currentObject); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 482 | if (!reordered) break; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 483 | currentObject = reordered; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 484 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 485 | } |
| 486 | case "save": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 487 | await client.database.guilds.write(interaction.guild.id, { "roleMenu.options": currentObject }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 488 | modified = false; |
Skyler Grey | 16ecb17 | 2023-03-05 07:30:32 +0000 | [diff] [blame] | 489 | await client.memory.forceUpdate(interaction.guild.id); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 490 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 491 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 492 | } |
| 493 | } else if (i.isStringSelectMenu()) { |
| 494 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 495 | case "action": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 496 | switch (i.values[0]) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 497 | case "edit": { |
| 498 | const edited = await editRoleMenuPage(i, m, current!); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 499 | if (!edited) break; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 500 | currentObject[page] = edited; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 501 | modified = true; |
| 502 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 503 | } |
| 504 | case "delete": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 505 | if (page === 0 && currentObject.keys.length - 1 > 0) page++; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 506 | else page--; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 507 | currentObject.splice(page, 1); |
| 508 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 509 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 510 | } |
| 511 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 512 | } |
| 513 | case "page": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 514 | page = parseInt(i.values[0]!); |
| 515 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 516 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 517 | } |
| 518 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 519 | } while (!closed); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 520 | await interaction.deleteReply(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 521 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 522 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 523 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 524 | const member = interaction.member as Discord.GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 525 | if (!member.permissions.has("ManageRoles")) |
| 526 | return "You must have the *Manage Roles* permission to use this command"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 527 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 528 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 529 | |
| 530 | export { command }; |
| 531 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 532 | export { check }; |