feat: handle template errors
Fixes ZDEV-20
This commit is contained in:
parent
2ce5082018
commit
ffa9eeb3f5
14 changed files with 231 additions and 94 deletions
|
@ -1,8 +1,9 @@
|
|||
import { ChannelType, GuildTextThreadCreateOptions, ThreadAutoArchiveDuration, ThreadChannel } from "discord.js";
|
||||
import z from "zod";
|
||||
import { TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter";
|
||||
import { TemplateParseError, TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter";
|
||||
import { MINUTES, convertDelayStringToMS, noop, zBoundedCharacters, zDelayString } from "../../../utils";
|
||||
import { savedMessageToTemplateSafeSavedMessage, userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
|
||||
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
||||
import { automodAction } from "../helpers";
|
||||
|
||||
const validThreadAutoArchiveDurations: ThreadAutoArchiveDuration[] = [
|
||||
|
@ -21,7 +22,7 @@ export const StartThreadAction = automodAction({
|
|||
limit_per_channel: z.number().nullable().default(5),
|
||||
}),
|
||||
|
||||
async apply({ pluginData, contexts, actionConfig }) {
|
||||
async apply({ pluginData, contexts, actionConfig, ruleName }) {
|
||||
// check if the message still exists, we don't want to create threads for deleted messages
|
||||
const threads = contexts.filter((c) => {
|
||||
if (!c.message || !c.user) return false;
|
||||
|
@ -48,15 +49,25 @@ export const StartThreadAction = automodAction({
|
|||
const channel = pluginData.guild.channels.cache.get(threadContext.message!.channel_id);
|
||||
if (!channel || !("threads" in channel) || channel.isThreadOnly()) continue;
|
||||
|
||||
const renderThreadName = async (str: string) =>
|
||||
renderTemplate(
|
||||
str,
|
||||
let threadName: string;
|
||||
try {
|
||||
threadName = await renderTemplate(
|
||||
actionConfig.name ?? "{user.renderedUsername}'s thread",
|
||||
new TemplateSafeValueContainer({
|
||||
user: userToTemplateSafeUser(threadContext.user!),
|
||||
msg: savedMessageToTemplateSafeSavedMessage(threadContext.message!),
|
||||
}),
|
||||
);
|
||||
const threadName = await renderThreadName(actionConfig.name ?? "{user.renderedUsername}'s thread");
|
||||
} catch (err) {
|
||||
if (err instanceof TemplateParseError) {
|
||||
pluginData.getPlugin(LogsPlugin).logBotAlert({
|
||||
body: `Error in thread name format of automod rule ${ruleName}: ${err.message}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const threadOptions: GuildTextThreadCreateOptions<unknown> = {
|
||||
name: threadName,
|
||||
autoArchiveDuration: autoArchive,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue