2020-07-22 22:08:49 +03:00
|
|
|
import { CaseArgs, CasesPluginType } from "../types";
|
|
|
|
import { resolveUser } from "../../../utils";
|
2020-10-01 01:43:38 +03:00
|
|
|
import { GuildPluginData } from "knub";
|
2020-07-22 22:08:49 +03:00
|
|
|
import { createCaseNote } from "./createCaseNote";
|
2020-07-22 22:56:21 +03:00
|
|
|
import { postCaseToCaseLogChannel } from "./postToCaseLogChannel";
|
|
|
|
import { logger } from "../../../logger";
|
2020-07-22 22:08:49 +03:00
|
|
|
|
2020-10-01 01:43:38 +03:00
|
|
|
export async function createCase(pluginData: GuildPluginData<CasesPluginType>, args: CaseArgs) {
|
2020-07-22 22:08:49 +03:00
|
|
|
const user = await resolveUser(pluginData.client, args.userId);
|
|
|
|
const userName = `${user.username}#${user.discriminator}`;
|
|
|
|
|
|
|
|
const mod = await resolveUser(pluginData.client, args.modId);
|
|
|
|
const modName = `${mod.username}#${mod.discriminator}`;
|
|
|
|
|
|
|
|
let ppName = null;
|
|
|
|
if (args.ppId) {
|
|
|
|
const pp = await resolveUser(pluginData.client, args.ppId);
|
|
|
|
ppName = `${pp.username}#${pp.discriminator}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.auditLogId) {
|
|
|
|
const existingAuditLogCase = await pluginData.state.cases.findByAuditLogId(args.auditLogId);
|
|
|
|
if (existingAuditLogCase) {
|
|
|
|
delete args.auditLogId;
|
2020-07-22 22:56:21 +03:00
|
|
|
logger.warn(`Duplicate audit log ID for mod case: ${args.auditLogId}`);
|
2020-07-22 22:08:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const createdCase = await pluginData.state.cases.create({
|
|
|
|
type: args.type,
|
|
|
|
user_id: args.userId,
|
|
|
|
user_name: userName,
|
|
|
|
mod_id: args.modId,
|
|
|
|
mod_name: modName,
|
|
|
|
audit_log_id: args.auditLogId,
|
|
|
|
pp_id: args.ppId,
|
|
|
|
pp_name: ppName,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (args.reason || (args.noteDetails && args.noteDetails.length)) {
|
|
|
|
await createCaseNote(pluginData, {
|
|
|
|
caseId: createdCase.id,
|
|
|
|
modId: args.modId,
|
|
|
|
body: args.reason || "",
|
|
|
|
automatic: args.automatic,
|
|
|
|
postInCaseLogOverride: false,
|
|
|
|
noteDetails: args.noteDetails,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.extraNotes) {
|
|
|
|
for (const extraNote of args.extraNotes) {
|
|
|
|
await createCaseNote(pluginData, {
|
|
|
|
caseId: createdCase.id,
|
|
|
|
modId: args.modId,
|
|
|
|
body: extraNote,
|
|
|
|
automatic: args.automatic,
|
|
|
|
postInCaseLogOverride: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = pluginData.config.get();
|
|
|
|
|
|
|
|
if (
|
|
|
|
config.case_log_channel &&
|
|
|
|
(!args.automatic || config.log_automatic_actions) &&
|
|
|
|
args.postInCaseLogOverride !== false
|
|
|
|
) {
|
2020-07-22 22:56:21 +03:00
|
|
|
await postCaseToCaseLogChannel(pluginData, createdCase);
|
2020-07-22 22:08:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return createdCase;
|
|
|
|
}
|