From 07d33a2dc50738f20a64587b664a585a715f148e Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sat, 1 Apr 2023 13:54:06 +0300 Subject: [PATCH] fix(automod): start_thread action thread options --- .../plugins/Automod/actions/startThread.ts | 51 ++++++++++++++----- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/backend/src/plugins/Automod/actions/startThread.ts b/backend/src/plugins/Automod/actions/startThread.ts index db0c5e13..055c9493 100644 --- a/backend/src/plugins/Automod/actions/startThread.ts +++ b/backend/src/plugins/Automod/actions/startThread.ts @@ -1,4 +1,10 @@ -import { ChannelType, GuildFeature, ThreadAutoArchiveDuration } from "discord.js"; +import { + ChannelType, + GuildFeature, + GuildTextThreadCreateOptions, + ThreadAutoArchiveDuration, + ThreadChannel, +} from "discord.js"; import * as t from "io-ts"; import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter"; import { convertDelayStringToMS, MINUTES, noop, tDelayString, tNullable } from "../../../utils"; @@ -51,7 +57,7 @@ export const StartThreadAction = automodAction({ for (const threadContext of threads) { const channel = pluginData.guild.channels.cache.get(threadContext.message!.channel_id); - if (!channel || !("threads" in channel)) continue; + if (!channel || !("threads" in channel) || channel.type === ChannelType.GuildForum) continue; const renderThreadName = async (str: string) => renderTemplate( @@ -62,18 +68,35 @@ export const StartThreadAction = automodAction({ }), ); const threadName = await renderThreadName(actionConfig.name ?? "{user.tag}s thread"); - const thread = await channel.threads - .create({ - name: threadName, - autoArchiveDuration: autoArchive, - // @ts-expect-error FIXME - type: actionConfig.private ? ChannelType.PrivateThread : ChannelType.PublicThread, - startMessage: - !actionConfig.private && guild.features.includes(GuildFeature.PrivateThreads) - ? threadContext.message!.id - : undefined, - }) - .catch(noop); + const threadOptions: GuildTextThreadCreateOptions = { + name: threadName, + autoArchiveDuration: autoArchive, + startMessage: + !actionConfig.private && guild.features.includes(GuildFeature.PrivateThreads) + ? threadContext.message!.id + : undefined, + }; + + let thread: ThreadChannel | undefined; + if (channel.type === ChannelType.GuildNews) { + thread = await channel.threads + .create({ + ...threadOptions, + type: ChannelType.AnnouncementThread, + }) + .catch(() => undefined); + } else { + thread = await channel.threads + .create({ + ...threadOptions, + type: actionConfig.private ? ChannelType.PrivateThread : ChannelType.PublicThread, + startMessage: + !actionConfig.private && guild.features.includes(GuildFeature.PrivateThreads) + ? threadContext.message!.id + : undefined, + }) + .catch(() => undefined); + } if (actionConfig.slowmode && thread) { const dur = Math.ceil(Math.max(convertDelayStringToMS(actionConfig.slowmode) ?? 0, 0) / 1000); if (dur > 0) {