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