Skip to content

Commit

Permalink
Fix automod cooldown logic (#442)
Browse files Browse the repository at this point in the history
* fix automod cooldowns

Signed-off-by: GitHub <[email protected]>

* almeida fixes

Signed-off-by: GitHub <[email protected]>

* https://shr2.i0.tf/82e683c8/bd310b9c-7492-44f0-aed5-af6a22a3c196.png

Signed-off-by: GitHub <[email protected]>

---------

Signed-off-by: GitHub <[email protected]>
  • Loading branch information
metal0 authored Jan 7, 2024
1 parent 094e94f commit f58e7f5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
10 changes: 10 additions & 0 deletions backend/src/plugins/Automod/functions/applyCooldown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { GuildPluginData } from "knub";
import { convertDelayStringToMS } from "../../../utils";
import { AutomodContext, AutomodPluginType, TRule } from "../types";

export function applyCooldown(pluginData: GuildPluginData<AutomodPluginType>, 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);
}
24 changes: 0 additions & 24 deletions backend/src/plugins/Automod/functions/checkAndUpdateCooldown.ts

This file was deleted.

8 changes: 8 additions & 0 deletions backend/src/plugins/Automod/functions/checkCooldown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { GuildPluginData } from "knub";
import { AutomodContext, AutomodPluginType, TRule } from "../types";

export function checkCooldown(pluginData: GuildPluginData<AutomodPluginType>, rule: TRule, context: AutomodContext) {
const cooldownKey = `${rule.name}-${context.user?.id}`;

return pluginData.state.cooldownManager.isOnCooldown(cooldownKey);
}
7 changes: 5 additions & 2 deletions backend/src/plugins/Automod/functions/runAutomod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AutomodPluginType>, context: AutomodContext) {
const userId = context.user?.id || context.member?.id || context.message?.user_id;
Expand Down Expand Up @@ -46,7 +47,7 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
}
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;
}

Expand Down Expand Up @@ -84,6 +85,8 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
}

if (matchResult) {
if (rule.cooldown) applyCooldown(pluginData, rule, context);

contexts = [context, ...(matchResult.extraContexts || [])];

for (const _context of contexts) {
Expand Down

0 comments on commit f58e7f5

Please sign in to comment.