blob: 48733fcd8174fe6f72e8b8087bc4281c7e393567 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
pineafanad54d752022-04-18 19:01:43 +01002import Discord, { CommandInteraction, GuildMember, Message, MessageActionRow, MessageButton } from "discord.js";
3import { SelectMenuOption, SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan63fc5e22022-08-04 22:04:10 +01004// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5// @ts-ignore
pineafanad54d752022-04-18 19:01:43 +01006import { WrappedCheck } from "jshaiku";
pineafan4edb7762022-06-26 19:21:04 +01007import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafanad54d752022-04-18 19:01:43 +01008import getEmojiByName from "../../utils/getEmojiByName.js";
pineafane625d782022-05-09 18:04:32 +01009import addPlural from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +010010import client from "../../utils/client.js";
pineafanad54d752022-04-18 19:01:43 +010011
12const command = (builder: SlashCommandSubcommandBuilder) =>
13 builder
pineafan63fc5e22022-08-04 22:04:10 +010014 .setName("track")
15 .setDescription("Moves a user along a role track")
16 .addUserOption(option => option.setName("user").setDescription("The user to manage").setRequired(true));
pineafanad54d752022-04-18 19:01:43 +010017
pineafan63fc5e22022-08-04 22:04:10 +010018const 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};
pineafanad54d752022-04-18 19:01:43 +010026
pineafan63fc5e22022-08-04 22:04:10 +010027const callback = async (interaction: CommandInteraction): Promise<void | unknown> => {
PineappleFanb3dd83c2022-06-17 10:53:48 +010028 const { renderUser } = client.logger;
pineafanad54d752022-04-18 19:01:43 +010029 const member = interaction.options.getMember("user") as GuildMember;
30 const guild = interaction.guild;
pineafan63fc5e22022-08-04 22:04:10 +010031 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;
pineafanad54d752022-04-18 19:01:43 +010035 let generated;
pineafan63fc5e22022-08-04 22:04:10 +010036 const roles = await guild.roles.fetch();
37 const memberRoles = await member.roles;
38 let managed: boolean;
pineafanad54d752022-04-18 19:01:43 +010039 while (true) {
pineafan63fc5e22022-08-04 22:04:10 +010040 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);});
pineafanad54d752022-04-18 19:01:43 +010045 return new SelectMenuOption({
pineafane23c4ec2022-07-27 21:56:27 +010046 default: index === track,
pineafanad54d752022-04-18 19:01:43 +010047 label: option.name,
48 value: index.toString(),
pineafane23c4ec2022-07-27 21:56:27 +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"))
pineafan63fc5e22022-08-04 22:04:10 +010051 });
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";
pineafanad54d752022-04-18 19:01:43 +010057 generated += "\n" + data.track.map((role, index) => {
pineafan63fc5e22022-08-04 22:04:10 +010058 const allow = (roles.get(role).position >= (interaction.member as GuildMember).roles.highest.position) && !managed;
59 allowed.push(!allow);
pineafanad54d752022-04-18 19:01:43 +010060 return getEmojiByName(generateFromTrack(
61 index,
62 memberRoles.cache.has(role),
63 data.track.length,
64 allow
65 )) + " " +
pineafan63fc5e22022-08-04 22:04:10 +010066 roles.get(role).name + " [<@&" + roles.get(role).id + ">]";
67 }).join("\n");
68 const selected = [];
pineafanad54d752022-04-18 19:01:43 +010069 for (let i = 0; i < data.track.length; i++) {
pineafan63fc5e22022-08-04 22:04:10 +010070 if (memberRoles.cache.has(data.track[i])) selected.push(data.track[i]);
pineafanad54d752022-04-18 19:01:43 +010071 }
pineafan63fc5e22022-08-04 22:04:10 +010072 const conflict = data.retainPrevious ? false : selected.length > 1;
73 let conflictDropdown;
74 let currentRoleIndex;
pineafanad54d752022-04-18 19:01:43 +010075 if (conflict) {
pineafan63fc5e22022-08-04 22:04:10 +010076 generated += `\n\n${getEmojiByName(`PUNISH.WARN.${managed ? "YELLOW" : "RED"}`)} This user has ${selected.length} roles from this track. `;
77 conflictDropdown = [];
pineafanad54d752022-04-18 19:01:43 +010078 if (
79 (roles.get(selected[0]).position < memberRoles.highest.position) || managed
80 ) {
pineafan63fc5e22022-08-04 22:04:10 +010081 generated += "In order to promote or demote this user, you must select which role the member should keep.";
pineafanad54d752022-04-18 19:01:43 +010082 selected.forEach(role => {
83 conflictDropdown.push(new SelectMenuOption({
84 label: roles.get(role).name,
pineafan63fc5e22022-08-04 22:04:10 +010085 value: roles.get(role).id
86 }));
87 });
pineafanad54d752022-04-18 19:01:43 +010088 conflictDropdown = [new Discord.MessageSelectMenu()
89 .addOptions(conflictDropdown)
90 .setCustomId("conflict")
91 .setMaxValues(1)
pineafan63fc5e22022-08-04 22:04:10 +010092 .setPlaceholder("Select a role to keep")];
pineafanad54d752022-04-18 19:01:43 +010093 } else {
pineafan63fc5e22022-08-04 22:04:10 +010094 generated += "You don't have permission to manage one or more of the users roles, and therefore can't select one to keep.";
pineafanad54d752022-04-18 19:01:43 +010095 }
96 } else {
pineafan63fc5e22022-08-04 22:04:10 +010097 currentRoleIndex = selected.length === 0 ? -1 : data.track.indexOf(selected[0].toString());
pineafanad54d752022-04-18 19:01:43 +010098 }
pineafan63fc5e22022-08-04 22:04:10 +010099 const 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 ]
pineafan63fc5e22022-08-04 22:04:10 +0100107 .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 ])});
pineafanad54d752022-04-18 19:01:43 +0100127 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) {
pineafan63fc5e22022-08-04 22:04:10 +0100131 return;
pineafanad54d752022-04-18 19:01:43 +0100132 }
pineafan63fc5e22022-08-04 22:04:10 +0100133 component.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100134 if (component.customId === "conflict") {
pineafan63fc5e22022-08-04 22:04:10 +0100135 const rolesToRemove = selected.filter(role => role !== component.values[0]);
136 await member.roles.remove(rolesToRemove);
pineafane23c4ec2022-07-27 21:56:27 +0100137 } else if (component.customId === "promote") {
pineafanad54d752022-04-18 19:01:43 +0100138 if (
pineafane23c4ec2022-07-27 21:56:27 +0100139 currentRoleIndex === -1 ? allowed[data.track.length - 1] :
pineafan63fc5e22022-08-04 22:04:10 +0100140 allowed[currentRoleIndex - 1] && allowed[currentRoleIndex]
pineafanad54d752022-04-18 19:01:43 +0100141 ) {
pineafane23c4ec2022-07-27 21:56:27 +0100142 if (currentRoleIndex === -1) {
pineafan63fc5e22022-08-04 22:04:10 +0100143 await member.roles.add(data.track[data.track.length - 1]);
pineafanad54d752022-04-18 19:01:43 +0100144 } else if (currentRoleIndex < data.track.length) {
pineafan63fc5e22022-08-04 22:04:10 +0100145 if (!data.retainPrevious) await member.roles.remove(data.track[currentRoleIndex]);
146 await member.roles.add(data.track[currentRoleIndex - 1]);
pineafanad54d752022-04-18 19:01:43 +0100147 }
148 }
pineafane23c4ec2022-07-27 21:56:27 +0100149 } else if (component.customId === "demote") {
pineafanad54d752022-04-18 19:01:43 +0100150 if(allowed[currentRoleIndex]) {
pineafane23c4ec2022-07-27 21:56:27 +0100151 if (currentRoleIndex === data.track.length - 1) {
pineafan63fc5e22022-08-04 22:04:10 +0100152 if (data.nullable) await member.roles.remove(data.track[currentRoleIndex]);
pineafanad54d752022-04-18 19:01:43 +0100153 } else if (currentRoleIndex > -1) {
pineafan63fc5e22022-08-04 22:04:10 +0100154 await member.roles.remove(data.track[currentRoleIndex]);
155 await member.roles.add(data.track[currentRoleIndex + 1]);
pineafanad54d752022-04-18 19:01:43 +0100156 }
157 }
pineafane23c4ec2022-07-27 21:56:27 +0100158 } else if (component.customId === "select") {
pineafan63fc5e22022-08-04 22:04:10 +0100159 track = component.values[0];
pineafanad54d752022-04-18 19:01:43 +0100160 }
161 }
pineafan63fc5e22022-08-04 22:04:10 +0100162};
pineafanad54d752022-04-18 19:01:43 +0100163
pineafan63fc5e22022-08-04 22:04:10 +0100164const check = async (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
165 const tracks = (await client.database.guilds.read(interaction.guild.id)).tracks;
166 if (tracks.length === 0) throw "This server does not have any tracks";
167 const member = (interaction.member as GuildMember);
pineafanad54d752022-04-18 19:01:43 +0100168 // Allow the owner to promote anyone
pineafan63fc5e22022-08-04 22:04:10 +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
pineafan63fc5e22022-08-04 22:04:10 +0100171 let managed = false;
pineafanc1c18792022-08-03 21:41:36 +0100172 for (const element of tracks) {
pineafan63fc5e22022-08-04 22:04:10 +0100173 if (!element.track.manageableBy) continue;
174 if (!element.track.manageableBy.some(role => member.roles.cache.has(role))) continue;
pineafanc1c18792022-08-03 21:41:36 +0100175 managed = true;
176 break;
pineafan63fc5e22022-08-04 22:04:10 +0100177 }
pineafanad54d752022-04-18 19:01:43 +0100178 // Check if the user has manage_roles permission
pineafane23c4ec2022-07-27 21:56:27 +0100179 if (!managed && ! member.permissions.has("MANAGE_ROLES")) throw "You do not have the *Manage Roles* permission";
pineafanad54d752022-04-18 19:01:43 +0100180 // Allow track
pineafan6702cef2022-06-13 17:52:37 +0100181 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100182};
pineafanad54d752022-04-18 19:01:43 +0100183
184export { command };
185export { callback };
pineafanc6158ab2022-06-17 16:34:07 +0100186export { check };
187