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 |
| 114 | ): Promise<[string | undefined, string | undefined, { min: number, max: number } | undefined]> => { |
| 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 | ) |
| 137 | ] |
| 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 | ) |
| 160 | ) |
| 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") |
| 165 | .addComponents( |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 166 | ...components |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 167 | ); |
| 168 | const button = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 169 | new ButtonBuilder() |
| 170 | .setCustomId("back") |
| 171 | .setLabel("Back") |
| 172 | .setStyle(ButtonStyle.Secondary) |
| 173 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji) |
| 174 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 175 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 176 | await i.showModal(modal); |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 177 | await interaction.editReply({ |
| 178 | embeds: [ |
| 179 | new EmojiEmbed() |
| 180 | .setTitle("Role Menu") |
| 181 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 182 | .setStatus("Success") |
| 183 | ], |
| 184 | components: [button] |
| 185 | }); |
| 186 | |
| 187 | let out: Discord.ModalSubmitInteraction | null; |
| 188 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 189 | out = (await modalInteractionCollector(m, interaction.user)) as Discord.ModalSubmitInteraction | null; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 190 | } catch (e) { |
| 191 | console.error(e); |
| 192 | out = null; |
| 193 | } |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 194 | if (!out) return [name, description, bounds]; |
| 195 | if (out.isButton()) return [name, description, bounds]; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 196 | name = out.fields.fields.find((f) => f.customId === "name")?.value ?? name; |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 197 | description = out.fields.fields.find((f) => f.customId === "description")?.value; |
| 198 | if (addBounds) { |
| 199 | const min = parseInt(out.fields.fields.find((f) => f.customId === "min")?.value!); |
| 200 | const max = parseInt(out.fields.fields.find((f) => f.customId === "max")?.value!); |
| 201 | bounds = { min, max }; |
| 202 | } |
| 203 | return [name, description, bounds]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 204 | }; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 205 | |
TheCodedProf | 7a83f76 | 2023-03-06 17:17:00 -0500 | [diff] [blame] | 206 | const defaultRoleMenuData = { |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 207 | enabled: true, |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 208 | name: "New Page", |
| 209 | description: "", |
TheCodedProf | 7a83f76 | 2023-03-06 17:17:00 -0500 | [diff] [blame] | 210 | min: 0, |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 211 | max: 1, |
TheCodedProf | 7a83f76 | 2023-03-06 17:17:00 -0500 | [diff] [blame] | 212 | options: [] |
| 213 | }; |
| 214 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 215 | const editRoleMenuPage = async ( |
| 216 | interaction: StringSelectMenuInteraction | ButtonInteraction, |
| 217 | m: Message, |
| 218 | data?: ObjectSchema |
| 219 | ): Promise<ObjectSchema | null> => { |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 220 | if (!data) data = _.cloneDeep(defaultRoleMenuData); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 221 | const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 222 | new ButtonBuilder() |
| 223 | .setCustomId("back") |
| 224 | .setLabel("Back") |
| 225 | .setStyle(ButtonStyle.Secondary) |
| 226 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id") as APIMessageComponentEmoji), |
| 227 | new ButtonBuilder() |
| 228 | .setCustomId("edit") |
| 229 | .setLabel("Edit") |
| 230 | .setStyle(ButtonStyle.Primary) |
| 231 | .setEmoji(getEmojiByName("ICONS.EDIT", "id") as APIMessageComponentEmoji), |
| 232 | new ButtonBuilder() |
| 233 | .setCustomId("addRole") |
| 234 | .setLabel("Add Role") |
| 235 | .setStyle(ButtonStyle.Secondary) |
| 236 | .setEmoji(getEmojiByName("TICKETS.SUGGESTION", "id") as APIMessageComponentEmoji) |
| 237 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 238 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 239 | let back = false; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 240 | do { |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 241 | const noRoles = data.options.length === 0; |
| 242 | const previewSelect = configToDropdown( |
| 243 | "Edit Roles", |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 244 | data.description ? { |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 245 | name: data.name, |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 246 | description: data.description, |
| 247 | min: 1, |
| 248 | max: 1, |
| 249 | options: noRoles ? [{ name: "Role 1", description: null, role: "No role set" }] : data.options |
| 250 | } : { |
| 251 | name: data.name, |
pineafan | 72659cc | 2023-05-28 13:36:44 +0100 | [diff] [blame] | 252 | min: 1, |
| 253 | max: 1, |
| 254 | options: noRoles ? [{ name: "Role 1", description: null, role: "No role set" }] : data.options |
| 255 | }, |
| 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 | 4a088b1 | 2023-06-06 15:29:59 -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 ? "" : "~~"; |
| 526 | return `> ${crossOut}**${key.name}:** ${key.description ?? "*No description set*"}${crossOut}`; |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 527 | }) |
| 528 | .join("\n") |
| 529 | : "") |
| 530 | ); |
TheCodedProf | fc8d6ba | 2023-06-05 11:16:35 -0400 | [diff] [blame] | 531 | if (currentObject.options.length > 0) { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 532 | pageSelect.addOptions( |
| 533 | currentObject.options.map((key: ObjectSchema, index) => { |
| 534 | return new StringSelectMenuOptionBuilder() |
| 535 | .setLabel(ellipsis(key.name, 50)) |
TheCodedProf | fc8d6ba | 2023-06-05 11:16:35 -0400 | [diff] [blame] | 536 | .setDescription( |
| 537 | ellipsis( |
| 538 | key.description?.length |
| 539 | ? key.description.length > 0 |
| 540 | ? key.description |
| 541 | : "No description set" |
| 542 | : "No description set", |
| 543 | 50 |
| 544 | ) |
| 545 | ) |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 546 | .setValue((index + 1).toString()); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 547 | }) |
| 548 | ); |
| 549 | } else { |
| 550 | pageSelect.setDisabled(true); |
TheCodedProf | fc8d6ba | 2023-06-05 11:16:35 -0400 | [diff] [blame] | 551 | pageSelect.addOptions( |
| 552 | new StringSelectMenuOptionBuilder().setLabel("No role menu pages").setValue("none") |
| 553 | ); |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 554 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 555 | } else { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 556 | page = Math.max(Math.min(page, currentObject.options.length), 0); |
| 557 | current = currentObject.options[page - 1]!; |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 558 | (actionSelect as StringSelectMenuBuilder).addOptions( |
| 559 | new StringSelectMenuOptionBuilder() |
| 560 | .setLabel(current.enabled ? "Disable" : "Enable") |
| 561 | .setDescription("Enable or disable this page") |
| 562 | .setValue("switch") |
| 563 | .setEmoji( |
| 564 | getEmojiByName( |
| 565 | current.enabled ? "CONTROL.CROSS" : "CONTROL.TICK", |
| 566 | "id" |
| 567 | ) as APIMessageComponentEmoji |
| 568 | ) |
| 569 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 570 | embed.setDescription( |
| 571 | `**Currently Editing:** ${current.name}\n\n` + |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 572 | `**Visible:** ${current.enabled ? `${tick} Yes` : `${cross} No`}\n\n` + |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 573 | `**Description:**\n> ${current.description ?? "*No description set*"}\n` + |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 574 | `\n\n${createPageIndicator(Object.keys(currentObject.options).length, page - 1, false, Object.values(currentObject.options).map((o) => !(o.enabled ?? true)))}` |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 575 | ); |
| 576 | |
| 577 | pageSelect.addOptions( |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 578 | currentObject.options.map((key: ObjectSchema, index) => { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 579 | return new StringSelectMenuOptionBuilder() |
| 580 | .setLabel(ellipsis(key.name, 50)) |
TheCodedProf | fc8d6ba | 2023-06-05 11:16:35 -0400 | [diff] [blame] | 581 | .setDescription( |
| 582 | ellipsis( |
| 583 | key.description?.length |
| 584 | ? key.description.length > 0 |
| 585 | ? key.description |
| 586 | : "No description set" |
| 587 | : "No description set", |
| 588 | 50 |
| 589 | ) |
| 590 | ) |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 591 | .setValue((index + 1).toString()); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 592 | }) |
| 593 | ); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 594 | } |
| 595 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 596 | await interaction.editReply({ |
| 597 | embeds: [embed], |
| 598 | components: [ |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 599 | page === 0 |
| 600 | ? (actionSelect as ActionRowBuilder<ButtonBuilder>) |
| 601 | : new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 602 | actionSelect as StringSelectMenuBuilder |
| 603 | ), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 604 | new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(pageSelect), |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 605 | buttonRow as ActionRowBuilder<ButtonBuilder> |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 606 | ] |
| 607 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 608 | let i: StringSelectMenuInteraction | ButtonInteraction; |
| 609 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 610 | i = (await m.awaitMessageComponent({ |
| 611 | time: 300000, |
| 612 | filter: (i) => |
| 613 | i.user.id === interaction.user.id && i.message.id === m.id && i.channelId === interaction.channelId |
| 614 | })) as ButtonInteraction | StringSelectMenuInteraction; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 615 | } catch (e) { |
| 616 | closed = true; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 617 | continue; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | await i.deferUpdate(); |
| 621 | if (i.isButton()) { |
| 622 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 623 | case "back": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 624 | page--; |
| 625 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 626 | } |
| 627 | case "next": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 628 | page++; |
| 629 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 630 | } |
| 631 | case "add": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 632 | const newPage = await editRoleMenuPage(i, m); |
pineafan | 44713d4 | 2023-06-03 20:29:49 +0100 | [diff] [blame] | 633 | if (_.isEqual(newPage, defaultRoleMenuData)) { |
| 634 | break; |
| 635 | } |
pineafan | 6f600f0 | 2023-06-03 20:27:07 +0100 | [diff] [blame] | 636 | if (newPage) { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 637 | currentObject.options.push(newPage); |
| 638 | page = currentObject.options.length; |
| 639 | modified = true; |
pineafan | 6f600f0 | 2023-06-03 20:27:07 +0100 | [diff] [blame] | 640 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 641 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 642 | } |
| 643 | case "reorder": { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 644 | const reordered = await reorderRoleMenuPages(interaction, m, currentObject.options); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 645 | if (!reordered) break; |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 646 | currentObject.options = reordered; |
| 647 | modified = true; |
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 "save": { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 651 | await client.database.guilds.write(interaction.guild.id, { |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 652 | "roleMenu": currentObject |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 653 | }); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 654 | modified = false; |
Skyler Grey | 16ecb17 | 2023-03-05 07:30:32 +0000 | [diff] [blame] | 655 | await client.memory.forceUpdate(interaction.guild.id); |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 656 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 657 | } |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 658 | case "switch": { |
| 659 | currentObject.enabled = !currentObject.enabled; |
| 660 | modified = true; |
| 661 | break; |
| 662 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 663 | } |
| 664 | } else if (i.isStringSelectMenu()) { |
| 665 | switch (i.customId) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 666 | case "action": { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 667 | switch (i.values[0]) { |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 668 | case "edit": { |
| 669 | const edited = await editRoleMenuPage(i, m, current!); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 670 | if (!edited) break; |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 671 | currentObject.options[page - 1] = edited; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 672 | modified = true; |
| 673 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 674 | } |
| 675 | case "delete": { |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 676 | if (page === 0 && currentObject.options.keys.length - 1 > 0) page++; |
TheCodedProf | f4facde | 2023-01-28 13:42:48 -0500 | [diff] [blame] | 677 | else page--; |
TheCodedProf | 920d729 | 2023-06-05 11:02:32 -0400 | [diff] [blame] | 678 | currentObject.options.splice(page, 1); |
| 679 | modified = true; |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 680 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 681 | } |
TheCodedProf | 4a088b1 | 2023-06-06 15:29:59 -0400 | [diff] [blame^] | 682 | case "switch": { |
| 683 | currentObject.options[page - 1]!.enabled = !currentObject.options[page - 1]!.enabled; |
| 684 | modified = true; |
| 685 | break; |
| 686 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 687 | } |
| 688 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 689 | } |
| 690 | case "page": { |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 691 | page = parseInt(i.values[0]!); |
| 692 | break; |
PineaFan | b0d0c24 | 2023-02-05 10:59:45 +0000 | [diff] [blame] | 693 | } |
TheCodedProf | 1c3ad3c | 2023-01-25 17:58:36 -0500 | [diff] [blame] | 694 | } |
| 695 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 696 | } while (!closed); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 697 | await interaction.deleteReply(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 698 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 699 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 700 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 701 | const member = interaction.member as Discord.GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 702 | if (!member.permissions.has("ManageRoles")) |
| 703 | return "You must have the *Manage Roles* permission to use this command"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 704 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 705 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 706 | |
| 707 | export { command }; |
| 708 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 709 | export { check }; |