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 | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 30 | const cross = getEmojiByName("CONTROL.CROSS"); |
| 31 | const tick = getEmojiByName("CONTROL.TICK"); |
TheCodedProf | e92b9b5 | 2023-03-06 17:07:34 -0500 | [diff] [blame] | 32 | const isEqual = _.isEqual; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 33 | |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 34 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 35 | builder.setName("rolemenu").setDescription("Allows you to change settings for the servers rolemenu"); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 36 | |
| 37 | interface ObjectSchema { |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 38 | enabled?: boolean; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 39 | name: string; |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 40 | description?: string; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 41 | min: number; |
| 42 | max: number; |
| 43 | options: { |
| 44 | name: string; |
| 45 | description: string | null; |
| 46 | role: string; |
| 47 | }[]; |
| 48 | } |
| 49 | |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 50 | const defaultRolePageConfig = { |
| 51 | name: "Role Menu Page", |
| 52 | description: "A new role menu page", |
| 53 | min: 0, |
| 54 | max: 0, |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 55 | options: [{ name: "Role 1", description: null, role: "No role set" }] |
| 56 | }; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 57 | |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 58 | const reorderRoleMenuPages = async (interaction: CommandInteraction, m: Message, currentObj: ObjectSchema[]) => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 59 | const reorderRow = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 60 | new StringSelectMenuBuilder() |
| 61 | .setCustomId("reorder") |
| 62 | .setPlaceholder("Select all pages in the order you want them to appear.") |
| 63 | .setMinValues(currentObj.length) |
| 64 | .setMaxValues(currentObj.length) |
| 65 | .addOptions( |
| 66 | currentObj.map((o, i) => new StringSelectMenuOptionBuilder().setLabel(o.name).setValue(i.toString())) |
| 67 | ) |
| 68 | ); |
| 69 | const buttonRow = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 70 | new ButtonBuilder() |
| 71 | .setCustomId("back") |
| 72 | .setLabel("Back") |
| 73 | .setStyle(ButtonStyle.Secondary) |
| 74 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 75 | ); |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 76 | await interaction.editReply({ |
| 77 | embeds: [ |
| 78 | new EmojiEmbed() |
| 79 | .setTitle("Role Menu") |
| 80 | .setDescription("Select pages in the order you want them to appear.") |
| 81 | .setStatus("Success") |
| 82 | ], |
| 83 | components: [reorderRow, buttonRow] |
| 84 | }); |
| 85 | let out: StringSelectMenuInteraction | ButtonInteraction | null; |
| 86 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 87 | out = (await m.awaitMessageComponent({ |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 88 | filter: (i) => i.channel!.id === interaction.channel!.id, |
| 89 | time: 300000 |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 90 | })) as StringSelectMenuInteraction | ButtonInteraction | null; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 91 | } catch (e) { |
| 92 | console.error(e); |
| 93 | out = null; |
| 94 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 95 | if (!out) return; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 96 | await out.deferUpdate(); |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 97 | if (out.isButton()) return; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 98 | const values = out.values; |
| 99 | |
| 100 | const newOrder: ObjectSchema[] = currentObj.map((_, i) => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 101 | const index = values.findIndex((v) => v === i.toString()); |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 102 | return currentObj[index]; |
| 103 | }) as ObjectSchema[]; |
| 104 | |
| 105 | return newOrder; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 106 | }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 107 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 108 | const editNameDescription = async ( |
| 109 | i: ButtonInteraction, |
| 110 | interaction: StringSelectMenuInteraction | ButtonInteraction, |
| 111 | m: Message, |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 112 | data: { name?: string; description?: string; bounds?: { min: number; max: number } }, |
| 113 | addBounds: boolean = false |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 114 | ): Promise<[string | undefined, string | undefined, { min: number; max: number } | undefined]> => { |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 115 | let { name, description, bounds } = data; |
| 116 | const components = [ |
| 117 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 118 | new TextInputBuilder() |
| 119 | .setLabel("Name") |
| 120 | .setCustomId("name") |
| 121 | .setPlaceholder("The name of the role (e.g. Programmer)") |
| 122 | .setStyle(TextInputStyle.Short) |
| 123 | .setValue(name ?? "") |
| 124 | .setRequired(true) |
| 125 | .setMaxLength(100) |
| 126 | ), |
| 127 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 128 | new TextInputBuilder() |
| 129 | .setLabel("Description") |
| 130 | .setCustomId("description") |
| 131 | .setPlaceholder("A short description of the role (e.g. A role for people who code)") |
| 132 | .setStyle(TextInputStyle.Short) |
| 133 | .setValue(description ?? "") |
| 134 | .setRequired(false) |
| 135 | .setMaxLength(100) |
| 136 | ) |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 137 | ]; |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 138 | if (addBounds && bounds) { |
| 139 | components.push( |
| 140 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 141 | new TextInputBuilder() |
| 142 | .setLabel("Minimum") |
| 143 | .setCustomId("min") |
| 144 | .setPlaceholder("The minimum number of roles a user can select") |
| 145 | .setStyle(TextInputStyle.Short) |
| 146 | .setValue(bounds.min.toString()) |
| 147 | .setRequired(true) |
| 148 | .setMaxLength(2) |
| 149 | ), |
| 150 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 151 | new TextInputBuilder() |
| 152 | .setLabel("Maximum") |
| 153 | .setCustomId("max") |
| 154 | .setPlaceholder("The maximum number of roles a user can select (0 for no limit)") |
| 155 | .setStyle(TextInputStyle.Short) |
| 156 | .setValue(bounds.max.toString()) |
| 157 | .setRequired(true) |
| 158 | .setMaxLength(2) |
| 159 | ) |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 160 | ); |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 161 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 162 | const modal = new ModalBuilder() |
| 163 | .setTitle("Edit Name and Description") |
| 164 | .setCustomId("editNameDescription") |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 165 | .addComponents(...components); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 166 | const button = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 167 | new ButtonBuilder() |
| 168 | .setCustomId("back") |
| 169 | .setLabel("Back") |
| 170 | .setStyle(ButtonStyle.Secondary) |
| 171 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 172 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 173 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 174 | await i.showModal(modal); |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 175 | await interaction.editReply({ |
| 176 | embeds: [ |
| 177 | new EmojiEmbed() |
| 178 | .setTitle("Role Menu") |
| 179 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 180 | .setStatus("Success") |
| 181 | ], |
| 182 | components: [button] |
| 183 | }); |
| 184 | |
| 185 | let out: Discord.ModalSubmitInteraction | null; |
| 186 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 187 | out = (await modalInteractionCollector(m, interaction.user)) as Discord.ModalSubmitInteraction | null; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 188 | } catch (e) { |
| 189 | console.error(e); |
| 190 | out = null; |
| 191 | } |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 192 | if (!out) return [name, description, bounds]; |
| 193 | if (out.isButton()) return [name, description, bounds]; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 194 | name = out.fields.fields.find((f) => f.customId === "name")?.value ?? name; |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 195 | description = out.fields.fields.find((f) => f.customId === "description")?.value; |
| 196 | if (addBounds) { |
| 197 | const min = parseInt(out.fields.fields.find((f) => f.customId === "min")?.value!); |
| 198 | const max = parseInt(out.fields.fields.find((f) => f.customId === "max")?.value!); |
| 199 | bounds = { min, max }; |
| 200 | } |
| 201 | return [name, description, bounds]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 202 | }; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 203 | |
TheCodedProf | 7a83f76 | 2023-03-06 17:17:00 -0500 | [diff] [blame] | 204 | const defaultRoleMenuData = { |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 205 | enabled: true, |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 206 | name: "New Page", |
| 207 | description: "", |
TheCodedProf | 7a83f76 | 2023-03-06 17:17:00 -0500 | [diff] [blame] | 208 | min: 0, |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 209 | max: 1, |
TheCodedProf | 7a83f76 | 2023-03-06 17:17:00 -0500 | [diff] [blame] | 210 | options: [] |
| 211 | }; |
| 212 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 213 | const editRoleMenuPage = async ( |
| 214 | interaction: StringSelectMenuInteraction | ButtonInteraction, |
| 215 | m: Message, |
| 216 | data?: ObjectSchema |
| 217 | ): Promise<ObjectSchema | null> => { |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 218 | if (!data) data = _.cloneDeep(defaultRoleMenuData); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 219 | const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 220 | new ButtonBuilder() |
| 221 | .setCustomId("back") |
| 222 | .setLabel("Back") |
| 223 | .setStyle(ButtonStyle.Secondary) |
| 224 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji), |
| 225 | new ButtonBuilder() |
| 226 | .setCustomId("edit") |
| 227 | .setLabel("Edit") |
| 228 | .setStyle(ButtonStyle.Primary) |
| 229 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 230 | new ButtonBuilder() |
| 231 | .setCustomId("addRole") |
| 232 | .setLabel("Add Role") |
| 233 | .setStyle(ButtonStyle.Secondary) |
| 234 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id") as APIMessageComponentEmoji) |
| 235 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 236 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 237 | let back = false; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 238 | do { |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 239 | const noRoles = data.options.length === 0; |
| 240 | const previewSelect = configToDropdown( |
| 241 | "Edit Roles", |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 242 | data.description |
| 243 | ? { |
| 244 | name: data.name, |
| 245 | description: data.description, |
| 246 | min: 1, |
| 247 | max: 1, |
| 248 | options: noRoles ? [{ name: "Role 1", description: null, role: "No role set" }] : data.options |
| 249 | } |
| 250 | : { |
| 251 | name: data.name, |
| 252 | min: 1, |
| 253 | max: 1, |
| 254 | options: noRoles ? [{ name: "Role 1", description: null, role: "No role set" }] : data.options |
| 255 | }, |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 256 | undefined, |
| 257 | noRoles |
| 258 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 259 | const embed = new EmojiEmbed() |
| 260 | .setTitle(`${data.name}`) |
| 261 | .setStatus("Success") |
| 262 | .setDescription( |
TheCodedProf | fc8d6ba | 2023-06-05 11:16:35 -0400 | [diff] [blame] | 263 | `**Description:**\n> ${data.description ?? "*No description set*"}\n\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 264 | `**Min:** ${data.min}` + |
| 265 | (data.min === 0 ? " (Members will be given a skip button)" : "") + |
| 266 | "\n" + |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 267 | `**Max:** ${data.max}\n` + |
| 268 | `\n**Roles:** ${data.options.length === 0 ? "*No roles set*" : data.options.length}` |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 269 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 270 | |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 271 | await interaction.editReply({ embeds: [embed], components: [previewSelect, buttons] }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 272 | let i: StringSelectMenuInteraction | ButtonInteraction; |
| 273 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 274 | i = (await m.awaitMessageComponent({ |
| 275 | time: 300000, |
| 276 | filter: (i) => |
| 277 | i.user.id === interaction.user.id && i.message.id === m.id && i.channelId === interaction.channelId |
| 278 | })) as ButtonInteraction | StringSelectMenuInteraction; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 279 | } catch (e) { |
| 280 | back = true; |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | if (i.isStringSelectMenu()) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 285 | if (i.customId === "roles") { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 286 | await i.deferUpdate(); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 287 | await createRoleMenuOptionPage( |
| 288 | interaction, |
| 289 | m, |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 290 | data.options.find((o) => o.role === (i as StringSelectMenuInteraction).values[0]), |
| 291 | false |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 292 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 293 | } |
| 294 | } else if (i.isButton()) { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 295 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 296 | case "back": { |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 297 | await i.deferUpdate(); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 298 | back = true; |
| 299 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 300 | } |
| 301 | case "edit": { |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 302 | const [name, description, _bounds] = await editNameDescription(i, interaction, m, data); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 303 | data.name = name ? name : data.name; |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 304 | description ? (data.description = description) : null; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 305 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 306 | } |
| 307 | case "addRole": { |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 308 | await i.deferUpdate(); |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 309 | const out = await createRoleMenuOptionPage(interaction, m, undefined, true); |
| 310 | if (out) data.options.push(out); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 311 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 312 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 313 | } |
| 314 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 315 | } while (!back); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 316 | if (isEqual(data, defaultRolePageConfig)) return null; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 317 | return data; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 318 | }; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 319 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 320 | const createRoleMenuOptionPage = async ( |
| 321 | interaction: StringSelectMenuInteraction | ButtonInteraction, |
| 322 | m: Message, |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 323 | data?: { name: string; description: string | null; role: string }, |
| 324 | newRole: boolean = false |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 325 | ) => { |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 326 | const initialData = _.cloneDeep(data); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 327 | const { renderRole } = client.logger; |
| 328 | if (!data) |
| 329 | data = { |
| 330 | name: "New role Menu Option", |
| 331 | description: null, |
| 332 | role: "" |
| 333 | }; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 334 | let back = false; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 335 | const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 336 | new ButtonBuilder() |
| 337 | .setCustomId("back") |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 338 | .setLabel(newRole ? "Add" : "Back") |
| 339 | .setStyle(newRole ? ButtonStyle.Success : ButtonStyle.Secondary) |
| 340 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 341 | new ButtonBuilder() |
| 342 | .setCustomId("edit") |
| 343 | .setLabel("Edit Details") |
| 344 | .setStyle(ButtonStyle.Primary) |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 345 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 346 | new ButtonBuilder() |
| 347 | .setCustomId("delete") |
| 348 | .setLabel("Delete") |
| 349 | .setStyle(ButtonStyle.Danger) |
| 350 | .setEmoji(getEmojiByName("TICKETS.ISSUE", "id") as APIMessageComponentEmoji), |
| 351 | new ButtonBuilder() |
| 352 | .setCustomId("cancel") |
| 353 | .setLabel("Cancel") |
| 354 | .setStyle(ButtonStyle.Secondary) |
| 355 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 356 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 357 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 358 | const roleSelect = new RoleSelectMenuBuilder() |
| 359 | .setCustomId("role") |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 360 | .setPlaceholder(data.role ? "Change role to" : "Select a role"); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 361 | const embed = new EmojiEmbed() |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 362 | .setTitle(`${data.name}`) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 363 | .setStatus("Success") |
| 364 | .setDescription( |
| 365 | `**Description:**\n> ${data.description ?? "No description set"}\n\n` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 366 | `**Role:** ${ |
| 367 | data.role ? renderRole((await interaction.guild!.roles.fetch(data.role))!) : "No role set" |
| 368 | }\n` |
| 369 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 370 | |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 371 | await interaction.editReply({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 372 | embeds: [embed], |
| 373 | components: [new ActionRowBuilder<RoleSelectMenuBuilder>().addComponents(roleSelect), buttons] |
| 374 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 375 | |
| 376 | let i: RoleSelectMenuInteraction | ButtonInteraction; |
| 377 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 378 | i = (await m.awaitMessageComponent({ |
| 379 | time: 300000, |
| 380 | filter: (i) => |
| 381 | i.user.id === interaction.user.id && i.message.id === m.id && i.channelId === interaction.channelId |
| 382 | })) as ButtonInteraction | RoleSelectMenuInteraction; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 383 | } catch (e) { |
| 384 | back = true; |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | if (i.isRoleSelectMenu()) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 389 | if (i.customId === "role") { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 390 | await i.deferUpdate(); |
| 391 | data.role = (i as RoleSelectMenuInteraction).values[0]!; |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 392 | await interaction.editReply({ |
| 393 | embeds: [ |
| 394 | new EmojiEmbed().setTitle(`Applying changes`).setStatus("Danger").setEmoji("NUCLEUS.LOADING") |
| 395 | ], |
| 396 | components: [] |
| 397 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 398 | } |
| 399 | } else if (i.isButton()) { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 400 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 401 | case "back": { |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 402 | await i.deferUpdate(); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 403 | back = true; |
| 404 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 405 | } |
| 406 | case "edit": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 407 | const [name, description] = await editNameDescription( |
| 408 | i, |
| 409 | interaction, |
| 410 | m, |
| 411 | data as { name: string; description: string } |
| 412 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 413 | data.name = name ? name : data.name; |
| 414 | data.description = description ? description : data.description; |
| 415 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 416 | } |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 417 | case "delete": { |
| 418 | await i.deferUpdate(); |
| 419 | return null; |
| 420 | } |
| 421 | case "cancel": { |
| 422 | await i.deferUpdate(); |
| 423 | if (newRole) return null; |
| 424 | else return initialData; |
| 425 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | } while (!back); |
| 429 | return data; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 430 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 431 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 432 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 433 | if (!interaction.guild) return; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 434 | const m = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 435 | |
| 436 | let page = 0; |
| 437 | let closed = false; |
| 438 | const config = await client.database.guilds.read(interaction.guild.id); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 439 | const currentObject: typeof config.roleMenu = _.cloneDeep(config.roleMenu); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 440 | let modified = false; |
| 441 | do { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 442 | const embed = new EmojiEmbed().setTitle("Role Menu").setEmoji("GUILD.GREEN").setStatus("Success"); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 443 | const noRoleMenus = currentObject.options.length === 0; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 444 | let current: ObjectSchema; |
| 445 | |
| 446 | const pageSelect = new StringSelectMenuBuilder() |
| 447 | .setCustomId("page") |
| 448 | .setPlaceholder("Select a Role Menu page to manage"); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 449 | let actionSelect; |
| 450 | if (page === 0) { |
| 451 | actionSelect = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 452 | new ButtonBuilder() |
| 453 | .setCustomId("switch") |
| 454 | .setLabel(currentObject.enabled ? "Enabled" : "Disabled") |
| 455 | .setStyle(currentObject.enabled ? ButtonStyle.Success : ButtonStyle.Danger) |
| 456 | .setEmoji( |
| 457 | getEmojiByName( |
| 458 | currentObject.enabled ? "CONTROL.TICK" : "CONTROL.CROSS", |
| 459 | "id" |
| 460 | ) as APIMessageComponentEmoji |
| 461 | ) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 462 | ); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 463 | } else { |
| 464 | actionSelect = new StringSelectMenuBuilder() |
| 465 | .setCustomId("action") |
| 466 | .setPlaceholder("Perform an action") |
| 467 | .addOptions( |
| 468 | new StringSelectMenuOptionBuilder() |
| 469 | .setLabel("Edit") |
| 470 | .setDescription("Edit this page") |
| 471 | .setValue("edit") |
| 472 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 473 | new StringSelectMenuOptionBuilder() |
| 474 | .setLabel("Delete") |
| 475 | .setDescription("Delete this page") |
| 476 | .setValue("delete") |
| 477 | .setEmoji(getEmojiByName("TICKETS.ISSUE", "id") as APIMessageComponentEmoji) |
| 478 | ); |
| 479 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 480 | const buttonRow = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 481 | new ButtonBuilder() |
| 482 | .setCustomId("back") |
| 483 | .setStyle(ButtonStyle.Primary) |
| 484 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 485 | .setDisabled(page === 0), |
| 486 | new ButtonBuilder() |
| 487 | .setCustomId("next") |
| 488 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id") as APIMessageComponentEmoji) |
| 489 | .setStyle(ButtonStyle.Primary) |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 490 | .setDisabled(page === Object.keys(currentObject.options).length || noRoleMenus), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 491 | new ButtonBuilder() |
| 492 | .setCustomId("add") |
| 493 | .setLabel("New Page") |
| 494 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id") as APIMessageComponentEmoji) |
| 495 | .setStyle(ButtonStyle.Secondary) |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 496 | .setDisabled(Object.keys(currentObject.options).length >= 24), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 497 | new ButtonBuilder() |
| 498 | .setCustomId("reorder") |
| 499 | .setLabel("Reorder Pages") |
| 500 | .setEmoji(getEmojiByName("ICONS.REORDER", "id") as APIMessageComponentEmoji) |
| 501 | .setStyle(ButtonStyle.Secondary) |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 502 | .setDisabled(Object.keys(currentObject.options).length <= 1), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 503 | new ButtonBuilder() |
| 504 | .setCustomId("save") |
| 505 | .setLabel("Save") |
| 506 | .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji) |
| 507 | .setStyle(ButtonStyle.Success) |
| 508 | .setDisabled(!modified) |
| 509 | ); |
| 510 | if (noRoleMenus) { |
| 511 | embed.setDescription( |
| 512 | "No role menu pages have been set up yet. Use the button below to add one.\n\n" + |
| 513 | createPageIndicator(1, 1, undefined, true) |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 514 | ); |
| 515 | pageSelect.setDisabled(true); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 516 | if (page > 0) (actionSelect as StringSelectMenuBuilder).setDisabled(true); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 517 | pageSelect.addOptions(new StringSelectMenuOptionBuilder().setLabel("No role menu pages").setValue("none")); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 518 | } else if (page === 0) { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 519 | embed.setDescription( |
PineappleFan | 0c1d9bf | 2023-06-06 18:35:44 +0100 | [diff] [blame] | 520 | `**Enabled:** ${currentObject.enabled ? `${tick} Yes` : `${cross} No`}\n\n` + |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 521 | `**Pages:** ${currentObject.options.length}\n` + |
| 522 | (currentObject.options.length > 0 |
| 523 | ? currentObject.options |
| 524 | .map((key: ObjectSchema) => { |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 525 | const crossOut = key.enabled ? "" : "~~"; |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 526 | return `> ${crossOut}**${key.name}:** ${ |
| 527 | key.description ?? "*No description set*" |
| 528 | }${crossOut}`; |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 529 | }) |
| 530 | .join("\n") |
| 531 | : "") |
| 532 | ); |
TheCodedProf | fc8d6ba | 2023-06-05 11:16:35 -0400 | [diff] [blame] | 533 | if (currentObject.options.length > 0) { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 534 | pageSelect.addOptions( |
| 535 | currentObject.options.map((key: ObjectSchema, index) => { |
| 536 | return new StringSelectMenuOptionBuilder() |
| 537 | .setLabel(ellipsis(key.name, 50)) |
TheCodedProf | fc8d6ba | 2023-06-05 11:16:35 -0400 | [diff] [blame] | 538 | .setDescription( |
| 539 | ellipsis( |
| 540 | key.description?.length |
| 541 | ? key.description.length > 0 |
| 542 | ? key.description |
| 543 | : "No description set" |
| 544 | : "No description set", |
| 545 | 50 |
| 546 | ) |
| 547 | ) |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 548 | .setValue((index + 1).toString()); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 549 | }) |
| 550 | ); |
| 551 | } else { |
| 552 | pageSelect.setDisabled(true); |
TheCodedProf | fc8d6ba | 2023-06-05 11:16:35 -0400 | [diff] [blame] | 553 | pageSelect.addOptions( |
| 554 | new StringSelectMenuOptionBuilder().setLabel("No role menu pages").setValue("none") |
| 555 | ); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 556 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 557 | } else { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 558 | page = Math.max(Math.min(page, currentObject.options.length), 0); |
| 559 | current = currentObject.options[page - 1]!; |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 560 | (actionSelect as StringSelectMenuBuilder).addOptions( |
| 561 | new StringSelectMenuOptionBuilder() |
| 562 | .setLabel(current.enabled ? "Disable" : "Enable") |
| 563 | .setDescription("Enable or disable this page") |
| 564 | .setValue("switch") |
| 565 | .setEmoji( |
| 566 | getEmojiByName( |
| 567 | current.enabled ? "CONTROL.CROSS" : "CONTROL.TICK", |
| 568 | "id" |
| 569 | ) as APIMessageComponentEmoji |
| 570 | ) |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 571 | ); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 572 | embed.setDescription( |
| 573 | `**Currently Editing:** ${current.name}\n\n` + |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 574 | `**Visible:** ${current.enabled ? `${tick} Yes` : `${cross} No`}\n\n` + |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 575 | `**Description:**\n> ${current.description ?? "*No description set*"}\n` + |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 576 | `\n\n${createPageIndicator( |
| 577 | Object.keys(currentObject.options).length, |
| 578 | page - 1, |
| 579 | false, |
| 580 | Object.values(currentObject.options).map((o) => !(o.enabled ?? true)) |
| 581 | )}` |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 582 | ); |
| 583 | |
| 584 | pageSelect.addOptions( |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 585 | currentObject.options.map((key: ObjectSchema, index) => { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 586 | return new StringSelectMenuOptionBuilder() |
| 587 | .setLabel(ellipsis(key.name, 50)) |
TheCodedProf | fc8d6ba | 2023-06-05 11:16:35 -0400 | [diff] [blame] | 588 | .setDescription( |
| 589 | ellipsis( |
| 590 | key.description?.length |
| 591 | ? key.description.length > 0 |
| 592 | ? key.description |
| 593 | : "No description set" |
| 594 | : "No description set", |
| 595 | 50 |
| 596 | ) |
| 597 | ) |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 598 | .setValue((index + 1).toString()); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 599 | }) |
| 600 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 601 | } |
| 602 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 603 | await interaction.editReply({ |
| 604 | embeds: [embed], |
| 605 | components: [ |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 606 | page === 0 |
| 607 | ? (actionSelect as ActionRowBuilder<ButtonBuilder>) |
| 608 | : new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 609 | actionSelect as StringSelectMenuBuilder |
| 610 | ), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 611 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(pageSelect), |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 612 | buttonRow as ActionRowBuilder<ButtonBuilder> |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 613 | ] |
| 614 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 615 | let i: StringSelectMenuInteraction | ButtonInteraction; |
| 616 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 617 | i = (await m.awaitMessageComponent({ |
| 618 | time: 300000, |
| 619 | filter: (i) => |
| 620 | i.user.id === interaction.user.id && i.message.id === m.id && i.channelId === interaction.channelId |
| 621 | })) as ButtonInteraction | StringSelectMenuInteraction; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 622 | } catch (e) { |
| 623 | closed = true; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 624 | continue; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | await i.deferUpdate(); |
| 628 | if (i.isButton()) { |
| 629 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 630 | case "back": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 631 | page--; |
| 632 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 633 | } |
| 634 | case "next": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 635 | page++; |
| 636 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 637 | } |
| 638 | case "add": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 639 | const newPage = await editRoleMenuPage(i, m); |
pineafan | 44713d4 | 2023-06-03 20:29:49 +0100 | [diff] [blame] | 640 | if (_.isEqual(newPage, defaultRoleMenuData)) { |
| 641 | break; |
| 642 | } |
pineafan | 6f600f0 | 2023-06-03 20:27:07 +0100 | [diff] [blame] | 643 | if (newPage) { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 644 | currentObject.options.push(newPage); |
| 645 | page = currentObject.options.length; |
| 646 | modified = true; |
pineafan | 6f600f0 | 2023-06-03 20:27:07 +0100 | [diff] [blame] | 647 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 648 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 649 | } |
| 650 | case "reorder": { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 651 | const reordered = await reorderRoleMenuPages(interaction, m, currentObject.options); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 652 | if (!reordered) break; |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 653 | currentObject.options = reordered; |
| 654 | modified = true; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 655 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 656 | } |
| 657 | case "save": { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 658 | await client.database.guilds.write(interaction.guild.id, { |
TheCodedProf | c62b070 | 2023-06-06 15:31:14 -0400 | [diff] [blame] | 659 | roleMenu: currentObject |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 660 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 661 | modified = false; |
Skyler Grey | 16ecb17 | 2023-03-05 07:30:32 +0000 | [diff] [blame] | 662 | await client.memory.forceUpdate(interaction.guild.id); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 663 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 664 | } |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 665 | case "switch": { |
| 666 | currentObject.enabled = !currentObject.enabled; |
| 667 | modified = true; |
| 668 | break; |
| 669 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 670 | } |
| 671 | } else if (i.isStringSelectMenu()) { |
| 672 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 673 | case "action": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 674 | switch (i.values[0]) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 675 | case "edit": { |
| 676 | const edited = await editRoleMenuPage(i, m, current!); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 677 | if (!edited) break; |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 678 | currentObject.options[page - 1] = edited; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 679 | modified = true; |
| 680 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 681 | } |
| 682 | case "delete": { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 683 | if (page === 0 && currentObject.options.keys.length - 1 > 0) page++; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 684 | else page--; |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 685 | currentObject.options.splice(page, 1); |
| 686 | modified = true; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 687 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 688 | } |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame] | 689 | case "switch": { |
| 690 | currentObject.options[page - 1]!.enabled = !currentObject.options[page - 1]!.enabled; |
| 691 | modified = true; |
| 692 | break; |
| 693 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 694 | } |
| 695 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 696 | } |
| 697 | case "page": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 698 | page = parseInt(i.values[0]!); |
| 699 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 700 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 701 | } |
| 702 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 703 | } while (!closed); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 704 | await interaction.deleteReply(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 705 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 706 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 707 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 708 | const member = interaction.member as Discord.GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 709 | if (!member.permissions.has("ManageRoles")) |
| 710 | return "You must have the *Manage Roles* permission to use this command"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 711 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 712 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 713 | |
| 714 | export { command }; |
| 715 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 716 | export { check }; |