From f58e7f5eb71a329d2b8e9dd08a79e17cc4d0717d Mon Sep 17 00:00:00 2001 From: Tiago R Date: Sun, 7 Jan 2024 21:12:50 +0000 Subject: [PATCH] Fix automod cooldown logic (#442) * fix automod cooldowns Signed-off-by: GitHub * almeida fixes Signed-off-by: GitHub * https://shr2.i0.tf/82e683c8/bd310b9c-7492-44f0-aed5-af6a22a3c196.png Signed-off-by: GitHub --------- Signed-off-by: GitHub --- .../Automod/functions/applyCooldown.ts | 10 ++++++++ .../functions/checkAndUpdateCooldown.ts | 24 ------------------- .../Automod/functions/checkCooldown.ts | 8 +++++++ .../plugins/Automod/functions/runAutomod.ts | 7 ++++-- 4 files changed, 23 insertions(+), 26 deletions(-) create mode 100644 backend/src/plugins/Automod/functions/applyCooldown.ts delete mode 100644 backend/src/plugins/Automod/functions/checkAndUpdateCooldown.ts create mode 100644 backend/src/plugins/Automod/functions/checkCooldown.ts diff --git a/backend/src/plugins/Automod/functions/applyCooldown.ts b/backend/src/plugins/Automod/functions/applyCooldown.ts new file mode 100644 index 000000000..387ac448c --- /dev/null +++ b/backend/src/plugins/Automod/functions/applyCooldown.ts @@ -0,0 +1,10 @@ +import { GuildPluginData } from "knub"; +import { convertDelayStringToMS } from "../../../utils"; +import { AutomodContext, AutomodPluginType, TRule } from "../types"; + +export function applyCooldown(pluginData: GuildPluginData, rule: TRule, context: AutomodContext) { + const cooldownKey = `${rule.name}-${context.user?.id}`; + + const cooldownTime = convertDelayStringToMS(rule.cooldown, "s"); + if (cooldownTime) pluginData.state.cooldownManager.setCooldown(cooldownKey, cooldownTime); +} diff --git a/backend/src/plugins/Automod/functions/checkAndUpdateCooldown.ts b/backend/src/plugins/Automod/functions/checkAndUpdateCooldown.ts deleted file mode 100644 index 6338d6986..000000000 --- a/backend/src/plugins/Automod/functions/checkAndUpdateCooldown.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { GuildPluginData } from "knub"; -import { convertDelayStringToMS } from "../../../utils"; -import { AutomodContext, AutomodPluginType, TRule } from "../types"; - -export function checkAndUpdateCooldown( - pluginData: GuildPluginData, - rule: TRule, - context: AutomodContext, -) { - const cooldownKey = `${rule.name}-${context.user?.id}`; - - if (cooldownKey) { - if (pluginData.state.cooldownManager.isOnCooldown(cooldownKey)) { - return true; - } - - const cooldownTime = convertDelayStringToMS(rule.cooldown, "s"); - if (cooldownTime) { - pluginData.state.cooldownManager.setCooldown(cooldownKey, cooldownTime); - } - } - - return false; -} diff --git a/backend/src/plugins/Automod/functions/checkCooldown.ts b/backend/src/plugins/Automod/functions/checkCooldown.ts new file mode 100644 index 000000000..3640c142f --- /dev/null +++ b/backend/src/plugins/Automod/functions/checkCooldown.ts @@ -0,0 +1,8 @@ +import { GuildPluginData } from "knub"; +import { AutomodContext, AutomodPluginType, TRule } from "../types"; + +export function checkCooldown(pluginData: GuildPluginData, rule: TRule, context: AutomodContext) { + const cooldownKey = `${rule.name}-${context.user?.id}`; + + return pluginData.state.cooldownManager.isOnCooldown(cooldownKey); +} diff --git a/backend/src/plugins/Automod/functions/runAutomod.ts b/backend/src/plugins/Automod/functions/runAutomod.ts index e9c2e3a74..4ccc38dee 100644 --- a/backend/src/plugins/Automod/functions/runAutomod.ts +++ b/backend/src/plugins/Automod/functions/runAutomod.ts @@ -7,7 +7,8 @@ import { CleanAction } from "../actions/clean"; import { AutomodTriggerMatchResult } from "../helpers"; import { availableTriggers } from "../triggers/availableTriggers"; import { AutomodContext, AutomodPluginType } from "../types"; -import { checkAndUpdateCooldown } from "./checkAndUpdateCooldown"; +import { applyCooldown } from "./applyCooldown"; +import { checkCooldown } from "./checkCooldown"; export async function runAutomod(pluginData: GuildPluginData, context: AutomodContext) { const userId = context.user?.id || context.member?.id || context.message?.user_id; @@ -46,7 +47,7 @@ export async function runAutomod(pluginData: GuildPluginData, } if (!rule.affects_self && userId && userId === pluginData.client.user?.id) continue; - if (rule.cooldown && checkAndUpdateCooldown(pluginData, rule, context)) { + if (rule.cooldown && checkCooldown(pluginData, rule, context)) { continue; } @@ -84,6 +85,8 @@ export async function runAutomod(pluginData: GuildPluginData, } if (matchResult) { + if (rule.cooldown) applyCooldown(pluginData, rule, context); + contexts = [context, ...(matchResult.extraContexts || [])]; for (const _context of contexts) {