diff --git a/backend/src/plugins/Automod/actions/archiveThread.ts b/backend/src/plugins/Automod/actions/archiveThread.ts index 6fe871a5..12f47131 100644 --- a/backend/src/plugins/Automod/actions/archiveThread.ts +++ b/backend/src/plugins/Automod/actions/archiveThread.ts @@ -1,19 +1,27 @@ import { ThreadChannel } from "discord.js"; import * as t from "io-ts"; -import { noop } from "../../../utils"; +import { noop, tNullable } from "../../../utils"; import { automodAction } from "../helpers"; export const ArchiveThreadAction = automodAction({ - configType: t.type({}), - defaultConfig: {}, + configType: t.type({ + lock: tNullable(t.boolean), + }), + defaultConfig: { + lock: false, + }, - async apply({ pluginData, contexts }) { + async apply({ pluginData, contexts, actionConfig }) { const threads = contexts - .filter((c) => c.message?.channel_id) - .map((c) => pluginData.guild.channels.cache.get(c.message!.channel_id)) + .filter((c) => c.thread?.id) + .map((c) => pluginData.guild.channels.cache.get(c.thread!.id)) .filter((c): c is ThreadChannel => c?.isThread() ?? false); for (const thread of threads) { + if (actionConfig.lock && !thread.locked) { + await thread.setLocked().catch(noop); + } + if (thread.archived) continue; await thread.setArchived().catch(noop); } }, diff --git a/backend/src/plugins/Automod/actions/availableActions.ts b/backend/src/plugins/Automod/actions/availableActions.ts index 76b2a60e..24ebfb7c 100644 --- a/backend/src/plugins/Automod/actions/availableActions.ts +++ b/backend/src/plugins/Automod/actions/availableActions.ts @@ -15,6 +15,7 @@ import { ReplyAction } from "./reply"; import { SetAntiraidLevelAction } from "./setAntiraidLevel"; import { SetCounterAction } from "./setCounter"; import { SetSlowmodeAction } from "./setSlowmode"; +import { UnArchiveThreadAction } from "./unArchiveThread"; import { WarnAction } from "./warn"; export const availableActions: Record> = { @@ -34,6 +35,7 @@ export const availableActions: Record> = { set_counter: SetCounterAction, set_slowmode: SetSlowmodeAction, archive_thread: ArchiveThreadAction, + unarchive_thread: UnArchiveThreadAction, }; export const AvailableActions = t.type({ @@ -53,4 +55,5 @@ export const AvailableActions = t.type({ set_counter: SetCounterAction.configType, set_slowmode: SetSlowmodeAction.configType, archive_thread: ArchiveThreadAction.configType, + unarchive_thread: UnArchiveThreadAction.configType, }); diff --git a/backend/src/plugins/Automod/actions/unArchiveThread.ts b/backend/src/plugins/Automod/actions/unArchiveThread.ts new file mode 100644 index 00000000..cdcd6128 --- /dev/null +++ b/backend/src/plugins/Automod/actions/unArchiveThread.ts @@ -0,0 +1,28 @@ +import { ThreadChannel } from "discord.js"; +import * as t from "io-ts"; +import { noop, tNullable } from "../../../utils"; +import { automodAction } from "../helpers"; + +export const UnArchiveThreadAction = automodAction({ + configType: t.type({ + unlock: tNullable(t.boolean), + }), + defaultConfig: { + unlock: false, + }, + + async apply({ pluginData, contexts, actionConfig }) { + const threads = contexts + .filter((c) => c.thread?.id) + .map((c) => pluginData.guild.channels.cache.get(c.thread!.id)) + .filter((c): c is ThreadChannel => c?.isThread() ?? false); + + for (const thread of threads) { + if (actionConfig.unlock && thread.locked) { + await thread.setLocked(false).catch(noop); + } + if (!thread.archived) continue; + await thread.setArchived(false).catch(noop); + } + }, +});