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