zappyzep/backend/src/plugins/ModActions/events/CreateBanCaseOnManualBanEvt.ts

74 lines
2.5 KiB
TypeScript
Raw Normal View History

import { IgnoredEventType, modActionsEvt } from "../types";
2020-07-23 00:37:33 +03:00
import { isEventIgnored } from "../functions/isEventIgnored";
import { clearIgnoredEvents } from "../functions/clearIgnoredEvents";
import { Constants as ErisConstants, User } from "eris";
2020-07-23 00:37:33 +03:00
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { CaseTypes } from "../../../data/CaseTypes";
import { safeFindRelevantAuditLogEntry } from "../../../utils/safeFindRelevantAuditLogEntry";
import { LogType } from "../../../data/LogType";
import { stripObjectToScalars, resolveUser, UnknownUser } from "../../../utils";
import { Case } from "../../../data/entities/Case";
2020-07-23 00:37:33 +03:00
/**
* Create a BAN case automatically when a user is banned manually.
* Attempts to find the ban's details in the audit log.
*/
export const CreateBanCaseOnManualBanEvt = modActionsEvt(
2020-07-23 00:37:33 +03:00
"guildBanAdd",
async ({ pluginData, args: { guild, user } }) => {
if (isEventIgnored(pluginData, IgnoredEventType.Ban, user.id)) {
clearIgnoredEvents(pluginData, IgnoredEventType.Ban, user.id);
2020-07-23 00:37:33 +03:00
return;
}
const relevantAuditLogEntry = await safeFindRelevantAuditLogEntry(
pluginData,
ErisConstants.AuditLogActions.MEMBER_BAN_ADD,
user.id,
);
const casesPlugin = pluginData.getPlugin(CasesPlugin);
let createdCase: Case | null = null;
let mod: User | UnknownUser | null = null;
let reason = "";
2020-07-23 00:37:33 +03:00
if (relevantAuditLogEntry) {
const modId = relevantAuditLogEntry.user.id;
const auditLogId = relevantAuditLogEntry.id;
mod = await resolveUser(pluginData.client, modId);
const config = mod instanceof UnknownUser ? pluginData.config.get() : pluginData.config.getForUser(mod);
if (config.create_cases_for_manual_actions) {
reason = relevantAuditLogEntry.reason || "";
createdCase = await casesPlugin.createCase({
userId: user.id,
modId,
type: CaseTypes.Ban,
auditLogId,
reason: reason || undefined,
automatic: true,
});
}
2020-07-23 00:37:33 +03:00
} else {
const config = pluginData.config.get();
if (config.create_cases_for_manual_actions) {
createdCase = await casesPlugin.createCase({
userId: user.id,
modId: "0",
type: CaseTypes.Ban,
});
}
2020-07-23 00:37:33 +03:00
}
pluginData.state.serverLogs.log(LogType.MEMBER_BAN, {
mod: mod ? stripObjectToScalars(mod, ["user"]) : null,
user: stripObjectToScalars(user, ["user"]),
caseNumber: createdCase?.case_number ?? 0,
reason,
});
2020-07-23 00:37:33 +03:00
},
);