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