From e52db4dcd2f871a7fedb8bee445763716c7a74e4 Mon Sep 17 00:00:00 2001 From: metal Date: Tue, 14 Sep 2021 14:51:22 +0000 Subject: [PATCH] compact locked triggers --- .../Automod/triggers/availableTriggers.ts | 6 -- .../plugins/Automod/triggers/threadArchive.ts | 7 +++ .../plugins/Automod/triggers/threadLock.ts | 57 ------------------- .../Automod/triggers/threadUnarchive.ts | 7 +++ .../plugins/Automod/triggers/threadUnlock.ts | 57 ------------------- 5 files changed, 14 insertions(+), 120 deletions(-) delete mode 100644 backend/src/plugins/Automod/triggers/threadLock.ts delete mode 100644 backend/src/plugins/Automod/triggers/threadUnlock.ts diff --git a/backend/src/plugins/Automod/triggers/availableTriggers.ts b/backend/src/plugins/Automod/triggers/availableTriggers.ts index ef2b31d7..ea8b6f04 100644 --- a/backend/src/plugins/Automod/triggers/availableTriggers.ts +++ b/backend/src/plugins/Automod/triggers/availableTriggers.ts @@ -34,8 +34,6 @@ import { UnmuteTrigger } from "./unmute"; import { WarnTrigger } from "./warn"; import { ThreadArchiveTrigger } from "./threadArchive"; import { ThreadUnarchiveTrigger } from "./threadUnarchive"; -import { ThreadLockTrigger } from "./threadLock"; -import { ThreadUnlockTrigger } from "./threadUnlock"; export const availableTriggers: Record> = { any_message: AnyMessageTrigger, @@ -77,8 +75,6 @@ export const availableTriggers: Record thread_delete: ThreadDeleteTrigger, thread_archive: ThreadArchiveTrigger, thread_unarchive: ThreadUnarchiveTrigger, - thread_lock: ThreadLockTrigger, - thread_unlock: ThreadUnlockTrigger, }; export const AvailableTriggers = t.type({ @@ -122,6 +118,4 @@ export const AvailableTriggers = t.type({ thread_delete: ThreadDeleteTrigger.configType, thread_archive: ThreadArchiveTrigger.configType, thread_unarchive: ThreadUnarchiveTrigger.configType, - thread_lock: ThreadLockTrigger.configType, - thread_unlock: ThreadUnlockTrigger.configType, }); diff --git a/backend/src/plugins/Automod/triggers/threadArchive.ts b/backend/src/plugins/Automod/triggers/threadArchive.ts index 2f088ad8..e0831fae 100644 --- a/backend/src/plugins/Automod/triggers/threadArchive.ts +++ b/backend/src/plugins/Automod/triggers/threadArchive.ts @@ -15,6 +15,7 @@ interface ThreadArchiveResult { export const ThreadArchiveTrigger = automodTrigger()({ configType: t.type({ parent: tNullable(t.union([t.string, t.array(t.string)])), + locked: tNullable(t.boolean), }), defaultConfig: {}, @@ -24,6 +25,12 @@ export const ThreadArchiveTrigger = automodTrigger()({ return; } + if (triggerConfig.locked && !context.threadChange.locked) { + return; + } else if (triggerConfig.locked === false && !context.threadChange.unlocked) { + return; + } + const thread = context.threadChange.archived; if (triggerConfig.parent) { diff --git a/backend/src/plugins/Automod/triggers/threadLock.ts b/backend/src/plugins/Automod/triggers/threadLock.ts deleted file mode 100644 index e2497584..00000000 --- a/backend/src/plugins/Automod/triggers/threadLock.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { Snowflake } from "discord-api-types"; -import { User, Util } from "discord.js"; -import * as t from "io-ts"; -import { tNullable } from "../../../utils"; -import { automodTrigger } from "../helpers"; - -interface ThreadLockResult { - matchedThreadId: Snowflake; - matchedThreadName: string; - matchedThreadParentId: Snowflake; - matchedThreadParentName: string; - matchedThreadOwner: User | undefined; -} - -export const ThreadLockTrigger = automodTrigger()({ - configType: t.type({ - parent: tNullable(t.union([t.string, t.array(t.string)])), - }), - - defaultConfig: {}, - - async match({ context, triggerConfig }) { - if (!context.threadChange?.locked) { - return; - } - - const thread = context.threadChange.locked; - - if (triggerConfig.parent) { - const parentIds = Array.isArray(triggerConfig.parent) ? triggerConfig.parent : [triggerConfig.parent]; - if (thread.parentId && !parentIds.includes(thread.parentId)) return; - } - - return { - extra: { - matchedThreadId: thread.id, - matchedThreadName: thread.name, - matchedThreadParentId: thread.parentId ?? "Unknown", - matchedThreadParentName: thread.parent?.name ?? "Unknown", - matchedThreadOwner: context.user, - }, - }; - }, - - async renderMatchInformation({ matchResult }) { - const threadId = matchResult.extra.matchedThreadId; - const threadName = matchResult.extra.matchedThreadName; - const threadOwner = matchResult.extra.matchedThreadOwner; - const parentId = matchResult.extra.matchedThreadParentId; - const parentName = matchResult.extra.matchedThreadParentName; - const base = `Thread **#${threadName}** (\`${threadId}\`) has been locked in the **#${parentName}** (\`${parentId}\`) channel`; - if (threadOwner) { - return `${base} by **${Util.escapeBold(threadOwner.tag)}** (\`${threadOwner.id}\`)`; - } - return base; - }, -}); diff --git a/backend/src/plugins/Automod/triggers/threadUnarchive.ts b/backend/src/plugins/Automod/triggers/threadUnarchive.ts index eb5577c9..8af0d0d7 100644 --- a/backend/src/plugins/Automod/triggers/threadUnarchive.ts +++ b/backend/src/plugins/Automod/triggers/threadUnarchive.ts @@ -15,6 +15,7 @@ interface ThreadUnarchiveResult { export const ThreadUnarchiveTrigger = automodTrigger()({ configType: t.type({ parent: tNullable(t.union([t.string, t.array(t.string)])), + locked: tNullable(t.boolean), }), defaultConfig: {}, @@ -24,6 +25,12 @@ export const ThreadUnarchiveTrigger = automodTrigger()({ return; } + if (triggerConfig.locked && !context.threadChange.locked) { + return; + } else if (triggerConfig.locked === false && !context.threadChange.unlocked) { + return; + } + const thread = context.threadChange.unarchived; if (triggerConfig.parent) { diff --git a/backend/src/plugins/Automod/triggers/threadUnlock.ts b/backend/src/plugins/Automod/triggers/threadUnlock.ts deleted file mode 100644 index 96311246..00000000 --- a/backend/src/plugins/Automod/triggers/threadUnlock.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { Snowflake } from "discord-api-types"; -import { User, Util } from "discord.js"; -import * as t from "io-ts"; -import { tNullable } from "../../../utils"; -import { automodTrigger } from "../helpers"; - -interface ThreadUnlockResult { - matchedThreadId: Snowflake; - matchedThreadName: string; - matchedThreadParentId: Snowflake; - matchedThreadParentName: string; - matchedThreadOwner: User | undefined; -} - -export const ThreadUnlockTrigger = automodTrigger()({ - configType: t.type({ - parent: tNullable(t.union([t.string, t.array(t.string)])), - }), - - defaultConfig: {}, - - async match({ context, triggerConfig }) { - if (!context.threadChange?.unlocked) { - return; - } - - const thread = context.threadChange.unlocked; - - if (triggerConfig.parent) { - const parentIds = Array.isArray(triggerConfig.parent) ? triggerConfig.parent : [triggerConfig.parent]; - if (thread.parentId && !parentIds.includes(thread.parentId)) return; - } - - return { - extra: { - matchedThreadId: thread.id, - matchedThreadName: thread.name, - matchedThreadParentId: thread.parentId ?? "Unknown", - matchedThreadParentName: thread.parent?.name ?? "Unknown", - matchedThreadOwner: context.user, - }, - }; - }, - - async renderMatchInformation({ matchResult }) { - const threadId = matchResult.extra.matchedThreadId; - const threadName = matchResult.extra.matchedThreadName; - const threadOwner = matchResult.extra.matchedThreadOwner; - const parentId = matchResult.extra.matchedThreadParentId; - const parentName = matchResult.extra.matchedThreadParentName; - const base = `Thread **#${threadName}** (\`${threadId}\`) has been locked in the **#${parentName}** (\`${parentId}\`) channel`; - if (threadOwner) { - return `${base} by **${Util.escapeBold(threadOwner.tag)}** (\`${threadOwner.id}\`)`; - } - return base; - }, -});