From 595c15acb115c385de75cc493c1d10ca94b53711 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 18 Aug 2022 18:51:12 +0100 Subject: [PATCH] Add audit command to see a user's audits --- src/commands/audits.ts | 61 ++++++++++++++++++++++ src/commands/ban.ts | 3 +- src/commands/kick.ts | 3 +- src/commands/mute.ts | 3 +- src/commands/warn.ts | 3 +- src/entity/Audit.ts | 16 +++--- src/entity/Server.ts | 3 -- src/helpers/AuditTools.ts | 20 +++++++ src/migration/1660754832945-CreateAudit.ts | 2 +- src/registry.ts | 2 + 10 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 src/commands/audits.ts create mode 100644 src/helpers/AuditTools.ts diff --git a/src/commands/audits.ts b/src/commands/audits.ts new file mode 100644 index 0000000..b6045a0 --- /dev/null +++ b/src/commands/audits.ts @@ -0,0 +1,61 @@ +import { ICommandContext } from "../contracts/ICommandContext"; +import Audit from "../entity/Audit"; +import AuditTools from "../helpers/AuditTools"; +import PublicEmbed from "../helpers/embeds/PublicEmbed"; +import { Command } from "../type/command"; +import SettingsHelper from "../helpers/SettingsHelper"; + +export default class Audits extends Command { + constructor() { + super(); + + super.Category = "Moderation"; + super.Roles = [ + "moderator" + ]; + } + + public override async execute(context: ICommandContext) { + if (!context.message.guild) return; + + switch (context.args[0]) { + case "user": + await this.SendAuditForUser(context); + break; + default: + await this.SendUsage(context); + } + } + + private async SendUsage(context: ICommandContext) { + const prefix = await SettingsHelper.GetServerPrefix(context.message.guild!.id); + + const description = [ + `\`${prefix}audits user \` - Send the audits for this user`, + ] + + const publicEmbed = new PublicEmbed(context, "Usage", description.join("\n")); + await publicEmbed.SendToCurrentChannel(); + } + + private async SendAuditForUser(context: ICommandContext) { + const userId = context.args[1]; + + const audits = await Audit.FetchAuditsByUserId(userId, context.message.guild!.id); + + if (!audits || audits.length == 0) { + const publicEmbed = new PublicEmbed(context, "", "There are no audits logged for this user."); + await publicEmbed.SendToCurrentChannel(); + + return; + } + + const publicEmbed = new PublicEmbed(context, "Audit Log", ""); + + for (let audit of audits) { + publicEmbed.addField(`${audit.AuditId} // ${AuditTools.TypeToFriendlyText(audit.AuditType)}`, audit.WhenCreated.toString()); + } + + await publicEmbed.SendToCurrentChannel(); + } +} \ No newline at end of file diff --git a/src/commands/ban.ts b/src/commands/ban.ts index 8d7973b..518b0d2 100644 --- a/src/commands/ban.ts +++ b/src/commands/ban.ts @@ -82,8 +82,7 @@ export default class Ban extends Command { const server = await Server.FetchOneById(Server, context.message.guild.id); if (server) { - const audit = new Audit(targetUser.id, AuditType.Ban, reason, context.message.author.id); - audit.AssignToServer(server); + const audit = new Audit(targetUser.id, AuditType.Ban, reason, context.message.author.id, context.message.guild.id); await audit.Save(Audit, audit); } diff --git a/src/commands/kick.ts b/src/commands/kick.ts index 53b8b90..5be4321 100644 --- a/src/commands/kick.ts +++ b/src/commands/kick.ts @@ -82,8 +82,7 @@ export default class Kick extends Command { const server = await Server.FetchOneById(Server, context.message.guild.id); if (server) { - const audit = new Audit(targetUser.id, AuditType.Kick, reason, context.message.author.id); - audit.AssignToServer(server); + const audit = new Audit(targetUser.id, AuditType.Kick, reason, context.message.author.id, context.message.guild.id); await audit.Save(Audit, audit); } diff --git a/src/commands/mute.ts b/src/commands/mute.ts index e426e4f..8e01eea 100644 --- a/src/commands/mute.ts +++ b/src/commands/mute.ts @@ -95,8 +95,7 @@ export default class Mute extends Command { const server = await Server.FetchOneById(Server, context.message.guild.id); if (server) { - const audit = new Audit(targetUser.id, AuditType.Mute, reason, context.message.author.id); - audit.AssignToServer(server); + const audit = new Audit(targetUser.id, AuditType.Mute, reason, context.message.author.id, context.message.guild.id); await audit.Save(Audit, audit); } diff --git a/src/commands/warn.ts b/src/commands/warn.ts index 69f1f9f..dbc70c0 100644 --- a/src/commands/warn.ts +++ b/src/commands/warn.ts @@ -70,8 +70,7 @@ export default class Warn extends Command { const server = await Server.FetchOneById(Server, context.message.guild.id); if (server) { - const audit = new Audit(user.id, AuditType.Warn, reason, context.message.author.id); - audit.AssignToServer(server); + const audit = new Audit(user.id, AuditType.Warn, reason, context.message.author.id, context.message.guild.id); await audit.Save(Audit, audit); } diff --git a/src/entity/Audit.ts b/src/entity/Audit.ts index b2dfcd5..58d2356 100644 --- a/src/entity/Audit.ts +++ b/src/entity/Audit.ts @@ -2,11 +2,10 @@ import { Column, Entity, getConnection, ManyToOne } from "typeorm"; import { AuditType } from "../constants/AuditType"; import BaseEntity from "../contracts/BaseEntity"; import StringTools from "../helpers/StringTools"; -import Server from "./Server"; @Entity() export default class Audit extends BaseEntity { - constructor(userId: string, auditType: AuditType, reason: string, moderatorId: string) { + constructor(userId: string, auditType: AuditType, reason: string, moderatorId: string, serverId: string) { super(); this.AuditId = StringTools.RandomString(5).toUpperCase(); @@ -14,6 +13,7 @@ export default class Audit extends BaseEntity { this.AuditType = auditType; this.Reason = reason; this.ModeratorId = moderatorId; + this.ServerId = serverId; } @Column() @@ -31,19 +31,15 @@ export default class Audit extends BaseEntity { @Column() ModeratorId: string; - @ManyToOne(() => Server, x => x.Audits) - Server: Server; + @Column() + ServerId: string; - public AssignToServer(server: Server) { - this.Server = server; - } - - public static async FetchAuditsByUserId(userId: string): Promise { + public static async FetchAuditsByUserId(userId: string, serverId: string): Promise { const connection = getConnection(); const repository = connection.getRepository(Audit); - const all = await repository.find({ UserId: userId }); + const all = await repository.find({ UserId: userId, ServerId: serverId }); return all; } diff --git a/src/entity/Server.ts b/src/entity/Server.ts index eadb249..bd0b1e9 100644 --- a/src/entity/Server.ts +++ b/src/entity/Server.ts @@ -18,9 +18,6 @@ export default class Server extends BaseEntity { @OneToMany(() => Role, x => x.Server) Roles: Role[]; - @OneToMany(() => Audit, x => x.Server) - Audits: Audit[]; - public AddSettingToServer(setting: Setting) { this.Settings.push(setting); } diff --git a/src/helpers/AuditTools.ts b/src/helpers/AuditTools.ts new file mode 100644 index 0000000..1464fa1 --- /dev/null +++ b/src/helpers/AuditTools.ts @@ -0,0 +1,20 @@ +import { AuditType } from "../constants/AuditType"; + +export default class AuditTools { + public static TypeToFriendlyText(auditType: AuditType): string { + switch (auditType) { + case AuditType.General: + return "General"; + case AuditType.Warn: + return "Warn"; + case AuditType.Mute: + return "Mute"; + case AuditType.Kick: + return "Kick"; + case AuditType.Ban: + return "Ban"; + default: + return "Other"; + } + } +} \ No newline at end of file diff --git a/src/migration/1660754832945-CreateAudit.ts b/src/migration/1660754832945-CreateAudit.ts index 9a44ed7..9dab091 100644 --- a/src/migration/1660754832945-CreateAudit.ts +++ b/src/migration/1660754832945-CreateAudit.ts @@ -3,7 +3,7 @@ import { MigrationInterface, QueryRunner } from "typeorm" export class CreateAudit1660754832945 implements MigrationInterface { public async up(queryRunner: QueryRunner): Promise { - queryRunner.query(`CREATE TABLE audit (Id varchar(255), WhenCreated datetime, WhenUpdated datetime, auditId varchar(255), userId varchar(255), auditType int, reason varchar(255), moderatorId varchar(255), serverId varchar(255), PRIMARY KEY (Id), FOREIGN KEY (serverId) REFERENCES server(id))`); + queryRunner.query(`CREATE TABLE audit (Id varchar(255), WhenCreated datetime, WhenUpdated datetime, auditId varchar(255), userId varchar(255), auditType int, reason varchar(255), moderatorId varchar(255), serverId varchar(255), PRIMARY KEY (Id))`); } public async down(queryRunner: QueryRunner): Promise { diff --git a/src/registry.ts b/src/registry.ts index 4fa68f7..c34c098 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -2,6 +2,7 @@ import { CoreClient } from "./client/client"; // Command Imports import About from "./commands/about"; +import Audits from "./commands/audits"; import Ban from "./commands/ban"; import Clear from "./commands/clear"; import Code from "./commands/code"; @@ -48,6 +49,7 @@ export default class Registry { CoreClient.RegisterCommand("warn", new Warn()); CoreClient.RegisterCommand("setup", new Setup()); CoreClient.RegisterCommand("say", new Say()); + CoreClient.RegisterCommand("audits", new Audits()); // Exclusive Commands: MankBot CoreClient.RegisterCommand("lobby", new Lobby(), "501231711271780357");