blob: c7f441f77cbeb90110fb7b698706649d36288edf [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LoadingEmbed } from "../../utils/defaults.js";
TheCodedProfb5e9d552023-01-29 15:43:26 -05002import Discord, { CommandInteraction, GuildMember, Message, ActionRowBuilder, ButtonBuilder, ButtonStyle, APIMessageComponentEmoji, StringSelectMenuBuilder, MessageComponentInteraction, StringSelectMenuInteraction, StringSelectMenuOptionBuilder } from "discord.js";
TheCodedProff86ba092023-01-27 17:10:07 -05003import type { SlashCommandSubcommandBuilder } from "discord.js";
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";
TheCodedProfb5e9d552023-01-29 15:43:26 -05008import { createVerticalTrack } from "../../utils/createPageIndicator.js";
pineafanad54d752022-04-18 19:01:43 +01009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("track")
13 .setDescription("Moves a user along a role track")
Skyler Grey11236ba2022-08-08 21:13:33 +010014 .addUserOption((option) => option.setName("user").setDescription("The user to manage").setRequired(true));
pineafanad54d752022-04-18 19:01:43 +010015
Skyler Greyc634e2b2022-08-06 17:50:48 +010016const callback = async (interaction: CommandInteraction): Promise<unknown> => {
TheCodedProfb5e9d552023-01-29 15:43:26 -050017 const { renderUser, renderRole} = client.logger;
pineafanad54d752022-04-18 19:01:43 +010018 const member = interaction.options.getMember("user") as GuildMember;
19 const guild = interaction.guild;
pineafan63fc5e22022-08-04 22:04:10 +010020 if (!guild) return;
21 const config = await client.database.guilds.read(guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +010022 await interaction.reply({ embeds: LoadingEmbed, ephemeral: true });
pineafan63fc5e22022-08-04 22:04:10 +010023 let track = 0;
pineafanad54d752022-04-18 19:01:43 +010024 let generated;
pineafan63fc5e22022-08-04 22:04:10 +010025 const roles = await guild.roles.fetch();
Skyler Greyc634e2b2022-08-06 17:50:48 +010026 const memberRoles = member.roles;
pineafan63fc5e22022-08-04 22:04:10 +010027 let managed: boolean;
Skyler Greyad002172022-08-16 18:48:26 +010028 let timedOut = false;
29 while (!timedOut) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -050030 const data = config.tracks[track]!;
PineaFan638eb132023-01-19 10:41:22 +000031 if (data.manageableBy.length)
Skyler Grey75ea9172022-08-06 10:22:23 +010032 managed = data.manageableBy.some((element: string) => {
33 return memberRoles.cache.has(element);
pineafan63fc5e22022-08-04 22:04:10 +010034 });
Skyler Grey75ea9172022-08-06 10:22:23 +010035 else managed = false;
TheCodedProf2ec2ba62023-01-18 22:26:20 -050036 const dropdown = new Discord.StringSelectMenuBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +010037 .addOptions(
38 config.tracks.map((option, index) => {
TheCodedProfb5e9d552023-01-29 15:43:26 -050039 const hasRoleInTrack: boolean = option.track.some((element: string) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010040 return memberRoles.cache.has(element);
41 });
TheCodedProfb5e9d552023-01-29 15:43:26 -050042 return new StringSelectMenuOptionBuilder({
Skyler Grey75ea9172022-08-06 10:22:23 +010043 default: index === track,
44 label: option.name,
45 value: index.toString(),
Skyler Grey11236ba2022-08-08 21:13:33 +010046 description: option.track.length === 0 ? "No" : addPlural(option.track.length, "role"),
Skyler Grey75ea9172022-08-06 10:22:23 +010047 emoji: client.emojis.resolve(
Skyler Grey11236ba2022-08-08 21:13:33 +010048 getEmojiByName("TRACKS.SINGLE." + (hasRoleInTrack ? "ACTIVE" : "INACTIVE"), "id")
TheCodedProf2ec2ba62023-01-18 22:26:20 -050049 ) as APIMessageComponentEmoji
Skyler Grey75ea9172022-08-06 10:22:23 +010050 });
51 })
52 )
53 .setCustomId("select")
54 .setMaxValues(1);
TheCodedProf2ec2ba62023-01-18 22:26:20 -050055 const allowed: boolean[] = [];
Skyler Grey11236ba2022-08-08 21:13:33 +010056 generated = "**Track:** " + data.name + "\n" + "**Member:** " + renderUser(member.user) + "\n";
Skyler Grey75ea9172022-08-06 10:22:23 +010057 generated +=
Skyler Grey11236ba2022-08-08 21:13:33 +010058 (data.nullable ? "Members do not need a role in this track" : "A role in this track is required") + "\n";
Skyler Grey75ea9172022-08-06 10:22:23 +010059 generated +=
60 (data.retainPrevious
61 ? "When promoted, the user keeps previous roles"
62 : "Members will lose their current role when promoted") + "\n";
TheCodedProfb5e9d552023-01-29 15:43:26 -050063 for (const role of data.track) {
64 const disabled: boolean =
65 roles.get(role)!.position >= (interaction.member as GuildMember).roles.highest.position && !managed;
66 allowed.push(!disabled)
67 }
68 generated += "\n" + createVerticalTrack(
69 data.track.map((role) => renderRole(roles.get(role)!)),
70 data.track.map((role) => memberRoles.cache.has(role)),
71 allowed.map((allow) => !allow)
72 );
pineafan63fc5e22022-08-04 22:04:10 +010073 const selected = [];
Skyler Greyc634e2b2022-08-06 17:50:48 +010074 for (const position of data.track) {
75 if (memberRoles.cache.has(position)) selected.push(position);
pineafanad54d752022-04-18 19:01:43 +010076 }
pineafan63fc5e22022-08-04 22:04:10 +010077 const conflict = data.retainPrevious ? false : selected.length > 1;
TheCodedProf2ec2ba62023-01-18 22:26:20 -050078 let conflictDropdown: StringSelectMenuBuilder[] = [];
TheCodedProfb5e9d552023-01-29 15:43:26 -050079 const conflictDropdownOptions: StringSelectMenuOptionBuilder[] = [];
TheCodedProf2ec2ba62023-01-18 22:26:20 -050080 let currentRoleIndex: number = -1;
pineafanad54d752022-04-18 19:01:43 +010081 if (conflict) {
Skyler Grey11236ba2022-08-08 21:13:33 +010082 generated += `\n\n${getEmojiByName(`PUNISH.WARN.${managed ? "YELLOW" : "RED"}`)} This user has ${
83 selected.length
84 } roles from this track. `;
pineafan63fc5e22022-08-04 22:04:10 +010085 conflictDropdown = [];
TheCodedProf2ec2ba62023-01-18 22:26:20 -050086 if (roles.get(selected[0]!)!.position < memberRoles.highest.position || managed) {
Skyler Grey75ea9172022-08-06 10:22:23 +010087 generated +=
88 "In order to promote or demote this user, you must select which role the member should keep.";
89 selected.forEach((role) => {
TheCodedProf2ec2ba62023-01-18 22:26:20 -050090 conflictDropdownOptions.push(
TheCodedProfb5e9d552023-01-29 15:43:26 -050091 new StringSelectMenuOptionBuilder()
92 .setLabel(roles.get(role)!.name)
93 .setValue(roles.get(role)!.id)
Skyler Grey75ea9172022-08-06 10:22:23 +010094 );
pineafan63fc5e22022-08-04 22:04:10 +010095 });
Skyler Grey75ea9172022-08-06 10:22:23 +010096 conflictDropdown = [
TheCodedProf2ec2ba62023-01-18 22:26:20 -050097 new Discord.StringSelectMenuBuilder()
98 .addOptions(conflictDropdownOptions)
Skyler Grey75ea9172022-08-06 10:22:23 +010099 .setCustomId("conflict")
100 .setMaxValues(1)
101 .setPlaceholder("Select a role to keep")
102 ];
pineafanad54d752022-04-18 19:01:43 +0100103 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100104 generated +=
105 "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 +0100106 }
107 } else {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500108 currentRoleIndex = selected.length === 0 ? -1 : data.track.indexOf(selected[0]!.toString());
pineafanad54d752022-04-18 19:01:43 +0100109 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 const m = (await interaction.editReply({
111 embeds: [
112 new EmojiEmbed()
113 .setEmoji("TRACKS.ICON")
114 .setTitle("Tracks")
115 .setDescription(`${generated}`)
116 .setStatus("Success")
117 ],
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500118 components: [new ActionRowBuilder<StringSelectMenuBuilder | ButtonBuilder>().addComponents(dropdown)]
Skyler Grey75ea9172022-08-06 10:22:23 +0100119 .concat(
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500120 conflict && conflictDropdown.length ? [new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(conflictDropdown)] : []
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 )
122 .concat([
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500123 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400124 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100125 .setEmoji(getEmojiByName("CONTROL.UP", "id"))
126 .setLabel("Move up")
127 .setCustomId("promote")
TheCodedProf21c08592022-09-13 14:14:43 -0400128 .setStyle(ButtonStyle.Success)
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 .setDisabled(
130 conflict ||
131 currentRoleIndex === 0 ||
Skyler Grey11236ba2022-08-08 21:13:33 +0100132 (currentRoleIndex === -1 ? false : !allowed[currentRoleIndex - 1])
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 ),
TheCodedProf21c08592022-09-13 14:14:43 -0400134 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100135 .setEmoji(getEmojiByName("CONTROL.DOWN", "id"))
136 .setLabel("Move down")
137 .setCustomId("demote")
TheCodedProf21c08592022-09-13 14:14:43 -0400138 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100139 .setDisabled(
140 conflict ||
141 (data.nullable
142 ? currentRoleIndex <= -1
Skyler Grey11236ba2022-08-08 21:13:33 +0100143 : currentRoleIndex === data.track.length - 1 || currentRoleIndex <= -1) ||
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 !allowed[currentRoleIndex]
145 )
146 ])
pineafan63fc5e22022-08-04 22:04:10 +0100147 ])
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 })) as Message;
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500149 let component: MessageComponentInteraction;
pineafanad54d752022-04-18 19:01:43 +0100150 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000151 component = await m.awaitMessageComponent({
152 time: 300000,
TheCodedProf267563a2023-01-21 17:00:57 -0500153 filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id }
PineaFan0d06edc2023-01-17 22:10:31 +0000154 });
pineafanad54d752022-04-18 19:01:43 +0100155 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100156 timedOut = true;
157 continue;
pineafanad54d752022-04-18 19:01:43 +0100158 }
pineafan63fc5e22022-08-04 22:04:10 +0100159 component.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100160 if (component.customId === "conflict") {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500161 const rolesToRemove = selected.filter((role) => role !== (component as StringSelectMenuInteraction).values[0]);
pineafan63fc5e22022-08-04 22:04:10 +0100162 await member.roles.remove(rolesToRemove);
pineafane23c4ec2022-07-27 21:56:27 +0100163 } else if (component.customId === "promote") {
pineafanad54d752022-04-18 19:01:43 +0100164 if (
Skyler Grey75ea9172022-08-06 10:22:23 +0100165 currentRoleIndex === -1
166 ? allowed[data.track.length - 1]
167 : allowed[currentRoleIndex - 1] && allowed[currentRoleIndex]
pineafanad54d752022-04-18 19:01:43 +0100168 ) {
pineafane23c4ec2022-07-27 21:56:27 +0100169 if (currentRoleIndex === -1) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500170 await member.roles.add(data.track[data.track.length - 1]!);
pineafanad54d752022-04-18 19:01:43 +0100171 } else if (currentRoleIndex < data.track.length) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500172 if (!data.retainPrevious) await member.roles.remove(data.track[currentRoleIndex]!);
173 await member.roles.add(data.track[currentRoleIndex - 1]!);
pineafanad54d752022-04-18 19:01:43 +0100174 }
175 }
pineafane23c4ec2022-07-27 21:56:27 +0100176 } else if (component.customId === "demote") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100177 if (allowed[currentRoleIndex]) {
pineafane23c4ec2022-07-27 21:56:27 +0100178 if (currentRoleIndex === data.track.length - 1) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500179 if (data.nullable) await member.roles.remove(data.track[currentRoleIndex]!);
pineafanad54d752022-04-18 19:01:43 +0100180 } else if (currentRoleIndex > -1) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500181 await member.roles.remove(data.track[currentRoleIndex]!);
182 await member.roles.add(data.track[currentRoleIndex + 1]!);
pineafanad54d752022-04-18 19:01:43 +0100183 }
184 }
pineafane23c4ec2022-07-27 21:56:27 +0100185 } else if (component.customId === "select") {
PineaFan638eb132023-01-19 10:41:22 +0000186 track = parseInt((component as StringSelectMenuInteraction).values[0]!);
pineafanad54d752022-04-18 19:01:43 +0100187 }
188 }
pineafan63fc5e22022-08-04 22:04:10 +0100189};
pineafanad54d752022-04-18 19:01:43 +0100190
TheCodedProff86ba092023-01-27 17:10:07 -0500191const check = async (interaction: CommandInteraction, _partial: boolean = false) => {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500192 const tracks = (await client.database.guilds.read(interaction.guild!.id)).tracks;
PineaFan5d98a4b2023-01-19 16:15:47 +0000193 if (tracks.length === 0) return "This server does not have any tracks";
Skyler Grey75ea9172022-08-06 10:22:23 +0100194 const member = interaction.member as GuildMember;
pineafanad54d752022-04-18 19:01:43 +0100195 // Allow the owner to promote anyone
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500196 if (member.id === interaction.guild!.ownerId) return true;
pineafan6702cef2022-06-13 17:52:37 +0100197 // Check if the user can manage any of the tracks
pineafan63fc5e22022-08-04 22:04:10 +0100198 let managed = false;
PineaFan638eb132023-01-19 10:41:22 +0000199 const memberRoles = member.roles.cache.map((r) => r.id)
pineafanc1c18792022-08-03 21:41:36 +0100200 for (const element of tracks) {
PineaFan638eb132023-01-19 10:41:22 +0000201 if (!element.manageableBy.length) continue;
202 if (!element.manageableBy.some((role: string) => memberRoles.includes(role))) continue;
pineafanc1c18792022-08-03 21:41:36 +0100203 managed = true;
204 break;
pineafan63fc5e22022-08-04 22:04:10 +0100205 }
pineafanad54d752022-04-18 19:01:43 +0100206 // Check if the user has manage_roles permission
PineaFan5d98a4b2023-01-19 16:15:47 +0000207 if (!managed && !member.permissions.has("ManageRoles")) return "You do not have the *Manage Roles* permission";
pineafanad54d752022-04-18 19:01:43 +0100208 // Allow track
pineafan6702cef2022-06-13 17:52:37 +0100209 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100210};
pineafanad54d752022-04-18 19:01:43 +0100211
212export { command };
213export { callback };
pineafanc6158ab2022-06-17 16:34:07 +0100214export { check };