From 00bb47790359814c02afde871a17f37c578cb9ed Mon Sep 17 00:00:00 2001 From: metal Date: Tue, 14 Sep 2021 14:10:30 +0000 Subject: [PATCH 1/4] add unarchive_thread --- .../Automod/actions/availableActions.ts | 3 +++ .../Automod/actions/unArchiveThread.ts | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 backend/src/plugins/Automod/actions/unArchiveThread.ts 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..cdd17d74 --- /dev/null +++ b/backend/src/plugins/Automod/actions/unArchiveThread.ts @@ -0,0 +1,27 @@ +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.message?.channel_id) + .map((c) => pluginData.guild.channels.cache.get(c.message!.channel_id)) + .filter((c): c is ThreadChannel => (c?.isThread() && c.archived) ?? false); + + for (const thread of threads) { + if (actionConfig.unlock) { + await thread.setLocked(false).catch(noop); + } + await thread.setArchived(false).catch(noop); + } + }, +}); From 9406136861937c92c3749dded4341819ee992759 Mon Sep 17 00:00:00 2001 From: metal Date: Tue, 14 Sep 2021 14:10:42 +0000 Subject: [PATCH 2/4] add locking to archive_thread --- .../plugins/Automod/actions/archiveThread.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/backend/src/plugins/Automod/actions/archiveThread.ts b/backend/src/plugins/Automod/actions/archiveThread.ts index 6fe871a5..b5d771e3 100644 --- a/backend/src/plugins/Automod/actions/archiveThread.ts +++ b/backend/src/plugins/Automod/actions/archiveThread.ts @@ -1,19 +1,26 @@ 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 is ThreadChannel => c?.isThread() ?? false); + .filter((c): c is ThreadChannel => (c?.isThread() && !c.archived) ?? false); for (const thread of threads) { + if (actionConfig.lock) { + await thread.setLocked().catch(noop); + } await thread.setArchived().catch(noop); } }, From b2755a754b1b36478f24a5316db901924a3ebd4f Mon Sep 17 00:00:00 2001 From: metal Date: Tue, 14 Sep 2021 15:37:45 +0000 Subject: [PATCH 3/4] check props on run loop --- backend/src/plugins/Automod/actions/archiveThread.ts | 5 +++-- backend/src/plugins/Automod/actions/unArchiveThread.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/backend/src/plugins/Automod/actions/archiveThread.ts b/backend/src/plugins/Automod/actions/archiveThread.ts index b5d771e3..1a5a32ed 100644 --- a/backend/src/plugins/Automod/actions/archiveThread.ts +++ b/backend/src/plugins/Automod/actions/archiveThread.ts @@ -15,12 +15,13 @@ export const ArchiveThreadAction = automodAction({ const threads = contexts .filter((c) => c.message?.channel_id) .map((c) => pluginData.guild.channels.cache.get(c.message!.channel_id)) - .filter((c): c is ThreadChannel => (c?.isThread() && !c.archived) ?? false); + .filter((c): c is ThreadChannel => c?.isThread() ?? false); for (const thread of threads) { - if (actionConfig.lock) { + 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/unArchiveThread.ts b/backend/src/plugins/Automod/actions/unArchiveThread.ts index cdd17d74..ceedcd6d 100644 --- a/backend/src/plugins/Automod/actions/unArchiveThread.ts +++ b/backend/src/plugins/Automod/actions/unArchiveThread.ts @@ -15,12 +15,13 @@ export const UnArchiveThreadAction = automodAction({ const threads = contexts .filter((c) => c.message?.channel_id) .map((c) => pluginData.guild.channels.cache.get(c.message!.channel_id)) - .filter((c): c is ThreadChannel => (c?.isThread() && c.archived) ?? false); + .filter((c): c is ThreadChannel => c?.isThread() ?? false); for (const thread of threads) { - if (actionConfig.unlock) { + if (actionConfig.unlock && thread.locked) { await thread.setLocked(false).catch(noop); } + if (!thread.archived) continue; await thread.setArchived(false).catch(noop); } }, From 574f56b7bb65c46cd1626fcbfc1ba5f7a90dedcf Mon Sep 17 00:00:00 2001 From: metal Date: Tue, 14 Sep 2021 15:38:41 +0000 Subject: [PATCH 4/4] use c.thread --- backend/src/plugins/Automod/actions/archiveThread.ts | 4 ++-- backend/src/plugins/Automod/actions/unArchiveThread.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/src/plugins/Automod/actions/archiveThread.ts b/backend/src/plugins/Automod/actions/archiveThread.ts index 1a5a32ed..12f47131 100644 --- a/backend/src/plugins/Automod/actions/archiveThread.ts +++ b/backend/src/plugins/Automod/actions/archiveThread.ts @@ -13,8 +13,8 @@ export const ArchiveThreadAction = automodAction({ 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) { diff --git a/backend/src/plugins/Automod/actions/unArchiveThread.ts b/backend/src/plugins/Automod/actions/unArchiveThread.ts index ceedcd6d..cdcd6128 100644 --- a/backend/src/plugins/Automod/actions/unArchiveThread.ts +++ b/backend/src/plugins/Automod/actions/unArchiveThread.ts @@ -13,8 +13,8 @@ export const UnArchiveThreadAction = automodAction({ 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) {