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