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