blob: da6288098180f687a7e7b20adbcf03509fdd36c8 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
Skyler Grey11236ba2022-08-08 21:13:33 +01002import Discord, { CommandInteraction, GuildMember, Message, MessageActionRow, MessageButton } from "discord.js";
3import { SelectMenuOption, SlashCommandSubcommandBuilder } from "@discordjs/builders";
Skyler Grey75ea9172022-08-06 10:22:23 +01004// @ts-expect-error
pineafanad54d752022-04-18 19:01:43 +01005import { WrappedCheck } from "jshaiku";
pineafan4edb7762022-06-26 19:21:04 +01006import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafanad54d752022-04-18 19:01:43 +01007import getEmojiByName from "../../utils/getEmojiByName.js";
pineafane625d782022-05-09 18:04:32 +01008import addPlural from "../../utils/plurals.js";
pineafan6702cef2022-06-13 17:52:37 +01009import client from "../../utils/client.js";
pineafanad54d752022-04-18 19:01:43 +010010
11const command = (builder: SlashCommandSubcommandBuilder) =>
12 builder
pineafan63fc5e22022-08-04 22:04:10 +010013 .setName("track")
14 .setDescription("Moves a user along a role track")
Skyler Grey11236ba2022-08-08 21:13:33 +010015 .addUserOption((option) => option.setName("user").setDescription("The user to manage").setRequired(true));
pineafanad54d752022-04-18 19:01:43 +010016
Skyler Grey11236ba2022-08-08 21:13:33 +010017const generateFromTrack = (position: number, active: string | boolean, size: number, disabled: string | boolean) => {
pineafan63fc5e22022-08-04 22:04:10 +010018 active = active ? "ACTIVE" : "INACTIVE";
19 disabled = disabled ? "GREY." : "";
Skyler Grey11236ba2022-08-08 21:13:33 +010020 if (position === 0 && size === 1) return "TRACKS.SINGLE." + disabled + active;
21 if (position === size - 1) return "TRACKS.VERTICAL.BOTTOM." + disabled + active;
pineafan63fc5e22022-08-04 22:04:10 +010022 if (position === 0) return "TRACKS.VERTICAL.TOP." + disabled + active;
23 return "TRACKS.VERTICAL.MIDDLE." + disabled + active;
24};
pineafanad54d752022-04-18 19:01:43 +010025
Skyler Greyc634e2b2022-08-06 17:50:48 +010026const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineappleFanb3dd83c2022-06-17 10:53:48 +010027 const { renderUser } = client.logger;
pineafanad54d752022-04-18 19:01:43 +010028 const member = interaction.options.getMember("user") as GuildMember;
29 const guild = interaction.guild;
pineafan63fc5e22022-08-04 22:04:10 +010030 if (!guild) return;
31 const config = await client.database.guilds.read(guild.id);
Skyler Grey75ea9172022-08-06 10:22:23 +010032 await interaction.reply({ embeds: LoadingEmbed, ephemeral: true });
pineafan63fc5e22022-08-04 22:04:10 +010033 let track = 0;
pineafanad54d752022-04-18 19:01:43 +010034 let generated;
pineafan63fc5e22022-08-04 22:04:10 +010035 const roles = await guild.roles.fetch();
Skyler Greyc634e2b2022-08-06 17:50:48 +010036 const memberRoles = member.roles;
pineafan63fc5e22022-08-04 22:04:10 +010037 let managed: boolean;
pineafanad54d752022-04-18 19:01:43 +010038 while (true) {
pineafan63fc5e22022-08-04 22:04:10 +010039 const data = config.tracks[track];
Skyler Grey75ea9172022-08-06 10:22:23 +010040 if (data.manageableBy !== undefined)
41 managed = data.manageableBy.some((element: string) => {
42 return memberRoles.cache.has(element);
pineafan63fc5e22022-08-04 22:04:10 +010043 });
Skyler Grey75ea9172022-08-06 10:22:23 +010044 else managed = false;
45 const dropdown = new Discord.MessageSelectMenu()
46 .addOptions(
47 config.tracks.map((option, index) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010048 const hasRoleInTrack = option.track.some((element: string) => {
49 return memberRoles.cache.has(element);
50 });
Skyler Grey75ea9172022-08-06 10:22:23 +010051 return new SelectMenuOption({
52 default: index === track,
53 label: option.name,
54 value: index.toString(),
Skyler Grey11236ba2022-08-08 21:13:33 +010055 description: option.track.length === 0 ? "No" : addPlural(option.track.length, "role"),
Skyler Grey75ea9172022-08-06 10:22:23 +010056 emoji: client.emojis.resolve(
Skyler Grey11236ba2022-08-08 21:13:33 +010057 getEmojiByName("TRACKS.SINGLE." + (hasRoleInTrack ? "ACTIVE" : "INACTIVE"), "id")
Skyler Grey75ea9172022-08-06 10:22:23 +010058 )
59 });
60 })
61 )
62 .setCustomId("select")
63 .setMaxValues(1);
pineafan63fc5e22022-08-04 22:04:10 +010064 const allowed = [];
Skyler Grey11236ba2022-08-08 21:13:33 +010065 generated = "**Track:** " + data.name + "\n" + "**Member:** " + renderUser(member.user) + "\n";
Skyler Grey75ea9172022-08-06 10:22:23 +010066 generated +=
Skyler Grey11236ba2022-08-08 21:13:33 +010067 (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 +010068 generated +=
69 (data.retainPrevious
70 ? "When promoted, the user keeps previous roles"
71 : "Members will lose their current role when promoted") + "\n";
72 generated +=
73 "\n" +
74 data.track
75 .map((role, index) => {
76 const allow =
Skyler Grey11236ba2022-08-08 21:13:33 +010077 roles.get(role).position >= (interaction.member as GuildMember).roles.highest.position &&
78 !managed;
Skyler Grey75ea9172022-08-06 10:22:23 +010079 allowed.push(!allow);
80 return (
81 getEmojiByName(
Skyler Grey11236ba2022-08-08 21:13:33 +010082 generateFromTrack(index, memberRoles.cache.has(role), data.track.length, allow)
Skyler Grey75ea9172022-08-06 10:22:23 +010083 ) +
84 " " +
85 roles.get(role).name +
86 " [<@&" +
87 roles.get(role).id +
88 ">]"
89 );
90 })
91 .join("\n");
pineafan63fc5e22022-08-04 22:04:10 +010092 const selected = [];
Skyler Greyc634e2b2022-08-06 17:50:48 +010093 for (const position of data.track) {
94 if (memberRoles.cache.has(position)) selected.push(position);
pineafanad54d752022-04-18 19:01:43 +010095 }
pineafan63fc5e22022-08-04 22:04:10 +010096 const conflict = data.retainPrevious ? false : selected.length > 1;
97 let conflictDropdown;
98 let currentRoleIndex;
pineafanad54d752022-04-18 19:01:43 +010099 if (conflict) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100100 generated += `\n\n${getEmojiByName(`PUNISH.WARN.${managed ? "YELLOW" : "RED"}`)} This user has ${
101 selected.length
102 } roles from this track. `;
pineafan63fc5e22022-08-04 22:04:10 +0100103 conflictDropdown = [];
Skyler Grey11236ba2022-08-08 21:13:33 +0100104 if (roles.get(selected[0]).position < memberRoles.highest.position || managed) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 generated +=
106 "In order to promote or demote this user, you must select which role the member should keep.";
107 selected.forEach((role) => {
108 conflictDropdown.push(
109 new SelectMenuOption({
110 label: roles.get(role).name,
111 value: roles.get(role).id
112 })
113 );
pineafan63fc5e22022-08-04 22:04:10 +0100114 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 conflictDropdown = [
116 new Discord.MessageSelectMenu()
117 .addOptions(conflictDropdown)
118 .setCustomId("conflict")
119 .setMaxValues(1)
120 .setPlaceholder("Select a role to keep")
121 ];
pineafanad54d752022-04-18 19:01:43 +0100122 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100123 generated +=
124 "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 +0100125 }
126 } else {
Skyler Grey11236ba2022-08-08 21:13:33 +0100127 currentRoleIndex = selected.length === 0 ? -1 : data.track.indexOf(selected[0].toString());
pineafanad54d752022-04-18 19:01:43 +0100128 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100129 const m = (await interaction.editReply({
130 embeds: [
131 new EmojiEmbed()
132 .setEmoji("TRACKS.ICON")
133 .setTitle("Tracks")
134 .setDescription(`${generated}`)
135 .setStatus("Success")
136 ],
137 components: [new MessageActionRow().addComponents(dropdown)]
138 .concat(
Skyler Grey11236ba2022-08-08 21:13:33 +0100139 conflict && conflictDropdown.length ? [new MessageActionRow().addComponents(conflictDropdown)] : []
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 )
141 .concat([
142 new MessageActionRow().addComponents([
143 new MessageButton()
144 .setEmoji(getEmojiByName("CONTROL.UP", "id"))
145 .setLabel("Move up")
146 .setCustomId("promote")
147 .setStyle("SUCCESS")
148 .setDisabled(
149 conflict ||
150 currentRoleIndex === 0 ||
Skyler Grey11236ba2022-08-08 21:13:33 +0100151 (currentRoleIndex === -1 ? false : !allowed[currentRoleIndex - 1])
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 ),
153 new MessageButton()
154 .setEmoji(getEmojiByName("CONTROL.DOWN", "id"))
155 .setLabel("Move down")
156 .setCustomId("demote")
157 .setStyle("DANGER")
158 .setDisabled(
159 conflict ||
160 (data.nullable
161 ? currentRoleIndex <= -1
Skyler Grey11236ba2022-08-08 21:13:33 +0100162 : currentRoleIndex === data.track.length - 1 || currentRoleIndex <= -1) ||
Skyler Grey75ea9172022-08-06 10:22:23 +0100163 !allowed[currentRoleIndex]
164 )
165 ])
pineafan63fc5e22022-08-04 22:04:10 +0100166 ])
Skyler Grey75ea9172022-08-06 10:22:23 +0100167 })) as Message;
pineafanad54d752022-04-18 19:01:43 +0100168 let component;
169 try {
Skyler Grey75ea9172022-08-06 10:22:23 +0100170 component = await m.awaitMessageComponent({ time: 300000 });
pineafanad54d752022-04-18 19:01:43 +0100171 } catch (e) {
pineafan63fc5e22022-08-04 22:04:10 +0100172 return;
pineafanad54d752022-04-18 19:01:43 +0100173 }
pineafan63fc5e22022-08-04 22:04:10 +0100174 component.deferUpdate();
pineafane23c4ec2022-07-27 21:56:27 +0100175 if (component.customId === "conflict") {
Skyler Grey11236ba2022-08-08 21:13:33 +0100176 const rolesToRemove = selected.filter((role) => role !== component.values[0]);
pineafan63fc5e22022-08-04 22:04:10 +0100177 await member.roles.remove(rolesToRemove);
pineafane23c4ec2022-07-27 21:56:27 +0100178 } else if (component.customId === "promote") {
pineafanad54d752022-04-18 19:01:43 +0100179 if (
Skyler Grey75ea9172022-08-06 10:22:23 +0100180 currentRoleIndex === -1
181 ? allowed[data.track.length - 1]
182 : allowed[currentRoleIndex - 1] && allowed[currentRoleIndex]
pineafanad54d752022-04-18 19:01:43 +0100183 ) {
pineafane23c4ec2022-07-27 21:56:27 +0100184 if (currentRoleIndex === -1) {
pineafan63fc5e22022-08-04 22:04:10 +0100185 await member.roles.add(data.track[data.track.length - 1]);
pineafanad54d752022-04-18 19:01:43 +0100186 } else if (currentRoleIndex < data.track.length) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100187 if (!data.retainPrevious) await member.roles.remove(data.track[currentRoleIndex]);
pineafan63fc5e22022-08-04 22:04:10 +0100188 await member.roles.add(data.track[currentRoleIndex - 1]);
pineafanad54d752022-04-18 19:01:43 +0100189 }
190 }
pineafane23c4ec2022-07-27 21:56:27 +0100191 } else if (component.customId === "demote") {
Skyler Grey75ea9172022-08-06 10:22:23 +0100192 if (allowed[currentRoleIndex]) {
pineafane23c4ec2022-07-27 21:56:27 +0100193 if (currentRoleIndex === data.track.length - 1) {
Skyler Grey11236ba2022-08-08 21:13:33 +0100194 if (data.nullable) await member.roles.remove(data.track[currentRoleIndex]);
pineafanad54d752022-04-18 19:01:43 +0100195 } else if (currentRoleIndex > -1) {
pineafan63fc5e22022-08-04 22:04:10 +0100196 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 === "select") {
pineafan63fc5e22022-08-04 22:04:10 +0100201 track = component.values[0];
pineafanad54d752022-04-18 19:01:43 +0100202 }
203 }
pineafan63fc5e22022-08-04 22:04:10 +0100204};
pineafanad54d752022-04-18 19:01:43 +0100205
Skyler Grey11236ba2022-08-08 21:13:33 +0100206const check = async (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
207 const tracks = (await client.database.guilds.read(interaction.guild.id)).tracks;
208 if (tracks.length === 0) throw new Error("This server does not have any tracks");
Skyler Grey75ea9172022-08-06 10:22:23 +0100209 const member = interaction.member as GuildMember;
pineafanad54d752022-04-18 19:01:43 +0100210 // Allow the owner to promote anyone
pineafan63fc5e22022-08-04 22:04:10 +0100211 if (member.id === interaction.guild.ownerId) return true;
pineafan6702cef2022-06-13 17:52:37 +0100212 // Check if the user can manage any of the tracks
pineafan63fc5e22022-08-04 22:04:10 +0100213 let managed = false;
pineafanc1c18792022-08-03 21:41:36 +0100214 for (const element of tracks) {
pineafan63fc5e22022-08-04 22:04:10 +0100215 if (!element.track.manageableBy) continue;
Skyler Grey11236ba2022-08-08 21:13:33 +0100216 if (!element.track.manageableBy.some((role) => member.roles.cache.has(role))) continue;
pineafanc1c18792022-08-03 21:41:36 +0100217 managed = true;
218 break;
pineafan63fc5e22022-08-04 22:04:10 +0100219 }
pineafanad54d752022-04-18 19:01:43 +0100220 // Check if the user has manage_roles permission
Skyler Grey75ea9172022-08-06 10:22:23 +0100221 if (!managed && !member.permissions.has("MANAGE_ROLES"))
Skyler Greyc634e2b2022-08-06 17:50:48 +0100222 throw new Error("You do not have the *Manage Roles* permission");
pineafanad54d752022-04-18 19:01:43 +0100223 // Allow track
pineafan6702cef2022-06-13 17:52:37 +0100224 return true;
pineafan63fc5e22022-08-04 22:04:10 +0100225};
pineafanad54d752022-04-18 19:01:43 +0100226
227export { command };
228export { callback };
pineafanc6158ab2022-06-17 16:34:07 +0100229export { check };