zappyzep/backend/src/plugins/CustomEvents/actions/createCaseAction.ts
2020-07-30 02:47:47 +03:00

41 lines
1.3 KiB
TypeScript

import { PluginData } from "knub";
import { CustomEventsPluginType, TCustomEvent } from "../types";
import * as t from "io-ts";
import { renderTemplate } from "../../../templateFormatter";
import { CaseTypes } from "../../../data/CaseTypes";
import { ActionError } from "../ActionError";
import { CasesPlugin } from "../../Cases/CasesPlugin";
export const CreateCaseAction = t.type({
type: t.literal("create_case"),
case_type: t.string,
mod: t.string,
target: t.string,
reason: t.string,
});
export type TCreateCaseAction = t.TypeOf<typeof CreateCaseAction>;
export async function createCaseAction(
pluginData: PluginData<CustomEventsPluginType>,
action: TCreateCaseAction,
values: any,
event: TCustomEvent,
eventData: any,
) {
const modId = await renderTemplate(action.mod, values, false);
const targetId = await renderTemplate(action.target, values, false);
const reason = await renderTemplate(action.reason, values, false);
if (CaseTypes[action.case_type] == null) {
throw new ActionError(`Invalid case type: ${action.type}`);
}
const casesPlugin = pluginData.getPlugin(CasesPlugin);
await casesPlugin.createCase({
userId: targetId,
modId,
type: CaseTypes[action.case_type],
reason: `__[${event.name}]__ ${reason}`,
});
}