From 67091151664c14b02bda1332f39bf782c91c4fd7 Mon Sep 17 00:00:00 2001 From: Almeida Date: Sun, 31 Oct 2021 17:23:26 +0000 Subject: [PATCH] feat: add thread_create_spam automod trigger (#273) --- backend/src/plugins/Automod/constants.ts | 1 + .../events/runAutomodOnThreadEvents.ts | 8 +++ .../Automod/triggers/availableTriggers.ts | 3 ++ .../Automod/triggers/threadCreateSpam.ts | 51 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 backend/src/plugins/Automod/triggers/threadCreateSpam.ts diff --git a/backend/src/plugins/Automod/constants.ts b/backend/src/plugins/Automod/constants.ts index 1f46f2e3..53ff75ad 100644 --- a/backend/src/plugins/Automod/constants.ts +++ b/backend/src/plugins/Automod/constants.ts @@ -16,4 +16,5 @@ export enum RecentActionType { MemberJoin, Sticker, MemberLeave, + ThreadCreate, } diff --git a/backend/src/plugins/Automod/events/runAutomodOnThreadEvents.ts b/backend/src/plugins/Automod/events/runAutomodOnThreadEvents.ts index f1f47a48..3f5a71c1 100644 --- a/backend/src/plugins/Automod/events/runAutomodOnThreadEvents.ts +++ b/backend/src/plugins/Automod/events/runAutomodOnThreadEvents.ts @@ -1,4 +1,5 @@ import { typedGuildEventListener } from "knub"; +import { RecentActionType } from "../constants"; import { runAutomod } from "../functions/runAutomod"; import { AutomodContext, AutomodPluginType } from "../types"; @@ -19,6 +20,13 @@ export const RunAutomodOnThreadCreate = typedGuildEventListener { + pluginData.state.recentActions.push({ + type: RecentActionType.ThreadCreate, + context, + count: 1, + identifier: null, + }); + runAutomod(pluginData, context); }); }, diff --git a/backend/src/plugins/Automod/triggers/availableTriggers.ts b/backend/src/plugins/Automod/triggers/availableTriggers.ts index 0bd3b949..ece5949f 100644 --- a/backend/src/plugins/Automod/triggers/availableTriggers.ts +++ b/backend/src/plugins/Automod/triggers/availableTriggers.ts @@ -27,6 +27,7 @@ import { RoleAddedTrigger } from "./roleAdded"; import { RoleRemovedTrigger } from "./roleRemoved"; import { StickerSpamTrigger } from "./stickerSpam"; import { ThreadCreateTrigger } from "./threadCreate"; +import { ThreadCreateSpamTrigger } from "./threadCreateSpam"; import { ThreadDeleteTrigger } from "./threadDelete"; import { UnbanTrigger } from "./unban"; import { UnmuteTrigger } from "./unmute"; @@ -54,6 +55,7 @@ export const availableTriggers: Record character_spam: CharacterSpamTrigger, member_join_spam: MemberJoinSpamTrigger, sticker_spam: StickerSpamTrigger, + thread_create_spam: ThreadCreateSpamTrigger, counter_trigger: CounterTrigger, @@ -94,6 +96,7 @@ export const AvailableTriggers = t.type({ character_spam: CharacterSpamTrigger.configType, member_join_spam: MemberJoinSpamTrigger.configType, sticker_spam: StickerSpamTrigger.configType, + thread_create_spam: ThreadCreateSpamTrigger.configType, counter_trigger: CounterTrigger.configType, diff --git a/backend/src/plugins/Automod/triggers/threadCreateSpam.ts b/backend/src/plugins/Automod/triggers/threadCreateSpam.ts new file mode 100644 index 00000000..79b568cb --- /dev/null +++ b/backend/src/plugins/Automod/triggers/threadCreateSpam.ts @@ -0,0 +1,51 @@ +import * as t from "io-ts"; +import { convertDelayStringToMS, tDelayString } from "../../../utils"; +import { RecentActionType } from "../constants"; +import { findRecentSpam } from "../functions/findRecentSpam"; +import { getMatchingRecentActions } from "../functions/getMatchingRecentActions"; +import { sumRecentActionCounts } from "../functions/sumRecentActionCounts"; +import { automodTrigger } from "../helpers"; + +export const ThreadCreateSpamTrigger = automodTrigger()({ + configType: t.type({ + amount: t.number, + within: tDelayString, + }), + + defaultConfig: {}, + + async match({ pluginData, context, triggerConfig }) { + if (!context.threadChange?.created) { + return; + } + + const recentSpam = findRecentSpam(pluginData, RecentActionType.ThreadCreate); + if (recentSpam) { + context.actioned = true; + return {}; + } + + const since = Date.now() - convertDelayStringToMS(triggerConfig.within)!; + const matchingActions = getMatchingRecentActions(pluginData, RecentActionType.ThreadCreate, null, since); + const totalCount = sumRecentActionCounts(matchingActions); + + if (totalCount >= triggerConfig.amount) { + const extraContexts = matchingActions.map(a => a.context).filter(c => c !== context); + + pluginData.state.recentSpam.push({ + type: RecentActionType.ThreadCreate, + timestamp: Date.now(), + archiveId: null, + identifiers: [], + }); + + return { + extraContexts, + }; + } + }, + + renderMatchInformation({ pluginData, contexts, triggerConfig }) { + return ""; + }, +});