blob: 19b9da28bcc42e26d3d2d8bbbf5c23e2b087586a [file] [log] [blame]
PineaFan0d06edc2023-01-17 22:10:31 +00001import { LoadingEmbed } from "../../utils/defaults.js";
Skyler Greyda16adf2023-03-05 10:22:12 +00002import Discord, {
3 CommandInteraction,
4 GuildMember,
5 Message,
6 ActionRowBuilder,
7 ButtonBuilder,
8 ButtonStyle,
9 APIMessageComponentEmoji,
10 StringSelectMenuBuilder,
11 MessageComponentInteraction,
12 StringSelectMenuInteraction,
13 StringSelectMenuOptionBuilder
14} from "discord.js";
TheCodedProff86ba092023-01-27 17:10:07 -050015import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan4edb7762022-06-26 19:21:04 +010016import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafanad54d752022-04-18 19:01:43 +010017import getEmojiByName from "../../utils/getEmojiByName.js";
pineafane625d782022-05-09 18:04:32 +010018import addPlural from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +010019import client from "../../utils/client.js";
TheCodedProfb5e9d552023-01-29 15:43:26 -050020import { createVerticalTrack } from "../../utils/createPageIndicator.js";
pineafanad54d752022-04-18 19:01:43 +010021
22const command = (builder: SlashCommandSubcommandBuilder) =>
23 builder
pineafan63fc5e22022-08-04 22:04:10 +010024 .setName("track")
25 .setDescription("Moves a user along a role track")
Skyler Grey11236ba2022-08-08 21:13:33 +010026 .addUserOption((option) => option.setName("user").setDescription("The user to manage").setRequired(true));
pineafanad54d752022-04-18 19:01:43 +010027
Skyler Greyc634e2b2022-08-06 17:50:48 +010028const callback = async (interaction: CommandInteraction): Promise<unknown> => {
Skyler Greyda16adf2023-03-05 10:22:12 +000029 const { renderUser, renderRole } = client.logger;
pineafanad54d752022-04-18 19:01:43 +010030 const member = interaction.options.getMember("user") as GuildMember;
31 const guild = interaction.guild;
pineafan63fc5e22022-08-04 22:04:10 +010032 if (!guild) return;
33 const config = await client.database.guilds.read(guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +010034 await interaction.reply({ embeds: LoadingEmbed, ephemeral: true });
pineafan63fc5e22022-08-04 22:04:10 +010035 let track = 0;
pineafanad54d752022-04-18 19:01:43 +010036 let generated;
pineafan63fc5e22022-08-04 22:04:10 +010037 const roles = await guild.roles.fetch();
Skyler Greyc634e2b2022-08-06 17:50:48 +010038 const memberRoles = member.roles;
pineafan63fc5e22022-08-04 22:04:10 +010039 let managed: boolean;
Skyler Greyad002172022-08-16 18:48:26 +010040 let timedOut = false;
41 while (!timedOut) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -050042 const data = config.tracks[track]!;
PineaFan638eb132023-01-19 10:41:22 +000043 if (data.manageableBy.length)
Skyler Grey75ea9172022-08-06 10:22:23 +010044 managed = data.manageableBy.some((element: string) => {
45 return memberRoles.cache.has(element);
pineafan63fc5e22022-08-04 22:04:10 +010046 });
Skyler Grey75ea9172022-08-06 10:22:23 +010047 else managed = false;
TheCodedProf2ec2ba62023-01-18 22:26:20 -050048 const dropdown = new Discord.StringSelectMenuBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +010049 .addOptions(
50 config.tracks.map((option, index) => {
TheCodedProfb5e9d552023-01-29 15:43:26 -050051 const hasRoleInTrack: boolean = option.track.some((element: string) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010052 return memberRoles.cache.has(element);
53 });
TheCodedProfb5e9d552023-01-29 15:43:26 -050054 return new StringSelectMenuOptionBuilder({
Skyler Grey75ea9172022-08-06 10:22:23 +010055 default: index === track,
56 label: option.name,
57 value: index.toString(),
Skyler Grey11236ba2022-08-08 21:13:33 +010058 description: option.track.length === 0 ? "No" : addPlural(option.track.length, "role"),
Skyler Grey75ea9172022-08-06 10:22:23 +010059 emoji: client.emojis.resolve(
Skyler Grey11236ba2022-08-08 21:13:33 +010060 getEmojiByName("TRACKS.SINGLE." + (hasRoleInTrack ? "ACTIVE" : "INACTIVE"), "id")
TheCodedProf2ec2ba62023-01-18 22:26:20 -050061 ) as APIMessageComponentEmoji
Skyler Grey75ea9172022-08-06 10:22:23 +010062 });
63 })
64 )
65 .setCustomId("select")
66 .setMaxValues(1);
TheCodedProf2ec2ba62023-01-18 22:26:20 -050067 const allowed: boolean[] = [];
Skyler Grey11236ba2022-08-08 21:13:33 +010068 generated = "**Track:** " + data.name + "\n" + "**Member:** " + renderUser(member.user) + "\n";
Skyler Grey75ea9172022-08-06 10:22:23 +010069 generated +=
Skyler Grey11236ba2022-08-08 21:13:33 +010070 (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 +010071 generated +=
72 (data.retainPrevious
73 ? "When promoted, the user keeps previous roles"
74 : "Members will lose their current role when promoted") + "\n";
TheCodedProfb5e9d552023-01-29 15:43:26 -050075 for (const role of data.track) {
76 const disabled: boolean =
77 roles.get(role)!.position >= (interaction.member as GuildMember).roles.highest.position && !managed;
Skyler Greyda16adf2023-03-05 10:22:12 +000078 allowed.push(!disabled);
TheCodedProfb5e9d552023-01-29 15:43:26 -050079 }
Skyler Greyda16adf2023-03-05 10:22:12 +000080 generated +=
81 "\n" +
82 createVerticalTrack(
83 data.track.map((role) => renderRole(roles.get(role)!)),
84 data.track.map((role) => memberRoles.cache.has(role)),
85 allowed.map((allow) => !allow)
86 );
pineafan63fc5e22022-08-04 22:04:10 +010087 const selected = [];
Skyler Greyc634e2b2022-08-06 17:50:48 +010088 for (const position of data.track) {
89 if (memberRoles.cache.has(position)) selected.push(position);
pineafanad54d752022-04-18 19:01:43 +010090 }
pineafan63fc5e22022-08-04 22:04:10 +010091 const conflict = data.retainPrevious ? false : selected.length > 1;
TheCodedProf2ec2ba62023-01-18 22:26:20 -050092 let conflictDropdown: StringSelectMenuBuilder[] = [];
TheCodedProfb5e9d552023-01-29 15:43:26 -050093 const conflictDropdownOptions: StringSelectMenuOptionBuilder[] = [];
TheCodedProf2ec2ba62023-01-18 22:26:20 -050094 let currentRoleIndex: number = -1;
pineafanad54d752022-04-18 19:01:43 +010095 if (conflict) {
Skyler Grey11236ba2022-08-08 21:13:33 +010096 generated += `\n\n${getEmojiByName(`PUNISH.WARN.${managed ? "YELLOW" : "RED"}`)} This user has ${
97 selected.length
98 } roles from this track. `;
pineafan63fc5e22022-08-04 22:04:10 +010099 conflictDropdown = [];
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500100 if (roles.get(selected[0]!)!.position < memberRoles.highest.position || managed) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100101 generated +=
102 "In order to promote or demote this user, you must select which role the member should keep.";
103 selected.forEach((role) => {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500104 conflictDropdownOptions.push(
TheCodedProfb5e9d552023-01-29 15:43:26 -0500105 new StringSelectMenuOptionBuilder()
106 .setLabel(roles.get(role)!.name)
107 .setValue(roles.get(role)!.id)
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 );
pineafan63fc5e22022-08-04 22:04:10 +0100109 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 conflictDropdown = [
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500111 new Discord.StringSelectMenuBuilder()
112 .addOptions(conflictDropdownOptions)
Skyler Grey75ea9172022-08-06 10:22:23 +0100113 .setCustomId("conflict")
114 .setMaxValues(1)
115 .setPlaceholder("Select a role to keep")
116 ];
pineafanad54d752022-04-18 19:01:43 +0100117 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 generated +=
119 "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 +0100120 }
121 } else {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500122 currentRoleIndex = selected.length === 0 ? -1 : data.track.indexOf(selected[0]!.toString());
pineafanad54d752022-04-18 19:01:43 +0100123 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100124 const m = (await interaction.editReply({
125 embeds: [
126 new EmojiEmbed()
127 .setEmoji("TRACKS.ICON")
128 .setTitle("Tracks")
129 .setDescription(`${generated}`)
130 .setStatus("Success")
131 ],
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500132 components: [new ActionRowBuilder<StringSelectMenuBuilder | ButtonBuilder>().addComponents(dropdown)]
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 .concat(
Skyler Greyda16adf2023-03-05 10:22:12 +0000134 conflict && conflictDropdown.length
135 ? [new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(conflictDropdown)]
136 : []
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 )
138 .concat([
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500139 new ActionRowBuilder<ButtonBuilder>().addComponents([
TheCodedProf21c08592022-09-13 14:14:43 -0400140 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 .setEmoji(getEmojiByName("CONTROL.UP", "id"))
142 .setLabel("Move up")
143 .setCustomId("promote")
TheCodedProf21c08592022-09-13 14:14:43 -0400144 .setStyle(ButtonStyle.Success)
Skyler Grey75ea9172022-08-06 10:22:23 +0100145 .setDisabled(
146 conflict ||
147 currentRoleIndex === 0 ||
Skyler Grey11236ba2022-08-08 21:13:33 +0100148 (currentRoleIndex === -1 ? false : !allowed[currentRoleIndex - 1])
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 ),
TheCodedProf21c08592022-09-13 14:14:43 -0400150 new ButtonBuilder()
Skyler Grey75ea9172022-08-06 10:22:23 +0100151 .setEmoji(getEmojiByName("CONTROL.DOWN", "id"))
152 .setLabel("Move down")
153 .setCustomId("demote")
TheCodedProf21c08592022-09-13 14:14:43 -0400154 .setStyle(ButtonStyle.Danger)
Skyler Grey75ea9172022-08-06 10:22:23 +0100155 .setDisabled(
156 conflict ||
157 (data.nullable
158 ? currentRoleIndex <= -1
Skyler Grey11236ba2022-08-08 21:13:33 +0100159 : currentRoleIndex === data.track.length - 1 || currentRoleIndex <= -1) ||
Skyler Grey75ea9172022-08-06 10:22:23 +0100160 !allowed[currentRoleIndex]
161 )
162 ])
pineafan63fc5e22022-08-04 22:04:10 +0100163 ])
Skyler Grey75ea9172022-08-06 10:22:23 +0100164 })) as Message;
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500165 let component: MessageComponentInteraction;
pineafanad54d752022-04-18 19:01:43 +0100166 try {
PineaFan0d06edc2023-01-17 22:10:31 +0000167 component = await m.awaitMessageComponent({
168 time: 300000,
Skyler Greyda16adf2023-03-05 10:22:12 +0000169 filter: (i) => {
170 return (
171 i.user.id === interaction.user.id &&
172 i.channel!.id === interaction.channel!.id &&
173 i.message.id === m.id
174 );
175 }
PineaFan0d06edc2023-01-17 22:10:31 +0000176 });
pineafanad54d752022-04-18 19:01:43 +0100177 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100178 timedOut = true;
179 continue;
pineafanad54d752022-04-18 19:01:43 +0100180 }
pineafan63fc5e22022-08-04 22:04:10 +0100181 component.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100182 if (component.customId === "conflict") {
Skyler Greyda16adf2023-03-05 10:22:12 +0000183 const rolesToRemove = selected.filter(
184 (role) => role !== (component as StringSelectMenuInteraction).values[0]
185 );
pineafan63fc5e22022-08-04 22:04:10 +0100186 await member.roles.remove(rolesToRemove);
pineafane23c4ec2022-07-27 21:56:27 +0100187 } else if (component.customId === "promote") {
pineafanad54d752022-04-18 19:01:43 +0100188 if (
Skyler Grey75ea9172022-08-06 10:22:23 +0100189 currentRoleIndex === -1
190 ? allowed[data.track.length - 1]
191 : allowed[currentRoleIndex - 1] && allowed[currentRoleIndex]
pineafanad54d752022-04-18 19:01:43 +0100192 ) {
pineafane23c4ec2022-07-27 21:56:27 +0100193 if (currentRoleIndex === -1) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500194 await member.roles.add(data.track[data.track.length - 1]!);
pineafanad54d752022-04-18 19:01:43 +0100195 } else if (currentRoleIndex < data.track.length) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500196 if (!data.retainPrevious) await member.roles.remove(data.track[currentRoleIndex]!);
197 await member.roles.add(data.track[currentRoleIndex - 1]!);
pineafanad54d752022-04-18 19:01:43 +0100198 }
199 }
pineafane23c4ec2022-07-27 21:56:27 +0100200 } else if (component.customId === "demote") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100201 if (allowed[currentRoleIndex]) {
pineafane23c4ec2022-07-27 21:56:27 +0100202 if (currentRoleIndex === data.track.length - 1) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500203 if (data.nullable) await member.roles.remove(data.track[currentRoleIndex]!);
pineafanad54d752022-04-18 19:01:43 +0100204 } else if (currentRoleIndex > -1) {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500205 await member.roles.remove(data.track[currentRoleIndex]!);
206 await member.roles.add(data.track[currentRoleIndex + 1]!);
pineafanad54d752022-04-18 19:01:43 +0100207 }
208 }
pineafane23c4ec2022-07-27 21:56:27 +0100209 } else if (component.customId === "select") {
PineaFan638eb132023-01-19 10:41:22 +0000210 track = parseInt((component as StringSelectMenuInteraction).values[0]!);
pineafanad54d752022-04-18 19:01:43 +0100211 }
212 }
pineafan63fc5e22022-08-04 22:04:10 +0100213};
pineafanad54d752022-04-18 19:01:43 +0100214
TheCodedProff86ba092023-01-27 17:10:07 -0500215const check = async (interaction: CommandInteraction, _partial: boolean = false) => {
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500216 const tracks = (await client.database.guilds.read(interaction.guild!.id)).tracks;
PineaFan5d98a4b2023-01-19 16:15:47 +0000217 if (tracks.length === 0) return "This server does not have any tracks";
Skyler Grey75ea9172022-08-06 10:22:23 +0100218 const member = interaction.member as GuildMember;
pineafanad54d752022-04-18 19:01:43 +0100219 // Allow the owner to promote anyone
TheCodedProf2ec2ba62023-01-18 22:26:20 -0500220 if (member.id === interaction.guild!.ownerId) return true;
pineafan6702cef2022-06-13 17:52:37 +0100221 // Check if the user can manage any of the tracks
pineafan63fc5e22022-08-04 22:04:10 +0100222 let managed = false;
Skyler Greyda16adf2023-03-05 10:22:12 +0000223 const memberRoles = member.roles.cache.map((r) => r.id);
pineafanc1c18792022-08-03 21:41:36 +0100224 for (const element of tracks) {
PineaFan638eb132023-01-19 10:41:22 +0000225 if (!element.manageableBy.length) continue;
226 if (!element.manageableBy.some((role: string) => memberRoles.includes(role))) continue;
pineafanc1c18792022-08-03 21:41:36 +0100227 managed = true;
228 break;
pineafan63fc5e22022-08-04 22:04:10 +0100229 }
pineafanad54d752022-04-18 19:01:43 +0100230 // Check if the user has manage_roles permission
PineaFan5d98a4b2023-01-19 16:15:47 +0000231 if (!managed && !member.permissions.has("ManageRoles")) return "You do not have the *Manage Roles* permission";
pineafanad54d752022-04-18 19:01:43 +0100232 // Allow track
pineafan6702cef2022-06-13 17:52:37 +0100233 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100234};
pineafanad54d752022-04-18 19:01:43 +0100235
236export { command };
237export { callback };
pineafanc6158ab2022-06-17 16:34:07 +0100238export { check };