blob: da332daed6b8dc878d7383c011d03cd33c867408 [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";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafanad54d752022-04-18 19:01:43 +01005import 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
pineafan4edb7762022-06-26 19:21:04 +010024const callback = async (interaction: CommandInteraction): Promise<any> => {
PineappleFanb3dd83c2022-06-17 10:53:48 +010025 const { renderUser } = client.logger;
pineafanad54d752022-04-18 19:01:43 +010026 const member = interaction.options.getMember("user") as GuildMember;
27 const guild = interaction.guild;
pineafan4edb7762022-06-26 19:21:04 +010028 let config = await client.database.guilds.read(guild.id);
29 await interaction.reply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010030 .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
pineafan4edb7762022-06-26 19:21:04 +010038 let managed
pineafanad54d752022-04-18 19:01:43 +010039 while (true) {
40 let data = config.tracks[track]
pineafan4edb7762022-06-26 19:21:04 +010041 if (data.manageableBy !== undefined) managed = data.manageableBy.some(element => {return memberRoles.cache.has(element)})
42 else managed = false
pineafanad54d752022-04-18 19:01:43 +010043 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(),
pineafane625d782022-05-09 18:04:32 +010049 description: option.track.length == 0 ? "No" : addPlural(option.track.length, "role"),
PineappleFanb3dd83c2022-06-17 10:53:48 +010050 emoji: client.emojis.resolve(getEmojiByName("TRACKS.SINGLE." + (hasRoleInTrack ? "ACTIVE" : "INACTIVE"), "id"))
pineafanad54d752022-04-18 19:01:43 +010051 })
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 }
pineafan4edb7762022-06-26 19:21:04 +010099 let m = await interaction.editReply({embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +0100100 .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 {
pineafanc6158ab2022-06-17 16:34:07 +0100129 component = await (m as Message).awaitMessageComponent({time: 300000});
pineafanad54d752022-04-18 19:01:43 +0100130 } 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
pineafan6702cef2022-06-13 17:52:37 +0100164const check = async (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan4edb7762022-06-26 19:21:04 +0100165 let tracks = (await client.database.guilds.read(interaction.guild.id)).tracks
166 if (!tracks) throw "This server does not have any tracks"
PineappleFan19b002b2022-05-19 11:54:01 +0100167 let member = (interaction.member as GuildMember)
pineafanad54d752022-04-18 19:01:43 +0100168 // Allow the owner to promote anyone
PineappleFan19b002b2022-05-19 11:54:01 +0100169 if (member.id == interaction.guild.ownerId) return true
pineafan6702cef2022-06-13 17:52:37 +0100170 // Check if the user can manage any of the tracks
pineafan6702cef2022-06-13 17:52:37 +0100171 let managed = false
pineafan4edb7762022-06-26 19:21:04 +0100172 tracks.forEach(element => {
173 if (!element.track.manageableBy) return
174 if (element.track.manageableBy.some(role => member.roles.cache.has(role))) managed = true
175 });
pineafanad54d752022-04-18 19:01:43 +0100176 // Check if the user has manage_roles permission
pineafan4edb7762022-06-26 19:21:04 +0100177 if (!managed && ! member.permissions.has("MANAGE_ROLES")) throw "You do not have the Manage roles permission";
pineafanad54d752022-04-18 19:01:43 +0100178 // Allow track
pineafan6702cef2022-06-13 17:52:37 +0100179 return true;
pineafanad54d752022-04-18 19:01:43 +0100180}
181
182export { command };
183export { callback };
pineafanc6158ab2022-06-17 16:34:07 +0100184export { check };
185