pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 1 | import Discord, { CommandInteraction, GuildMember, Message, MessageActionRow, MessageButton } from "discord.js"; |
| 2 | import { SelectMenuOption, SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
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 |
| 11 | .setName("track") |
| 12 | .setDescription("Moves a user along a role track") |
| 13 | .addUserOption(option => option.setName("user").setDescription("The user to manage").setRequired(true)) |
| 14 | |
| 15 | const generateFromTrack = (position: number, active: any, size: number, disabled: any) => { |
| 16 | active = active ? "ACTIVE" : "INACTIVE" |
| 17 | disabled = disabled ? "GREY." : "" |
| 18 | if (position == 0 && size == 1) return "TRACKS.SINGLE." + disabled + active |
| 19 | if (position == size - 1) return "TRACKS.VERTICAL.BOTTOM." + disabled + active |
| 20 | if (position == 0) return "TRACKS.VERTICAL.TOP." + disabled + active |
| 21 | return "TRACKS.VERTICAL.MIDDLE." + disabled + active |
| 22 | } |
| 23 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 24 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
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 | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 28 | let config = await client.database.guilds.read(guild.id); |
| 29 | await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 30 | .setEmoji("NUCLEUS.LOADING") |
| 31 | .setTitle("Loading") |
| 32 | .setStatus("Danger") |
| 33 | ], ephemeral: true}) |
| 34 | let track = 0 |
| 35 | let generated; |
| 36 | const roles = await guild.roles.fetch() |
| 37 | let memberRoles = await member.roles |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 38 | let managed |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 39 | while (true) { |
| 40 | let data = config.tracks[track] |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 41 | if (data.manageableBy !== undefined) managed = data.manageableBy.some(element => {return memberRoles.cache.has(element)}) |
| 42 | else managed = false |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 43 | let dropdown = new Discord.MessageSelectMenu().addOptions(config.tracks.map((option, index) => { |
| 44 | let hasRoleInTrack = option.track.some(element => {return memberRoles.cache.has(element)}) |
| 45 | return new SelectMenuOption({ |
| 46 | default: index == track, |
| 47 | label: option.name, |
| 48 | value: index.toString(), |
pineafan | e625d78 | 2022-05-09 18:04:32 +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 | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 51 | }) |
| 52 | })).setCustomId("select").setMaxValues(1) |
| 53 | let 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" |
| 57 | generated += "\n" + data.track.map((role, index) => { |
| 58 | let allow = (roles.get(role).position >= (interaction.member as GuildMember).roles.highest.position) && !managed |
| 59 | allowed.push(!allow) |
| 60 | return getEmojiByName(generateFromTrack( |
| 61 | index, |
| 62 | memberRoles.cache.has(role), |
| 63 | data.track.length, |
| 64 | allow |
| 65 | )) + " " + |
| 66 | roles.get(role).name + " [<@&" + roles.get(role).id + ">]" |
| 67 | }).join("\n") |
| 68 | let selected = []; |
| 69 | for (let i = 0; i < data.track.length; i++) { |
| 70 | if (memberRoles.cache.has(data.track[i])) selected.push(data.track[i]) |
| 71 | } |
| 72 | let conflict = data.retainPrevious ? false : selected.length > 1; |
| 73 | let conflictDropdown |
| 74 | let currentRoleIndex |
| 75 | if (conflict) { |
| 76 | generated += `\n\n${getEmojiByName(`PUNISH.WARN.${managed ? "YELLOW" : "RED"}`)} This user has ${selected.length} roles from this track. ` |
| 77 | conflictDropdown = [] |
| 78 | if ( |
| 79 | (roles.get(selected[0]).position < memberRoles.highest.position) || managed |
| 80 | ) { |
| 81 | generated += `In order to promote or demote this user, you must select which role the member should keep.` |
| 82 | selected.forEach(role => { |
| 83 | conflictDropdown.push(new SelectMenuOption({ |
| 84 | label: roles.get(role).name, |
| 85 | value: roles.get(role).id, |
| 86 | })) |
| 87 | }) |
| 88 | conflictDropdown = [new Discord.MessageSelectMenu() |
| 89 | .addOptions(conflictDropdown) |
| 90 | .setCustomId("conflict") |
| 91 | .setMaxValues(1) |
| 92 | .setPlaceholder("Select a role to keep")] |
| 93 | } else { |
| 94 | generated += "You don't have permission to manage one or more of the users roles, and therefore can't select one to keep." |
| 95 | } |
| 96 | } else { |
| 97 | currentRoleIndex = selected.length == 0 ? -1 : data.track.indexOf(selected[0].toString()) |
| 98 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 99 | let 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 | ] |
| 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 | ]) |
| 126 | ])}) |
| 127 | let component; |
| 128 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 129 | component = await (m as Message).awaitMessageComponent({time: 300000}); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 130 | } catch (e) { |
| 131 | return |
| 132 | } |
| 133 | component.deferUpdate() |
| 134 | if (component.customId == "conflict") { |
| 135 | let rolesToRemove = selected.filter(role => role != component.values[0]) |
| 136 | await member.roles.remove(rolesToRemove) |
| 137 | } else if (component.customId == "promote") { |
| 138 | if ( |
| 139 | currentRoleIndex == -1 ? allowed[data.track.length - 1] : |
| 140 | allowed[currentRoleIndex - 1] && allowed[currentRoleIndex] |
| 141 | ) { |
| 142 | if (currentRoleIndex == -1) { |
| 143 | await member.roles.add(data.track[data.track.length - 1]) |
| 144 | } else if (currentRoleIndex < data.track.length) { |
| 145 | if (!data.retainPrevious) await member.roles.remove(data.track[currentRoleIndex]) |
| 146 | await member.roles.add(data.track[currentRoleIndex - 1]) |
| 147 | } |
| 148 | } |
| 149 | } else if (component.customId == "demote") { |
| 150 | if(allowed[currentRoleIndex]) { |
| 151 | if (currentRoleIndex == data.track.length - 1) { |
| 152 | if (data.nullable) await member.roles.remove(data.track[currentRoleIndex]) |
| 153 | } else if (currentRoleIndex > -1) { |
| 154 | await member.roles.remove(data.track[currentRoleIndex]) |
| 155 | await member.roles.add(data.track[currentRoleIndex + 1]) |
| 156 | } |
| 157 | } |
| 158 | } else if (component.customId == "select") { |
| 159 | track = component.values[0] |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 164 | const check = async (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 165 | let tracks = (await client.database.guilds.read(interaction.guild.id)).tracks |
| 166 | if (!tracks) throw "This server does not have any tracks" |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 167 | let member = (interaction.member as GuildMember) |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 168 | // Allow the owner to promote anyone |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame] | 169 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 170 | // Check if the user can manage any of the tracks |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 171 | let managed = false |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 172 | tracks.forEach(element => { |
| 173 | if (!element.track.manageableBy) return |
| 174 | if (element.track.manageableBy.some(role => member.roles.cache.has(role))) managed = true |
| 175 | }); |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 176 | // Check if the user has manage_roles permission |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 177 | 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] | 178 | // Allow track |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 179 | return true; |
pineafan | ad54d75 | 2022-04-18 19:01:43 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | export { command }; |
| 183 | export { callback }; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 184 | export { check }; |
| 185 | |