Bug fixes and ~~performance~~ typing improvements
diff --git a/src/commands/user/track.ts b/src/commands/user/track.ts
index 1a4c308..da62880 100644
--- a/src/commands/user/track.ts
+++ b/src/commands/user/track.ts
@@ -1,15 +1,6 @@
import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
-import Discord, {
- CommandInteraction,
- GuildMember,
- Message,
- MessageActionRow,
- MessageButton
-} from "discord.js";
-import {
- SelectMenuOption,
- SlashCommandSubcommandBuilder
-} from "@discordjs/builders";
+import Discord, { CommandInteraction, GuildMember, Message, MessageActionRow, MessageButton } from "discord.js";
+import { SelectMenuOption, SlashCommandSubcommandBuilder } from "@discordjs/builders";
// @ts-expect-error
import { WrappedCheck } from "jshaiku";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
@@ -21,25 +12,13 @@
builder
.setName("track")
.setDescription("Moves a user along a role track")
- .addUserOption((option) =>
- option
- .setName("user")
- .setDescription("The user to manage")
- .setRequired(true)
- );
+ .addUserOption((option) => option.setName("user").setDescription("The user to manage").setRequired(true));
-const generateFromTrack = (
- position: number,
- active: string | boolean,
- size: number,
- disabled: string | boolean
-) => {
+const generateFromTrack = (position: number, active: string | boolean, size: number, disabled: string | boolean) => {
active = active ? "ACTIVE" : "INACTIVE";
disabled = disabled ? "GREY." : "";
- if (position === 0 && size === 1)
- return "TRACKS.SINGLE." + disabled + active;
- if (position === size - 1)
- return "TRACKS.VERTICAL.BOTTOM." + disabled + active;
+ if (position === 0 && size === 1) return "TRACKS.SINGLE." + disabled + active;
+ if (position === size - 1) return "TRACKS.VERTICAL.BOTTOM." + disabled + active;
if (position === 0) return "TRACKS.VERTICAL.TOP." + disabled + active;
return "TRACKS.VERTICAL.MIDDLE." + disabled + active;
};
@@ -66,25 +45,16 @@
const dropdown = new Discord.MessageSelectMenu()
.addOptions(
config.tracks.map((option, index) => {
- const hasRoleInTrack = option.track.some(
- (element: string) => {
- return memberRoles.cache.has(element);
- }
- );
+ const hasRoleInTrack = option.track.some((element: string) => {
+ return memberRoles.cache.has(element);
+ });
return new SelectMenuOption({
default: index === track,
label: option.name,
value: index.toString(),
- description:
- option.track.length === 0
- ? "No"
- : addPlural(option.track.length, "role"),
+ description: option.track.length === 0 ? "No" : addPlural(option.track.length, "role"),
emoji: client.emojis.resolve(
- getEmojiByName(
- "TRACKS.SINGLE." +
- (hasRoleInTrack ? "ACTIVE" : "INACTIVE"),
- "id"
- )
+ getEmojiByName("TRACKS.SINGLE." + (hasRoleInTrack ? "ACTIVE" : "INACTIVE"), "id")
)
});
})
@@ -92,17 +62,9 @@
.setCustomId("select")
.setMaxValues(1);
const allowed = [];
- generated =
- "**Track:** " +
- data.name +
- "\n" +
- "**Member:** " +
- renderUser(member.user) +
- "\n";
+ generated = "**Track:** " + data.name + "\n" + "**Member:** " + renderUser(member.user) + "\n";
generated +=
- (data.nullable
- ? "Members do not need a role in this track"
- : "A role in this track is required") + "\n";
+ (data.nullable ? "Members do not need a role in this track" : "A role in this track is required") + "\n";
generated +=
(data.retainPrevious
? "When promoted, the user keeps previous roles"
@@ -112,18 +74,12 @@
data.track
.map((role, index) => {
const allow =
- roles.get(role).position >=
- (interaction.member as GuildMember).roles.highest
- .position && !managed;
+ roles.get(role).position >= (interaction.member as GuildMember).roles.highest.position &&
+ !managed;
allowed.push(!allow);
return (
getEmojiByName(
- generateFromTrack(
- index,
- memberRoles.cache.has(role),
- data.track.length,
- allow
- )
+ generateFromTrack(index, memberRoles.cache.has(role), data.track.length, allow)
) +
" " +
roles.get(role).name +
@@ -141,15 +97,11 @@
let conflictDropdown;
let currentRoleIndex;
if (conflict) {
- generated += `\n\n${getEmojiByName(
- `PUNISH.WARN.${managed ? "YELLOW" : "RED"}`
- )} This user has ${selected.length} roles from this track. `;
+ generated += `\n\n${getEmojiByName(`PUNISH.WARN.${managed ? "YELLOW" : "RED"}`)} This user has ${
+ selected.length
+ } roles from this track. `;
conflictDropdown = [];
- if (
- roles.get(selected[0]).position <
- memberRoles.highest.position ||
- managed
- ) {
+ if (roles.get(selected[0]).position < memberRoles.highest.position || managed) {
generated +=
"In order to promote or demote this user, you must select which role the member should keep.";
selected.forEach((role) => {
@@ -172,10 +124,7 @@
"You don't have permission to manage one or more of the users roles, and therefore can't select one to keep.";
}
} else {
- currentRoleIndex =
- selected.length === 0
- ? -1
- : data.track.indexOf(selected[0].toString());
+ currentRoleIndex = selected.length === 0 ? -1 : data.track.indexOf(selected[0].toString());
}
const m = (await interaction.editReply({
embeds: [
@@ -187,13 +136,7 @@
],
components: [new MessageActionRow().addComponents(dropdown)]
.concat(
- conflict && conflictDropdown.length
- ? [
- new MessageActionRow().addComponents(
- conflictDropdown
- )
- ]
- : []
+ conflict && conflictDropdown.length ? [new MessageActionRow().addComponents(conflictDropdown)] : []
)
.concat([
new MessageActionRow().addComponents([
@@ -205,9 +148,7 @@
.setDisabled(
conflict ||
currentRoleIndex === 0 ||
- (currentRoleIndex === -1
- ? false
- : !allowed[currentRoleIndex - 1])
+ (currentRoleIndex === -1 ? false : !allowed[currentRoleIndex - 1])
),
new MessageButton()
.setEmoji(getEmojiByName("CONTROL.DOWN", "id"))
@@ -218,9 +159,7 @@
conflict ||
(data.nullable
? currentRoleIndex <= -1
- : currentRoleIndex ===
- data.track.length - 1 ||
- currentRoleIndex <= -1) ||
+ : currentRoleIndex === data.track.length - 1 || currentRoleIndex <= -1) ||
!allowed[currentRoleIndex]
)
])
@@ -234,9 +173,7 @@
}
component.deferUpdate();
if (component.customId === "conflict") {
- const rolesToRemove = selected.filter(
- (role) => role !== component.values[0]
- );
+ const rolesToRemove = selected.filter((role) => role !== component.values[0]);
await member.roles.remove(rolesToRemove);
} else if (component.customId === "promote") {
if (
@@ -247,16 +184,14 @@
if (currentRoleIndex === -1) {
await member.roles.add(data.track[data.track.length - 1]);
} else if (currentRoleIndex < data.track.length) {
- if (!data.retainPrevious)
- await member.roles.remove(data.track[currentRoleIndex]);
+ if (!data.retainPrevious) await member.roles.remove(data.track[currentRoleIndex]);
await member.roles.add(data.track[currentRoleIndex - 1]);
}
}
} else if (component.customId === "demote") {
if (allowed[currentRoleIndex]) {
if (currentRoleIndex === data.track.length - 1) {
- if (data.nullable)
- await member.roles.remove(data.track[currentRoleIndex]);
+ if (data.nullable) await member.roles.remove(data.track[currentRoleIndex]);
} else if (currentRoleIndex > -1) {
await member.roles.remove(data.track[currentRoleIndex]);
await member.roles.add(data.track[currentRoleIndex + 1]);
@@ -268,14 +203,9 @@
}
};
-const check = async (
- interaction: CommandInteraction,
- _defaultCheck: WrappedCheck
-) => {
- const tracks = (await client.database.guilds.read(interaction.guild.id))
- .tracks;
- if (tracks.length === 0)
- throw new Error("This server does not have any tracks");
+const check = async (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => {
+ const tracks = (await client.database.guilds.read(interaction.guild.id)).tracks;
+ if (tracks.length === 0) throw new Error("This server does not have any tracks");
const member = interaction.member as GuildMember;
// Allow the owner to promote anyone
if (member.id === interaction.guild.ownerId) return true;
@@ -283,12 +213,7 @@
let managed = false;
for (const element of tracks) {
if (!element.track.manageableBy) continue;
- if (
- !element.track.manageableBy.some((role) =>
- member.roles.cache.has(role)
- )
- )
- continue;
+ if (!element.track.manageableBy.some((role) => member.roles.cache.has(role))) continue;
managed = true;
break;
}