From 5116ccf230c933c290676d033a5639b3913ee03b Mon Sep 17 00:00:00 2001
From: IRONM00N <64110067+IRONM00N@users.noreply.github.com>
Date: Mon, 21 Feb 2022 16:08:38 -0500
Subject: perf: make massban actually work

---
 src/lib/common/util/Moderation.ts | 101 ++++++++++++++++++++++++++------------
 1 file changed, 70 insertions(+), 31 deletions(-)

(limited to 'src/lib/common/util')

diff --git a/src/lib/common/util/Moderation.ts b/src/lib/common/util/Moderation.ts
index 04ccb31..365dbd5 100644
--- a/src/lib/common/util/Moderation.ts
+++ b/src/lib/common/util/Moderation.ts
@@ -123,25 +123,41 @@ export class Moderation {
 		const user = (await util.resolveNonCachedUser(options.user))!.id;
 		const moderator = (await util.resolveNonCachedUser(options.moderator))!.id;
 		const guild = client.guilds.resolveId(options.guild)!;
-		const duration = options.duration ? options.duration : undefined;
 
+		return this.createModLogEntrySimple(
+			{
+				...options,
+				user: user,
+				moderator: moderator,
+				guild: guild
+			},
+			getCaseNumber
+		);
+	}
+
+	/**
+	 * Creates a modlog entry with already resolved ids.
+	 * @param options Options for creating a modlog entry.
+	 * @param getCaseNumber Whether or not to get the case number of the entry.
+	 * @returns An object with the modlog and the case number.
+	 */
+	public static async createModLogEntrySimple(
+		options: SimpleCreateModLogEntryOptions,
+		getCaseNumber = false
+	): Promise<{ log: ModLog | null; caseNum: number | null }> {
 		// If guild does not exist create it so the modlog can reference a guild.
 		await Guild.findOrCreate({
-			where: {
-				id: guild
-			},
-			defaults: {
-				id: guild
-			}
+			where: { id: options.guild },
+			defaults: { id: options.guild }
 		});
 
 		const modLogEntry = ModLog.build({
 			type: options.type,
-			user,
-			moderator,
+			user: options.user,
+			moderator: options.moderator,
 			reason: options.reason,
-			duration: duration,
-			guild,
+			duration: options.duration ? options.duration : undefined,
+			guild: options.guild,
 			pseudo: options.pseudo ?? false,
 			evidence: options.evidence,
 			hidden: options.hidden ?? false
@@ -153,7 +169,9 @@ export class Moderation {
 
 		if (!getCaseNumber) return { log: saveResult, caseNum: null };
 
-		const caseNum = (await ModLog.findAll({ where: { type: options.type, user: user, guild: guild, hidden: 'false' } }))?.length;
+		const caseNum = (
+			await ModLog.findAll({ where: { type: options.type, user: options.user, guild: options.guild, hidden: false } })
+		)?.length;
 		return { log: saveResult, caseNum };
 	}
 
@@ -269,7 +287,6 @@ export class Moderation {
 		if (appealsEnabled && options.modlog)
 			components = [
 				new ActionRow({
-					type: ComponentType.ActionRow,
 					components: [
 						new ButtonComponent({
 							custom_id: `appeal;${this.punishmentToPresentTense(options.punishment)};${
@@ -294,54 +311,76 @@ export class Moderation {
 	}
 }
 
-/**
- * Options for creating a modlog entry.
- */
-export interface CreateModLogEntryOptions {
+interface BaseCreateModLogEntryOptions {
 	/**
 	 * The type of modlog entry.
 	 */
 	type: ModLogType;
 
 	/**
-	 * The user that a modlog entry is created for.
+	 * The reason for the punishment.
 	 */
-	user: BushGuildMemberResolvable;
+	reason: string | undefined | null;
 
 	/**
-	 * The moderator that created the modlog entry.
+	 * The duration of the punishment.
 	 */
-	moderator: BushGuildMemberResolvable;
+	duration?: number;
 
 	/**
-	 * The reason for the punishment.
+	 * Whether the punishment is a pseudo punishment.
 	 */
-	reason: string | undefined | null;
+	pseudo?: boolean;
 
 	/**
-	 * The duration of the punishment.
+	 * The evidence for the punishment.
 	 */
-	duration?: number;
+	evidence?: string;
+
+	/**
+	 * Makes the modlog entry hidden.
+	 */
+	hidden?: boolean;
+}
+
+/**
+ * Options for creating a modlog entry.
+ */
+export interface CreateModLogEntryOptions extends BaseCreateModLogEntryOptions {
+	/**
+	 * The user that a modlog entry is created for.
+	 */
+	user: BushGuildMemberResolvable;
+
+	/**
+	 * The moderator that created the modlog entry.
+	 */
+	moderator: BushGuildMemberResolvable;
 
 	/**
 	 * The guild that the punishment is created for.
 	 */
 	guild: BushGuildResolvable;
+}
 
+/**
+ * Simple options for creating a modlog entry.
+ */
+export interface SimpleCreateModLogEntryOptions extends BaseCreateModLogEntryOptions {
 	/**
-	 * Whether the punishment is a pseudo punishment.
+	 * The user that a modlog entry is created for.
 	 */
-	pseudo?: boolean;
+	user: Snowflake;
 
 	/**
-	 * The evidence for the punishment.
+	 * The moderator that created the modlog entry.
 	 */
-	evidence?: string;
+	moderator: Snowflake;
 
 	/**
-	 * Makes the modlog entry hidden.
+	 * The guild that the punishment is created for.
 	 */
-	hidden?: boolean;
+	guild: Snowflake;
 }
 
 /**
-- 
cgit