Feature/vba 77 #178
10 changed files with 94 additions and 22 deletions
61
src/commands/audits.ts
Normal file
61
src/commands/audits.ts
Normal 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) {
|
||||
Should send the usage if this isn't set Should send the usage if this isn't set
|
||||
publicEmbed.addField(`${audit.AuditId} // ${AuditTools.TypeToFriendlyText(audit.AuditType)}`, audit.WhenCreated.toString());
|
||||
}
|
||||
|
||||
await publicEmbed.SendToCurrentChannel();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -18,9 +18,6 @@ export default class Server extends BaseEntity {
|
|||
Unused import Unused import
Unused import Unused import
|
||||
@OneToMany(() => Role, x => x.Server)
|
||||
Roles: Role[];
|
||||
|
||||
@OneToMany(() => Audit, x => x.Server)
|
||||
Unused import Unused import
|
||||
Audits: Audit[];
|
||||
Unused import Unused import
|
||||
|
||||
Unused import Unused import
|
||||
public AddSettingToServer(setting: Setting) {
|
||||
this.Settings.push(setting);
|
||||
}
|
||||
|
|
|||
Unused import Unused import
Unused import Unused import
|
20
src/helpers/AuditTools.ts
Normal file
20
src/helpers/AuditTools.ts
Normal 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";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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> {
|
||||
|
|
|
@ -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");
|
||||
|
|
Loading…
Reference in a new issue
Move this into its own text file like the other files