Add audit command to see a user's audits

This commit is contained in:
Ethan Lane 2022-08-18 18:51:12 +01:00
parent 4812f5b691
commit 595c15acb1
Signed by: Vylpes
GPG key ID: EED233CC06D12504
10 changed files with 94 additions and 22 deletions

61
src/commands/audits.ts Normal file
View file

@ -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 <id>\` - 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();
}
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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<Audit[] | undefined> {
public static async FetchAuditsByUserId(userId: string, serverId: string): Promise<Audit[] | undefined> {
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;
}

View file

@ -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);
}

20
src/helpers/AuditTools.ts Normal file
View file

@ -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";
}
}
}

View file

@ -3,7 +3,7 @@ import { MigrationInterface, QueryRunner } from "typeorm"
export class CreateAudit1660754832945 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {

View file

@ -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");