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