pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 2 | import Discord, { |
| 3 | CommandInteraction, |
| 4 | GuildMember, |
| 5 | Message, |
| 6 | MessageActionRow, |
| 7 | MessageButton |
| 8 | } from "discord.js"; |
| 9 | import { |
| 10 | SelectMenuOption, |
| 11 | SlashCommandSubcommandBuilder |
| 12 | } from "@discordjs/builders"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 14 | // @ts-expect-error |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 15 | import { WrappedCheck } from "jshaiku"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 16 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 17 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 18 | import addPlural from "../../utils/plurals.js"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 19 | import client from "../../utils/client.js"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 20 | |
| 21 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 22 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | .setName("track") |
| 24 | .setDescription("Moves a user along a role track") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 25 | .addUserOption((option) => |
| 26 | option |
| 27 | .setName("user") |
| 28 | .setDescription("The user to manage") |
| 29 | .setRequired(true) |
| 30 | ); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 31 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 32 | const generateFromTrack = ( |
| 33 | position: number, |
| 34 | active: string | boolean, |
| 35 | size: number, |
| 36 | disabled: string | boolean |
| 37 | ) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 38 | active = active ? "ACTIVE" : "INACTIVE"; |
| 39 | disabled = disabled ? "GREY." : ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 40 | if (position === 0 && size === 1) |
| 41 | return "TRACKS.SINGLE." + disabled + active; |
| 42 | if (position === size - 1) |
| 43 | return "TRACKS.VERTICAL.BOTTOM." + disabled + active; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | if (position === 0) return "TRACKS.VERTICAL.TOP." + disabled + active; |
| 45 | return "TRACKS.VERTICAL.MIDDLE." + disabled + active; |
| 46 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 47 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 48 | const callback = async ( |
| 49 | interaction: CommandInteraction |
| 50 | ): Promise<void | unknown> => { |
PineappleFan | b3dd83c | 2022-06-17 10:53:48 +0100 | [diff] [blame] | 51 | const { renderUser } = client.logger; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 52 | const member = interaction.options.getMember("user") as GuildMember; |
| 53 | const guild = interaction.guild; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | if (!guild) return; |
| 55 | const config = await client.database.guilds.read(guild.id); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 56 | await interaction.reply({ embeds: LoadingEmbed, ephemeral: true }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | let track = 0; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 58 | let generated; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | const roles = await guild.roles.fetch(); |
| 60 | const memberRoles = await member.roles; |
| 61 | let managed: boolean; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 62 | while (true) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 63 | const data = config.tracks[track]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 64 | if (data.manageableBy !== undefined) |
| 65 | managed = data.manageableBy.some((element: string) => { |
| 66 | return memberRoles.cache.has(element); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 67 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 68 | else managed = false; |
| 69 | const dropdown = new Discord.MessageSelectMenu() |
| 70 | .addOptions( |
| 71 | config.tracks.map((option, index) => { |
| 72 | const hasRoleInTrack = option.track.some( |
| 73 | (element: string) => { |
| 74 | return memberRoles.cache.has(element); |
| 75 | } |
| 76 | ); |
| 77 | return new SelectMenuOption({ |
| 78 | default: index === track, |
| 79 | label: option.name, |
| 80 | value: index.toString(), |
| 81 | description: |
| 82 | option.track.length === 0 |
| 83 | ? "No" |
| 84 | : addPlural(option.track.length, "role"), |
| 85 | emoji: client.emojis.resolve( |
| 86 | getEmojiByName( |
| 87 | "TRACKS.SINGLE." + |
| 88 | (hasRoleInTrack ? "ACTIVE" : "INACTIVE"), |
| 89 | "id" |
| 90 | ) |
| 91 | ) |
| 92 | }); |
| 93 | }) |
| 94 | ) |
| 95 | .setCustomId("select") |
| 96 | .setMaxValues(1); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 97 | const allowed = []; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 98 | generated = |
| 99 | "**Track:** " + |
| 100 | data.name + |
| 101 | "\n" + |
| 102 | "**Member:** " + |
| 103 | renderUser(member.user) + |
| 104 | "\n"; |
| 105 | generated += |
| 106 | (data.nullable |
| 107 | ? "Members do not need a role in this track" |
| 108 | : "A role in this track is required") + "\n"; |
| 109 | generated += |
| 110 | (data.retainPrevious |
| 111 | ? "When promoted, the user keeps previous roles" |
| 112 | : "Members will lose their current role when promoted") + "\n"; |
| 113 | generated += |
| 114 | "\n" + |
| 115 | data.track |
| 116 | .map((role, index) => { |
| 117 | const allow = |
| 118 | roles.get(role).position >= |
| 119 | (interaction.member as GuildMember).roles.highest |
| 120 | .position && !managed; |
| 121 | allowed.push(!allow); |
| 122 | return ( |
| 123 | getEmojiByName( |
| 124 | generateFromTrack( |
| 125 | index, |
| 126 | memberRoles.cache.has(role), |
| 127 | data.track.length, |
| 128 | allow |
| 129 | ) |
| 130 | ) + |
| 131 | " " + |
| 132 | roles.get(role).name + |
| 133 | " [<@&" + |
| 134 | roles.get(role).id + |
| 135 | ">]" |
| 136 | ); |
| 137 | }) |
| 138 | .join("\n"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 139 | const selected = []; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 140 | for (let i = 0; i < data.track.length; i++) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 141 | if (memberRoles.cache.has(data.track[i])) |
| 142 | selected.push(data.track[i]); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 143 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 144 | const conflict = data.retainPrevious ? false : selected.length > 1; |
| 145 | let conflictDropdown; |
| 146 | let currentRoleIndex; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 147 | if (conflict) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 148 | generated += `\n\n${getEmojiByName( |
| 149 | `PUNISH.WARN.${managed ? "YELLOW" : "RED"}` |
| 150 | )} This user has ${selected.length} roles from this track. `; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 151 | conflictDropdown = []; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 152 | if ( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 153 | roles.get(selected[0]).position < |
| 154 | memberRoles.highest.position || |
| 155 | managed |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 156 | ) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 157 | generated += |
| 158 | "In order to promote or demote this user, you must select which role the member should keep."; |
| 159 | selected.forEach((role) => { |
| 160 | conflictDropdown.push( |
| 161 | new SelectMenuOption({ |
| 162 | label: roles.get(role).name, |
| 163 | value: roles.get(role).id |
| 164 | }) |
| 165 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 166 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 167 | conflictDropdown = [ |
| 168 | new Discord.MessageSelectMenu() |
| 169 | .addOptions(conflictDropdown) |
| 170 | .setCustomId("conflict") |
| 171 | .setMaxValues(1) |
| 172 | .setPlaceholder("Select a role to keep") |
| 173 | ]; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 174 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 175 | generated += |
| 176 | "You don't have permission to manage one or more of the users roles, and therefore can't select one to keep."; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 177 | } |
| 178 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 179 | currentRoleIndex = |
| 180 | selected.length === 0 |
| 181 | ? -1 |
| 182 | : data.track.indexOf(selected[0].toString()); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 183 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 184 | const m = (await interaction.editReply({ |
| 185 | embeds: [ |
| 186 | new EmojiEmbed() |
| 187 | .setEmoji("TRACKS.ICON") |
| 188 | .setTitle("Tracks") |
| 189 | .setDescription(`${generated}`) |
| 190 | .setStatus("Success") |
| 191 | ], |
| 192 | components: [new MessageActionRow().addComponents(dropdown)] |
| 193 | .concat( |
| 194 | conflict && conflictDropdown.length |
| 195 | ? [ |
| 196 | new MessageActionRow().addComponents( |
| 197 | conflictDropdown |
| 198 | ) |
| 199 | ] |
| 200 | : [] |
| 201 | ) |
| 202 | .concat([ |
| 203 | new MessageActionRow().addComponents([ |
| 204 | new MessageButton() |
| 205 | .setEmoji(getEmojiByName("CONTROL.UP", "id")) |
| 206 | .setLabel("Move up") |
| 207 | .setCustomId("promote") |
| 208 | .setStyle("SUCCESS") |
| 209 | .setDisabled( |
| 210 | conflict || |
| 211 | currentRoleIndex === 0 || |
| 212 | (currentRoleIndex === -1 |
| 213 | ? false |
| 214 | : !allowed[currentRoleIndex - 1]) |
| 215 | ), |
| 216 | new MessageButton() |
| 217 | .setEmoji(getEmojiByName("CONTROL.DOWN", "id")) |
| 218 | .setLabel("Move down") |
| 219 | .setCustomId("demote") |
| 220 | .setStyle("DANGER") |
| 221 | .setDisabled( |
| 222 | conflict || |
| 223 | (data.nullable |
| 224 | ? currentRoleIndex <= -1 |
| 225 | : currentRoleIndex === |
| 226 | data.track.length - 1 || |
| 227 | currentRoleIndex <= -1) || |
| 228 | !allowed[currentRoleIndex] |
| 229 | ) |
| 230 | ]) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 231 | ]) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 232 | })) as Message; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 233 | let component; |
| 234 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 235 | component = await m.awaitMessageComponent({ time: 300000 }); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 236 | } catch (e) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 237 | return; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 238 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 239 | component.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 240 | if (component.customId === "conflict") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 241 | const rolesToRemove = selected.filter( |
| 242 | (role) => role !== component.values[0] |
| 243 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 244 | await member.roles.remove(rolesToRemove); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 245 | } else if (component.customId === "promote") { |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 246 | if ( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 247 | currentRoleIndex === -1 |
| 248 | ? allowed[data.track.length - 1] |
| 249 | : allowed[currentRoleIndex - 1] && allowed[currentRoleIndex] |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 250 | ) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 251 | if (currentRoleIndex === -1) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 252 | await member.roles.add(data.track[data.track.length - 1]); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 253 | } else if (currentRoleIndex < data.track.length) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 254 | if (!data.retainPrevious) |
| 255 | await member.roles.remove(data.track[currentRoleIndex]); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 256 | await member.roles.add(data.track[currentRoleIndex - 1]); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 259 | } else if (component.customId === "demote") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 260 | if (allowed[currentRoleIndex]) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 261 | if (currentRoleIndex === data.track.length - 1) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 262 | if (data.nullable) |
| 263 | await member.roles.remove(data.track[currentRoleIndex]); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 264 | } else if (currentRoleIndex > -1) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 265 | await member.roles.remove(data.track[currentRoleIndex]); |
| 266 | await member.roles.add(data.track[currentRoleIndex + 1]); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 269 | } else if (component.customId === "select") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 270 | track = component.values[0]; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 271 | } |
| 272 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 273 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 274 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 275 | const check = async ( |
| 276 | interaction: CommandInteraction, |
| 277 | _defaultCheck: WrappedCheck |
| 278 | ) => { |
| 279 | const tracks = (await client.database.guilds.read(interaction.guild.id)) |
| 280 | .tracks; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 281 | if (tracks.length === 0) throw "This server does not have any tracks"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 282 | const member = interaction.member as GuildMember; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 283 | // Allow the owner to promote anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 284 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 285 | // Check if the user can manage any of the tracks |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 286 | let managed = false; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 287 | for (const element of tracks) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 288 | if (!element.track.manageableBy) continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 289 | if ( |
| 290 | !element.track.manageableBy.some((role) => |
| 291 | member.roles.cache.has(role) |
| 292 | ) |
| 293 | ) |
| 294 | continue; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 295 | managed = true; |
| 296 | break; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 297 | } |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 298 | // Check if the user has manage_roles permission |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 299 | if (!managed && !member.permissions.has("MANAGE_ROLES")) |
| 300 | throw "You do not have the *Manage Roles* permission"; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 301 | // Allow track |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 302 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 303 | }; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 304 | |
| 305 | export { command }; |
| 306 | export { callback }; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 307 | export { check }; |