3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-14 22:05:01 +00:00

feat: handle template errors

Fixes ZDEV-20
This commit is contained in:
Dragory 2024-01-27 16:01:48 +02:00
parent 2ce5082018
commit ffa9eeb3f5
No known key found for this signature in database
14 changed files with 231 additions and 94 deletions

View file

@ -11,6 +11,7 @@ import {
messageToTemplateSafeMessage,
userToTemplateSafeUser,
} from "../../utils/templateSafeObjects";
import { LogsPlugin } from "../Logs/LogsPlugin";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { runEvent } from "./functions/runEvent";
import { CustomEventsPluginType, zCustomEventsConfig } from "./types";
@ -25,6 +26,7 @@ export const CustomEventsPlugin = zeppelinGuildPlugin<CustomEventsPluginType>()(
name: "custom_events",
showInDocs: false,
dependencies: () => [LogsPlugin],
configParser: (input) => zCustomEventsConfig.parse(input),
defaultOptions,

View file

@ -4,6 +4,7 @@ import { canActOn } from "../../../pluginUtils";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
import { resolveMember, zSnowflake } from "../../../utils";
import { ActionError } from "../ActionError";
import { catchTemplateError } from "../catchTemplateError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
export const zAddRoleAction = z.strictObject({
@ -20,7 +21,10 @@ export async function addRoleAction(
event: TCustomEvent,
eventData: any,
) {
const targetId = await renderTemplate(action.target, values, false);
const targetId = await catchTemplateError(
() => renderTemplate(action.target, values, false),
"Invalid target format",
);
const target = await resolveMember(pluginData.client, pluginData.guild, targetId);
if (!target) throw new ActionError(`Unknown target member: ${targetId}`);

View file

@ -5,6 +5,7 @@ import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFor
import { zBoundedCharacters, zSnowflake } from "../../../utils";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { ActionError } from "../ActionError";
import { catchTemplateError } from "../catchTemplateError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
export const zCreateCaseAction = z.strictObject({
@ -23,10 +24,12 @@ export async function createCaseAction(
event: TCustomEvent,
eventData: any, // eslint-disable-line @typescript-eslint/no-unused-vars
) {
const modId = await renderTemplate(action.mod, values, false);
const targetId = await renderTemplate(action.target, values, false);
const reason = await renderTemplate(action.reason, values, false);
const modId = await catchTemplateError(() => renderTemplate(action.mod, values, false), "Invalid mod format");
const targetId = await catchTemplateError(
() => renderTemplate(action.target, values, false),
"Invalid target format",
);
const reason = await catchTemplateError(() => renderTemplate(action.reason, values, false), "Invalid reason format");
if (CaseTypes[action.case_type] == null) {
throw new ActionError(`Invalid case type: ${action.type}`);

View file

@ -4,6 +4,7 @@ import z from "zod";
import { TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter";
import { zBoundedCharacters, zSnowflake } from "../../../utils";
import { ActionError } from "../ActionError";
import { catchTemplateError } from "../catchTemplateError";
import { CustomEventsPluginType } from "../types";
export const zMessageAction = z.strictObject({
@ -18,7 +19,10 @@ export async function messageAction(
action: TMessageAction,
values: TemplateSafeValueContainer,
) {
const targetChannelId = await renderTemplate(action.channel, values, false);
const targetChannelId = await catchTemplateError(
() => renderTemplate(action.channel, values, false),
"Invalid channel format",
);
const targetChannel = pluginData.guild.channels.cache.get(targetChannelId as Snowflake);
if (!targetChannel) throw new ActionError("Unknown target channel");
if (!(targetChannel instanceof TextChannel)) throw new ActionError("Target channel is not a text channel");

View file

@ -5,6 +5,7 @@ import { canActOn } from "../../../pluginUtils";
import { TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter";
import { resolveMember, zSnowflake } from "../../../utils";
import { ActionError } from "../ActionError";
import { catchTemplateError } from "../catchTemplateError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
export const zMoveToVoiceChannelAction = z.strictObject({
@ -21,7 +22,10 @@ export async function moveToVoiceChannelAction(
event: TCustomEvent,
eventData: any,
) {
const targetId = await renderTemplate(action.target, values, false);
const targetId = await catchTemplateError(
() => renderTemplate(action.target, values, false),
"Invalid target format",
);
const target = await resolveMember(pluginData.client, pluginData.guild, targetId);
if (!target) throw new ActionError("Unknown target member");
@ -29,7 +33,10 @@ export async function moveToVoiceChannelAction(
throw new ActionError("Missing permissions");
}
const targetChannelId = await renderTemplate(action.channel, values, false);
const targetChannelId = await catchTemplateError(
() => renderTemplate(action.channel, values, false),
"Invalid channel format",
);
const targetChannel = pluginData.guild.channels.cache.get(targetChannelId as Snowflake);
if (!targetChannel) throw new ActionError("Unknown target channel");
if (!(targetChannel instanceof VoiceChannel)) throw new ActionError("Target channel is not a voice channel");

View file

@ -0,0 +1,13 @@
import { TemplateParseError } from "../../templateFormatter";
import { ActionError } from "./ActionError";
export function catchTemplateError(fn: () => Promise<string>, errorText: string): Promise<string> {
try {
return fn();
} catch (err) {
if (err instanceof TemplateParseError) {
throw new ActionError(`${errorText}: ${err.message}`);
}
throw err;
}
}