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