3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

Rename ModActions to Cases

This commit is contained in:
Dragory 2018-07-12 02:53:26 +03:00
parent 62dfc80a0e
commit e6f296381e
7 changed files with 154 additions and 152 deletions

View file

@ -0,0 +1,21 @@
exports.up = async function(knex) {
await knex.schema.renameTable('mod_actions', 'cases');
await knex.schema.renameTable('mod_action_notes', 'case_notes');
await knex.schema.table('cases', table => {
table.renameColumn('action_type', 'type');
});
await knex.schema.table('case_notes', table => {
table.renameColumn('mod_action_id', 'case_id');
});
};
exports.down = async function(knex) {
await knex.schema.table('cases', table => {
table.renameColumn('type', 'action_type');
});
await knex.schema.table('case_notes', table => {
table.renameColumn('case_id', 'mod_action_id');
});
await knex.schema.renameTable('cases', 'mod_actions');
await knex.schema.renameTable('case_notes', 'mod_action_notes');
};

View file

@ -1,4 +1,4 @@
export enum ModActionType { export enum CaseType {
Ban = 1, Ban = 1,
Unban, Unban,
Note, Note,

76
src/data/GuildCases.ts Normal file
View file

@ -0,0 +1,76 @@
import * as knex from "../knex";
import Case from "../models/Case";
export class GuildCases {
protected guildId: string;
constructor(guildId) {
this.guildId = guildId;
}
async find(id: number): Promise<Case> {
const result = await knex("cases")
.where("guild_id", this.guildId)
.where("id", id)
.first();
return result ? new Case(result) : null;
}
async findByCaseNumber(caseNumber: number): Promise<Case> {
const result = await knex("cases")
.where("guild_id", this.guildId)
.where("case_number", caseNumber)
.first();
return result ? new Case(result) : null;
}
async getCaseNotes(caseId: number): Promise<Case[]> {
const results = await knex("case_notes")
.where("case_id", caseId)
.select();
return results.map(r => new Case(r));
}
async getByUserId(userId: string): Promise<Case[]> {
const results = await knex("cases")
.where("guild_id", this.guildId)
.where("user_id", userId)
.select();
return results.map(r => new Case(r));
}
async create(data): Promise<number> {
return knex
.insert({
...data,
guild_id: this.guildId,
case_number: knex.raw(
"(SELECT IFNULL(MAX(case_number)+1, 1) FROM cases AS ma2 WHERE guild_id = ?)",
this.guildId
)
})
.returning("id")
.into("cases")
.then(ids => Number(ids[0]));
}
update(id, data) {
return knex("cases")
.where("id", id)
.update(data);
}
createNote(caseId: number, data: any) {
return knex
.insert({
...data,
case_id: caseId
})
.into("case_notes")
.return();
}
}

View file

@ -1,76 +0,0 @@
import knex from "../knex";
import ModAction from "../models/ModAction";
export class GuildModActions {
protected guildId: string;
constructor(guildId) {
this.guildId = guildId;
}
async find(id: number): Promise<ModAction> {
const result = await knex("mod_actions")
.where("guild_id", this.guildId)
.where("id", id)
.first();
return result ? new ModAction(result) : null;
}
async findByCaseNumber(caseNumber: number): Promise<ModAction> {
const result = await knex("mod_actions")
.where("guild_id", this.guildId)
.where("case_number", caseNumber)
.first();
return result ? new ModAction(result) : null;
}
async getActionNotes(actionId: number): Promise<ModAction[]> {
const results = await knex("mod_action_notes")
.where("mod_action_id", actionId)
.select();
return results.map(r => new ModAction(r));
}
async getByUserId(userId: string): Promise<ModAction[]> {
const results = await knex("mod_actions")
.where("guild_id", this.guildId)
.where("user_id", userId)
.select();
return results.map(r => new ModAction(r));
}
async create(data): Promise<number> {
return knex
.insert({
...data,
guild_id: this.guildId,
case_number: knex.raw(
"(SELECT IFNULL(MAX(case_number)+1, 1) FROM mod_actions AS ma2 WHERE guild_id = ?)",
this.guildId
)
})
.returning("id")
.into("mod_actions")
.then(ids => Number(ids[0]));
}
update(id, data) {
return knex("mod_actions")
.where("id", id)
.update(data);
}
createNote(modActionId: number, data: any) {
return knex
.insert({
...data,
mod_action_id: modActionId
})
.into("mod_action_notes")
.return();
}
}

View file

@ -1,4 +1,4 @@
import knex from "../knex"; import * as knex from "../knex";
import * as moment from "moment-timezone"; import * as moment from "moment-timezone";
import Mute from "../models/Mute"; import Mute from "../models/Mute";

View file

@ -1,6 +1,6 @@
import Model from "./Model"; import Model from "./Model";
export default class ModAction extends Model { export default class Case extends Model {
public id: number; public id: number;
public guild_id: string; public guild_id: string;
public case_number: number; public case_number: number;
@ -8,7 +8,7 @@ export default class ModAction extends Model {
public user_name: string; public user_name: string;
public mod_id: string; public mod_id: string;
public mod_name: string; public mod_name: string;
public action_type: number; public type: number;
public audit_log_id: string; public audit_log_id: string;
public created_at: string; public created_at: string;
} }

View file

@ -2,7 +2,7 @@ import { Plugin, decorators as d, waitForReaction } from "knub";
import { Guild, GuildAuditLogEntry, Member, Message, TextChannel, User } from "eris"; import { Guild, GuildAuditLogEntry, Member, Message, TextChannel, User } from "eris";
import * as moment from "moment-timezone"; import * as moment from "moment-timezone";
import * as humanizeDuration from "humanize-duration"; import * as humanizeDuration from "humanize-duration";
import { GuildModActions } from "../data/GuildModActions"; import { GuildCases } from "../data/GuildCases";
import { import {
convertDelayStringToMS, convertDelayStringToMS,
errorMessage, errorMessage,
@ -12,8 +12,8 @@ import {
} from "../utils"; } from "../utils";
import { GuildMutes } from "../data/GuildMutes"; import { GuildMutes } from "../data/GuildMutes";
import Timer = NodeJS.Timer; import Timer = NodeJS.Timer;
import ModAction from "../models/ModAction"; import Case from "../models/Case";
import { ModActionType } from "../data/ModActionType"; import { CaseType } from "../data/CaseType";
import { GuildServerLogs } from "../data/GuildServerLogs"; import { GuildServerLogs } from "../data/GuildServerLogs";
import { LogType } from "../data/LogType"; import { LogType } from "../data/LogType";
@ -24,14 +24,14 @@ const sleep = (ms: number): Promise<void> => {
}; };
export class ModActionsPlugin extends Plugin { export class ModActionsPlugin extends Plugin {
protected modActions: GuildModActions; protected cases: GuildCases;
protected mutes: GuildMutes; protected mutes: GuildMutes;
protected serverLogs: GuildServerLogs; protected serverLogs: GuildServerLogs;
protected muteClearIntervalId: Timer; protected muteClearIntervalId: Timer;
async onLoad() { async onLoad() {
this.modActions = new GuildModActions(this.guildId); this.cases = new GuildCases(this.guildId);
this.mutes = new GuildMutes(this.guildId); this.mutes = new GuildMutes(this.guildId);
this.serverLogs = new GuildServerLogs(this.guildId); this.serverLogs = new GuildServerLogs(this.guildId);
@ -63,7 +63,7 @@ export class ModActionsPlugin extends Plugin {
kick_message: "You have been kicked from {guildName}. Reason given: {reason}", kick_message: "You have been kicked from {guildName}. Reason given: {reason}",
ban_message: "You have been banned from {guildName}. Reason given: {reason}", ban_message: "You have been banned from {guildName}. Reason given: {reason}",
log_automatic_actions: true, log_automatic_actions: true,
action_log_channel: null, case_log_channel: null,
alert_on_rejoin: false, alert_on_rejoin: false,
alert_channel: null alert_channel: null
}, },
@ -104,16 +104,16 @@ export class ModActionsPlugin extends Plugin {
const modId = relevantAuditLogEntry.user.id; const modId = relevantAuditLogEntry.user.id;
const auditLogId = relevantAuditLogEntry.id; const auditLogId = relevantAuditLogEntry.id;
await this.createModAction( await this.createCase(
user.id, user.id,
modId, modId,
ModActionType.Ban, CaseType.Ban,
auditLogId, auditLogId,
relevantAuditLogEntry.reason, relevantAuditLogEntry.reason,
true true
); );
} else { } else {
await this.createModAction(user.id, null, ModActionType.Ban); await this.createCase(user.id, null, CaseType.Ban);
} }
} }
@ -132,9 +132,9 @@ export class ModActionsPlugin extends Plugin {
const modId = relevantAuditLogEntry.user.id; const modId = relevantAuditLogEntry.user.id;
const auditLogId = relevantAuditLogEntry.id; const auditLogId = relevantAuditLogEntry.id;
await this.createModAction(user.id, modId, ModActionType.Unban, auditLogId, null, true); await this.createCase(user.id, modId, CaseType.Unban, auditLogId, null, true);
} else { } else {
await this.createModAction(user.id, null, ModActionType.Unban); await this.createCase(user.id, null, CaseType.Unban);
} }
} }
@ -148,7 +148,7 @@ export class ModActionsPlugin extends Plugin {
const alertChannelId = this.configValue("alert_channel"); const alertChannelId = this.configValue("alert_channel");
if (!alertChannelId) return; if (!alertChannelId) return;
const actions = await this.modActions.getByUserId(member.id); const actions = await this.cases.getByUserId(member.id);
if (actions.length) { if (actions.length) {
const alertChannel: any = this.guild.channels.get(alertChannelId); const alertChannel: any = this.guild.channels.get(alertChannelId);
@ -166,7 +166,7 @@ export class ModActionsPlugin extends Plugin {
@d.command(/update|updatecase/, "<caseNumber:number> <note:string$>") @d.command(/update|updatecase/, "<caseNumber:number> <note:string$>")
@d.permission("note") @d.permission("note")
async updateCmd(msg: Message, args: any) { async updateCmd(msg: Message, args: any) {
const action = await this.modActions.findByCaseNumber(args.caseNumber); const action = await this.cases.findByCaseNumber(args.caseNumber);
if (!action) { if (!action) {
msg.channel.createMessage("Case not found!"); msg.channel.createMessage("Case not found!");
return; return;
@ -174,20 +174,20 @@ export class ModActionsPlugin extends Plugin {
if (action.mod_id === null) { if (action.mod_id === null) {
// If the action has no moderator information, assume the first one to update it did the action // If the action has no moderator information, assume the first one to update it did the action
await this.modActions.update(action.id, { await this.cases.update(action.id, {
mod_id: msg.author.id, mod_id: msg.author.id,
mod_name: `${msg.author.username}#${msg.author.discriminator}` mod_name: `${msg.author.username}#${msg.author.discriminator}`
}); });
} }
await this.createModActionNote(action.id, msg.author.id, args.note); await this.createCaseNote(action.id, msg.author.id, args.note);
this.postModActionToActionLog(action.id); // Post updated action to action log this.postCaseToCaseLog(action.id); // Post updated case to case log
} }
@d.command("note", "<userId:string> <note:string$>") @d.command("note", "<userId:string> <note:string$>")
@d.permission("note") @d.permission("note")
async noteCmd(msg: Message, args: any) { async noteCmd(msg: Message, args: any) {
await this.createModAction(args.userId, msg.author.id, ModActionType.Note, null, args.note); await this.createCase(args.userId, msg.author.id, CaseType.Note, null, args.note);
} }
@d.command("warn", "<member:Member> <reason:string$>") @d.command("warn", "<member:Member> <reason:string$>")
@ -215,13 +215,7 @@ export class ModActionsPlugin extends Plugin {
} }
} }
await this.createModAction( await this.createCase(args.member.id, msg.author.id, CaseType.Warn, null, args.reason);
args.member.id,
msg.author.id,
ModActionType.Warn,
null,
args.reason
);
msg.channel.createMessage(successMessage("Member warned")); msg.channel.createMessage(successMessage("Member warned"));
@ -259,13 +253,7 @@ export class ModActionsPlugin extends Plugin {
await this.mutes.addOrUpdateMute(args.member.id, muteTime); await this.mutes.addOrUpdateMute(args.member.id, muteTime);
// Create a case for this action // Create a case for this action
await this.createModAction( await this.createCase(args.member.id, msg.author.id, CaseType.Mute, null, args.reason);
args.member.id,
msg.author.id,
ModActionType.Mute,
null,
args.reason
);
// Message the user informing them of the mute // Message the user informing them of the mute
let messageSent = true; let messageSent = true;
@ -335,13 +323,7 @@ export class ModActionsPlugin extends Plugin {
args.member.kick(args.reason); args.member.kick(args.reason);
// Create a case for this action // Create a case for this action
await this.createModAction( await this.createCase(args.member.id, msg.author.id, CaseType.Kick, null, args.reason);
args.member.id,
msg.author.id,
ModActionType.Kick,
null,
args.reason
);
// Confirm the action to the moderator // Confirm the action to the moderator
let response = `Member kicked`; let response = `Member kicked`;
@ -384,7 +366,7 @@ export class ModActionsPlugin extends Plugin {
args.member.ban(1, args.reason); args.member.ban(1, args.reason);
// Create a case for this action // Create a case for this action
await this.createModAction(args.member.id, msg.author.id, ModActionType.Ban, null, args.reason); await this.createCase(args.member.id, msg.author.id, CaseType.Ban, null, args.reason);
// Confirm the action to the moderator // Confirm the action to the moderator
let response = `Member banned`; let response = `Member banned`;
@ -408,25 +390,25 @@ export class ModActionsPlugin extends Plugin {
async showcaseCmd(msg: Message, args: any) { async showcaseCmd(msg: Message, args: any) {
if (args.caseNumberOrUserId.length >= 17) { if (args.caseNumberOrUserId.length >= 17) {
// Assume user id // Assume user id
const actions = await this.modActions.getByUserId(args.caseNumberOrUserId); const actions = await this.cases.getByUserId(args.caseNumberOrUserId);
if (actions.length === 0) { if (actions.length === 0) {
msg.channel.createMessage("No cases found for the specified user!"); msg.channel.createMessage("No cases found for the specified user!");
} else { } else {
for (const action of actions) { for (const action of actions) {
await this.displayModAction(action, msg.channel.id); await this.displayCase(action, msg.channel.id);
} }
} }
} else { } else {
// Assume case id // Assume case id
const action = await this.modActions.findByCaseNumber(args.caseNumberOrUserId); const action = await this.cases.findByCaseNumber(args.caseNumberOrUserId);
if (!action) { if (!action) {
msg.channel.createMessage("Case not found!"); msg.channel.createMessage("Case not found!");
return; return;
} }
this.displayModAction(action.id, msg.channel.id); this.displayCase(action.id, msg.channel.id);
} }
} }
@ -479,36 +461,36 @@ export class ModActionsPlugin extends Plugin {
* Shows information about the specified action in a message embed. * Shows information about the specified action in a message embed.
* If no channelId is specified, uses the channel id from config. * If no channelId is specified, uses the channel id from config.
*/ */
protected async displayModAction(actionOrId: ModAction | number, channelId: string) { protected async displayCase(caseOrCaseId: Case | number, channelId: string) {
let action: ModAction; let theCase: Case;
if (typeof actionOrId === "number") { if (typeof caseOrCaseId === "number") {
action = await this.modActions.find(actionOrId); theCase = await this.cases.find(caseOrCaseId);
} else { } else {
action = actionOrId; theCase = caseOrCaseId;
} }
if (!action) return; if (!theCase) return;
if (!this.guild.channels.get(channelId)) return; if (!this.guild.channels.get(channelId)) return;
const notes = await this.modActions.getActionNotes(action.id); const notes = await this.cases.getCaseNotes(theCase.id);
const createdAt = moment(action.created_at); const createdAt = moment(theCase.created_at);
const actionTypeStr = ModActionType[action.action_type].toUpperCase(); const actionTypeStr = CaseType[theCase.type].toUpperCase();
const embed: any = { const embed: any = {
title: `${actionTypeStr} - Case #${action.case_number}`, title: `${actionTypeStr} - Case #${theCase.case_number}`,
footer: { footer: {
text: `Case created at ${createdAt.format("YYYY-MM-DD [at] HH:mm")}` text: `Case created at ${createdAt.format("YYYY-MM-DD [at] HH:mm")}`
}, },
fields: [ fields: [
{ {
name: "User", name: "User",
value: `${action.user_name}\n<@!${action.user_id}>`, value: `${theCase.user_name}\n<@!${theCase.user_id}>`,
inline: true inline: true
}, },
{ {
name: "Moderator", name: "Moderator",
value: `${action.mod_name}\n<@!${action.mod_id}>`, value: `${theCase.mod_name}\n<@!${theCase.mod_id}>`,
inline: true inline: true
} }
] ]
@ -537,20 +519,19 @@ export class ModActionsPlugin extends Plugin {
}); });
} }
(this.bot.guilds.get(this.guildId).channels.get(channelId) as TextChannel).createMessage({ const channel = this.guild.channels.get(channelId) as TextChannel;
embed channel.createMessage({ embed });
});
} }
/** /**
* Posts the specified mod action to the guild's action log channel * Posts the specified mod action to the guild's action log channel
*/ */
protected async postModActionToActionLog(actionOrId: ModAction | number) { protected async postCaseToCaseLog(caseOrCaseId: Case | number) {
const actionLogChannelId = this.configValue("action_log_channel"); const caseLogChannelId = this.configValue("case_log_channel");
if (!actionLogChannelId) return; if (!caseLogChannelId) return;
if (!this.guild.channels.get(actionLogChannelId)) return; if (!this.guild.channels.get(caseLogChannelId)) return;
return this.displayModAction(actionOrId, actionLogChannelId); return this.displayCase(caseOrCaseId, caseLogChannelId);
} }
/** /**
@ -577,10 +558,10 @@ export class ModActionsPlugin extends Plugin {
}); });
} }
protected async createModAction( protected async createCase(
userId: string, userId: string,
modId: string, modId: string,
actionType: ModActionType, caseType: CaseType,
auditLogId: string = null, auditLogId: string = null,
reason: string = null, reason: string = null,
automatic = false automatic = false
@ -591,36 +572,36 @@ export class ModActionsPlugin extends Plugin {
const mod = this.bot.users.get(modId); const mod = this.bot.users.get(modId);
const modName = mod ? `${mod.username}#${mod.discriminator}` : "Unknown#0000"; const modName = mod ? `${mod.username}#${mod.discriminator}` : "Unknown#0000";
const createdId = await this.modActions.create({ const createdId = await this.cases.create({
type: caseType,
user_id: userId, user_id: userId,
user_name: userName, user_name: userName,
mod_id: modId, mod_id: modId,
mod_name: modName, mod_name: modName,
action_type: actionType,
audit_log_id: auditLogId audit_log_id: auditLogId
}); });
if (reason) { if (reason) {
await this.createModActionNote(createdId, modId, reason); await this.createCaseNote(createdId, modId, reason);
} }
if ( if (
this.configValue("action_log_channel") && this.configValue("case_log_channel") &&
(!automatic || this.configValue("log_automatic_actions")) (!automatic || this.configValue("log_automatic_actions"))
) { ) {
try { try {
await this.postModActionToActionLog(createdId); await this.postCaseToCaseLog(createdId);
} catch (e) {} // tslint:disable-line } catch (e) {} // tslint:disable-line
} }
return createdId; return createdId;
} }
protected async createModActionNote(modActionId: number, modId: string, body: string) { protected async createCaseNote(caseId: number, modId: string, body: string) {
const mod = this.bot.users.get(modId); const mod = this.bot.users.get(modId);
const modName = mod ? `${mod.username}#${mod.discriminator}` : "Unknown#0000"; const modName = mod ? `${mod.username}#${mod.discriminator}` : "Unknown#0000";
return this.modActions.createNote(modActionId, { return this.cases.createNote(caseId, {
mod_id: modId, mod_id: modId,
mod_name: modName, mod_name: modName,
body: body || "" body: body || ""