From e6f296381e10ea22d0d8aee534a9019dc66c2991 Mon Sep 17 00:00:00 2001 From: Dragory Date: Thu, 12 Jul 2018 02:53:26 +0300 Subject: [PATCH] Rename ModActions to Cases --- ...80712020400_rename_mod_actions_to_cases.js | 21 +++ src/data/{ModActionType.ts => CaseType.ts} | 2 +- src/data/GuildCases.ts | 76 +++++++++++ src/data/GuildModActions.ts | 76 ----------- src/data/GuildMutes.ts | 2 +- src/models/{ModAction.ts => Case.ts} | 4 +- src/plugins/ModActions.ts | 125 ++++++++---------- 7 files changed, 154 insertions(+), 152 deletions(-) create mode 100644 migrations/20180712020400_rename_mod_actions_to_cases.js rename src/data/{ModActionType.ts => CaseType.ts} (65%) create mode 100644 src/data/GuildCases.ts delete mode 100644 src/data/GuildModActions.ts rename src/models/{ModAction.ts => Case.ts} (78%) diff --git a/migrations/20180712020400_rename_mod_actions_to_cases.js b/migrations/20180712020400_rename_mod_actions_to_cases.js new file mode 100644 index 00000000..07d05d01 --- /dev/null +++ b/migrations/20180712020400_rename_mod_actions_to_cases.js @@ -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'); +}; diff --git a/src/data/ModActionType.ts b/src/data/CaseType.ts similarity index 65% rename from src/data/ModActionType.ts rename to src/data/CaseType.ts index 6d20314e..355e098a 100644 --- a/src/data/ModActionType.ts +++ b/src/data/CaseType.ts @@ -1,4 +1,4 @@ -export enum ModActionType { +export enum CaseType { Ban = 1, Unban, Note, diff --git a/src/data/GuildCases.ts b/src/data/GuildCases.ts new file mode 100644 index 00000000..f8ef564e --- /dev/null +++ b/src/data/GuildCases.ts @@ -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 { + 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 { + 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 { + const results = await knex("case_notes") + .where("case_id", caseId) + .select(); + + return results.map(r => new Case(r)); + } + + async getByUserId(userId: string): Promise { + 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 { + 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(); + } +} diff --git a/src/data/GuildModActions.ts b/src/data/GuildModActions.ts deleted file mode 100644 index 2d4eead3..00000000 --- a/src/data/GuildModActions.ts +++ /dev/null @@ -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 { - 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 { - 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 { - 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 { - 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 { - 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(); - } -} diff --git a/src/data/GuildMutes.ts b/src/data/GuildMutes.ts index 877fc935..e167b819 100644 --- a/src/data/GuildMutes.ts +++ b/src/data/GuildMutes.ts @@ -1,4 +1,4 @@ -import knex from "../knex"; +import * as knex from "../knex"; import * as moment from "moment-timezone"; import Mute from "../models/Mute"; diff --git a/src/models/ModAction.ts b/src/models/Case.ts similarity index 78% rename from src/models/ModAction.ts rename to src/models/Case.ts index 9951c29c..5b0cd5a5 100644 --- a/src/models/ModAction.ts +++ b/src/models/Case.ts @@ -1,6 +1,6 @@ import Model from "./Model"; -export default class ModAction extends Model { +export default class Case extends Model { public id: number; public guild_id: string; public case_number: number; @@ -8,7 +8,7 @@ export default class ModAction extends Model { public user_name: string; public mod_id: string; public mod_name: string; - public action_type: number; + public type: number; public audit_log_id: string; public created_at: string; } diff --git a/src/plugins/ModActions.ts b/src/plugins/ModActions.ts index ec033cb9..06957aa1 100644 --- a/src/plugins/ModActions.ts +++ b/src/plugins/ModActions.ts @@ -2,7 +2,7 @@ import { Plugin, decorators as d, waitForReaction } from "knub"; import { Guild, GuildAuditLogEntry, Member, Message, TextChannel, User } from "eris"; import * as moment from "moment-timezone"; import * as humanizeDuration from "humanize-duration"; -import { GuildModActions } from "../data/GuildModActions"; +import { GuildCases } from "../data/GuildCases"; import { convertDelayStringToMS, errorMessage, @@ -12,8 +12,8 @@ import { } from "../utils"; import { GuildMutes } from "../data/GuildMutes"; import Timer = NodeJS.Timer; -import ModAction from "../models/ModAction"; -import { ModActionType } from "../data/ModActionType"; +import Case from "../models/Case"; +import { CaseType } from "../data/CaseType"; import { GuildServerLogs } from "../data/GuildServerLogs"; import { LogType } from "../data/LogType"; @@ -24,14 +24,14 @@ const sleep = (ms: number): Promise => { }; export class ModActionsPlugin extends Plugin { - protected modActions: GuildModActions; + protected cases: GuildCases; protected mutes: GuildMutes; protected serverLogs: GuildServerLogs; protected muteClearIntervalId: Timer; async onLoad() { - this.modActions = new GuildModActions(this.guildId); + this.cases = new GuildCases(this.guildId); this.mutes = new GuildMutes(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}", ban_message: "You have been banned from {guildName}. Reason given: {reason}", log_automatic_actions: true, - action_log_channel: null, + case_log_channel: null, alert_on_rejoin: false, alert_channel: null }, @@ -104,16 +104,16 @@ export class ModActionsPlugin extends Plugin { const modId = relevantAuditLogEntry.user.id; const auditLogId = relevantAuditLogEntry.id; - await this.createModAction( + await this.createCase( user.id, modId, - ModActionType.Ban, + CaseType.Ban, auditLogId, relevantAuditLogEntry.reason, true ); } 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 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 { - 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"); if (!alertChannelId) return; - const actions = await this.modActions.getByUserId(member.id); + const actions = await this.cases.getByUserId(member.id); if (actions.length) { const alertChannel: any = this.guild.channels.get(alertChannelId); @@ -166,7 +166,7 @@ export class ModActionsPlugin extends Plugin { @d.command(/update|updatecase/, " ") @d.permission("note") async updateCmd(msg: Message, args: any) { - const action = await this.modActions.findByCaseNumber(args.caseNumber); + const action = await this.cases.findByCaseNumber(args.caseNumber); if (!action) { msg.channel.createMessage("Case not found!"); return; @@ -174,20 +174,20 @@ export class ModActionsPlugin extends Plugin { if (action.mod_id === null) { // 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_name: `${msg.author.username}#${msg.author.discriminator}` }); } - await this.createModActionNote(action.id, msg.author.id, args.note); - this.postModActionToActionLog(action.id); // Post updated action to action log + await this.createCaseNote(action.id, msg.author.id, args.note); + this.postCaseToCaseLog(action.id); // Post updated case to case log } @d.command("note", " ") @d.permission("note") 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", " ") @@ -215,13 +215,7 @@ export class ModActionsPlugin extends Plugin { } } - await this.createModAction( - args.member.id, - msg.author.id, - ModActionType.Warn, - null, - args.reason - ); + await this.createCase(args.member.id, msg.author.id, CaseType.Warn, null, args.reason); msg.channel.createMessage(successMessage("Member warned")); @@ -259,13 +253,7 @@ export class ModActionsPlugin extends Plugin { await this.mutes.addOrUpdateMute(args.member.id, muteTime); // Create a case for this action - await this.createModAction( - args.member.id, - msg.author.id, - ModActionType.Mute, - null, - args.reason - ); + await this.createCase(args.member.id, msg.author.id, CaseType.Mute, null, args.reason); // Message the user informing them of the mute let messageSent = true; @@ -335,13 +323,7 @@ export class ModActionsPlugin extends Plugin { args.member.kick(args.reason); // Create a case for this action - await this.createModAction( - args.member.id, - msg.author.id, - ModActionType.Kick, - null, - args.reason - ); + await this.createCase(args.member.id, msg.author.id, CaseType.Kick, null, args.reason); // Confirm the action to the moderator let response = `Member kicked`; @@ -384,7 +366,7 @@ export class ModActionsPlugin extends Plugin { args.member.ban(1, args.reason); // 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 let response = `Member banned`; @@ -408,25 +390,25 @@ export class ModActionsPlugin extends Plugin { async showcaseCmd(msg: Message, args: any) { if (args.caseNumberOrUserId.length >= 17) { // Assume user id - const actions = await this.modActions.getByUserId(args.caseNumberOrUserId); + const actions = await this.cases.getByUserId(args.caseNumberOrUserId); if (actions.length === 0) { msg.channel.createMessage("No cases found for the specified user!"); } else { for (const action of actions) { - await this.displayModAction(action, msg.channel.id); + await this.displayCase(action, msg.channel.id); } } } else { // Assume case id - const action = await this.modActions.findByCaseNumber(args.caseNumberOrUserId); + const action = await this.cases.findByCaseNumber(args.caseNumberOrUserId); if (!action) { msg.channel.createMessage("Case not found!"); 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. * If no channelId is specified, uses the channel id from config. */ - protected async displayModAction(actionOrId: ModAction | number, channelId: string) { - let action: ModAction; - if (typeof actionOrId === "number") { - action = await this.modActions.find(actionOrId); + protected async displayCase(caseOrCaseId: Case | number, channelId: string) { + let theCase: Case; + if (typeof caseOrCaseId === "number") { + theCase = await this.cases.find(caseOrCaseId); } else { - action = actionOrId; + theCase = caseOrCaseId; } - if (!action) return; + if (!theCase) 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 actionTypeStr = ModActionType[action.action_type].toUpperCase(); + const createdAt = moment(theCase.created_at); + const actionTypeStr = CaseType[theCase.type].toUpperCase(); const embed: any = { - title: `${actionTypeStr} - Case #${action.case_number}`, + title: `${actionTypeStr} - Case #${theCase.case_number}`, footer: { text: `Case created at ${createdAt.format("YYYY-MM-DD [at] HH:mm")}` }, fields: [ { name: "User", - value: `${action.user_name}\n<@!${action.user_id}>`, + value: `${theCase.user_name}\n<@!${theCase.user_id}>`, inline: true }, { name: "Moderator", - value: `${action.mod_name}\n<@!${action.mod_id}>`, + value: `${theCase.mod_name}\n<@!${theCase.mod_id}>`, inline: true } ] @@ -537,20 +519,19 @@ export class ModActionsPlugin extends Plugin { }); } - (this.bot.guilds.get(this.guildId).channels.get(channelId) as TextChannel).createMessage({ - embed - }); + const channel = this.guild.channels.get(channelId) as TextChannel; + channel.createMessage({ embed }); } /** * Posts the specified mod action to the guild's action log channel */ - protected async postModActionToActionLog(actionOrId: ModAction | number) { - const actionLogChannelId = this.configValue("action_log_channel"); - if (!actionLogChannelId) return; - if (!this.guild.channels.get(actionLogChannelId)) return; + protected async postCaseToCaseLog(caseOrCaseId: Case | number) { + const caseLogChannelId = this.configValue("case_log_channel"); + if (!caseLogChannelId) 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, modId: string, - actionType: ModActionType, + caseType: CaseType, auditLogId: string = null, reason: string = null, automatic = false @@ -591,36 +572,36 @@ export class ModActionsPlugin extends Plugin { const mod = this.bot.users.get(modId); 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_name: userName, mod_id: modId, mod_name: modName, - action_type: actionType, audit_log_id: auditLogId }); if (reason) { - await this.createModActionNote(createdId, modId, reason); + await this.createCaseNote(createdId, modId, reason); } if ( - this.configValue("action_log_channel") && + this.configValue("case_log_channel") && (!automatic || this.configValue("log_automatic_actions")) ) { try { - await this.postModActionToActionLog(createdId); + await this.postCaseToCaseLog(createdId); } catch (e) {} // tslint:disable-line } 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 modName = mod ? `${mod.username}#${mod.discriminator}` : "Unknown#0000"; - return this.modActions.createNote(modActionId, { + return this.cases.createNote(caseId, { mod_id: modId, mod_name: modName, body: body || ""