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