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