blob: eea0b7b90e6afd3a3150a10b2a01000e185faa5f [file] [log] [blame]
pineafanad54d752022-04-18 19:01:43 +01001import Discord, { CommandInteraction, GuildMember, Message, MessageActionRow, MessageButton } from "discord.js";
2import { SelectMenuOption, SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
4import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
5import getEmojiByName from "../../utils/getEmojiByName.js";
pineafane625d782022-05-09 18:04:32 +01006import addPlural from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +01007import client from "../../utils/client.js";
pineafanad54d752022-04-18 19:01:43 +01008
9const 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
15const 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
24const callback = async (interaction: CommandInteraction) => {
25 // @ts-ignore
pineafan6702cef2022-06-13 17:52:37 +010026 const { renderUser } = interaction.client.logger;
pineafanad54d752022-04-18 19:01:43 +010027 const member = interaction.options.getMember("user") as GuildMember;
28 const guild = interaction.guild;
pineafan6702cef2022-06-13 17:52:37 +010029 let config = await client.database.read(guild.id);
pineafanad54d752022-04-18 19:01:43 +010030 await interaction.reply({embeds: [new generateEmojiEmbed()
31 .setEmoji("NUCLEUS.LOADING")
32 .setTitle("Loading")
33 .setStatus("Danger")
34 ], ephemeral: true})
35 let track = 0
36 let generated;
37 const roles = await guild.roles.fetch()
38 let memberRoles = await member.roles
39 while (true) {
40 let data = config.tracks[track]
41 let managed = data.manageableBy.some(element => {return memberRoles.cache.has(element)})
42 let dropdown = new Discord.MessageSelectMenu().addOptions(config.tracks.map((option, index) => {
43 let hasRoleInTrack = option.track.some(element => {return memberRoles.cache.has(element)})
44 return new SelectMenuOption({
45 default: index == track,
46 label: option.name,
47 value: index.toString(),
pineafane625d782022-05-09 18:04:32 +010048 description: option.track.length == 0 ? "No" : addPlural(option.track.length, "role"),
pineafanad54d752022-04-18 19:01:43 +010049 emoji: interaction.client.emojis.resolve(getEmojiByName("TRACKS.SINGLE." + (hasRoleInTrack ? "ACTIVE" : "INACTIVE"), "id"))
50 })
51 })).setCustomId("select").setMaxValues(1)
52 let allowed = []
53 generated = "**Track:** " + data.name + "\n" + "**Member:** " + renderUser(member.user) + "\n"
54 generated += (data.nullable ? "Members do not need a role in this track" : "A role in this track is required") + "\n"
55 generated += (data.retainPrevious ? "When promoted, the user keeps previous roles" : "Members will lose their current role when promoted") + "\n"
56 generated += "\n" + data.track.map((role, index) => {
57 let allow = (roles.get(role).position >= (interaction.member as GuildMember).roles.highest.position) && !managed
58 allowed.push(!allow)
59 return getEmojiByName(generateFromTrack(
60 index,
61 memberRoles.cache.has(role),
62 data.track.length,
63 allow
64 )) + " " +
65 roles.get(role).name + " [<@&" + roles.get(role).id + ">]"
66 }).join("\n")
67 let selected = [];
68 for (let i = 0; i < data.track.length; i++) {
69 if (memberRoles.cache.has(data.track[i])) selected.push(data.track[i])
70 }
71 let conflict = data.retainPrevious ? false : selected.length > 1;
72 let conflictDropdown
73 let currentRoleIndex
74 if (conflict) {
75 generated += `\n\n${getEmojiByName(`PUNISH.WARN.${managed ? "YELLOW" : "RED"}`)} This user has ${selected.length} roles from this track. `
76 conflictDropdown = []
77 if (
78 (roles.get(selected[0]).position < memberRoles.highest.position) || managed
79 ) {
80 generated += `In order to promote or demote this user, you must select which role the member should keep.`
81 selected.forEach(role => {
82 conflictDropdown.push(new SelectMenuOption({
83 label: roles.get(role).name,
84 value: roles.get(role).id,
85 }))
86 })
87 conflictDropdown = [new Discord.MessageSelectMenu()
88 .addOptions(conflictDropdown)
89 .setCustomId("conflict")
90 .setMaxValues(1)
91 .setPlaceholder("Select a role to keep")]
92 } else {
93 generated += "You don't have permission to manage one or more of the users roles, and therefore can't select one to keep."
94 }
95 } else {
96 currentRoleIndex = selected.length == 0 ? -1 : data.track.indexOf(selected[0].toString())
97 }
98 let m = await interaction.editReply({embeds: [new generateEmojiEmbed()
99 .setEmoji("TRACKS.ICON")
100 .setTitle("Tracks")
101 .setDescription(`${generated}`)
102 .setStatus("Success")
103 ], components: [
104 new MessageActionRow().addComponents(dropdown)
105 ]
106 .concat(conflict && conflictDropdown.length ? [new MessageActionRow().addComponents(conflictDropdown)] : [])
107 .concat([
108 new MessageActionRow().addComponents([
109 new MessageButton()
110 .setEmoji(getEmojiByName("CONTROL.UP", "id"))
111 .setLabel("Move up")
112 .setCustomId("promote")
113 .setStyle("SUCCESS")
114 .setDisabled(conflict || currentRoleIndex == 0 || (currentRoleIndex == -1 ? false : !allowed[currentRoleIndex - 1])),
115 new MessageButton()
116 .setEmoji(getEmojiByName("CONTROL.DOWN", "id"))
117 .setLabel("Move down")
118 .setCustomId("demote")
119 .setStyle("DANGER")
120 .setDisabled(conflict || (
121 data.nullable ? currentRoleIndex <= -1 :
122 currentRoleIndex == data.track.length - 1 || currentRoleIndex <= -1
123 ) || !allowed[currentRoleIndex]),
124 ])
125 ])})
126 let component;
127 try {
pineafanc6158ab2022-06-17 16:34:07 +0100128 component = await (m as Message).awaitMessageComponent({time: 300000});
pineafanad54d752022-04-18 19:01:43 +0100129 } catch (e) {
130 return
131 }
132 component.deferUpdate()
133 if (component.customId == "conflict") {
134 let rolesToRemove = selected.filter(role => role != component.values[0])
135 await member.roles.remove(rolesToRemove)
136 } else if (component.customId == "promote") {
137 if (
138 currentRoleIndex == -1 ? allowed[data.track.length - 1] :
139 allowed[currentRoleIndex - 1] && allowed[currentRoleIndex]
140 ) {
141 if (currentRoleIndex == -1) {
142 await member.roles.add(data.track[data.track.length - 1])
143 } else if (currentRoleIndex < data.track.length) {
144 if (!data.retainPrevious) await member.roles.remove(data.track[currentRoleIndex])
145 await member.roles.add(data.track[currentRoleIndex - 1])
146 }
147 }
148 } else if (component.customId == "demote") {
149 if(allowed[currentRoleIndex]) {
150 if (currentRoleIndex == data.track.length - 1) {
151 if (data.nullable) await member.roles.remove(data.track[currentRoleIndex])
152 } else if (currentRoleIndex > -1) {
153 await member.roles.remove(data.track[currentRoleIndex])
154 await member.roles.add(data.track[currentRoleIndex + 1])
155 }
156 }
157 } else if (component.customId == "select") {
158 track = component.values[0]
159 }
160 }
161}
162
pineafan6702cef2022-06-13 17:52:37 +0100163const check = async (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
PineappleFan19b002b2022-05-19 11:54:01 +0100164 let member = (interaction.member as GuildMember)
pineafanad54d752022-04-18 19:01:43 +0100165 // Allow the owner to promote anyone
PineappleFan19b002b2022-05-19 11:54:01 +0100166 if (member.id == interaction.guild.ownerId) return true
pineafan6702cef2022-06-13 17:52:37 +0100167 // Check if the user can manage any of the tracks
168 // @ts-ignore
169 let tracks = (await interaction.client.database.get(interaction.guild.id)).tracks
170 let managed = false
171 tracks.forEach(element => { if (element.track.manageableBy.some(role => member.roles.cache.has(role))) managed = true });
pineafanad54d752022-04-18 19:01:43 +0100172 // Check if the user has manage_roles permission
pineafan6702cef2022-06-13 17:52:37 +0100173 if (!managed && ! member.permissions.has("MANAGE_ROLES")) throw "You do not have the `manage_roles` permission";
pineafanad54d752022-04-18 19:01:43 +0100174 // Allow track
pineafan6702cef2022-06-13 17:52:37 +0100175 return true;
pineafanad54d752022-04-18 19:01:43 +0100176}
177
178export { command };
179export { callback };
pineafanc6158ab2022-06-17 16:34:07 +0100180export { check };
181