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