Fix automod cooldown logic (#442)
* fix automod cooldowns Signed-off-by: GitHub <noreply@github.com> * almeida fixes Signed-off-by: GitHub <noreply@github.com> * https://shr2.i0.tf/82e683c8/bd310b9c-7492-44f0-aed5-af6a22a3c196.png Signed-off-by: GitHub <noreply@github.com> --------- Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
parent
094e94f0f4
commit
f58e7f5eb7
4 changed files with 23 additions and 26 deletions
10
backend/src/plugins/Automod/functions/applyCooldown.ts
Normal file
10
backend/src/plugins/Automod/functions/applyCooldown.ts
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -1,24 +0,0 @@
|
||||||
import { GuildPluginData } from "knub";
|
|
||||||
import { convertDelayStringToMS } from "../../../utils";
|
|
||||||
import { AutomodContext, AutomodPluginType, TRule } from "../types";
|
|
||||||
|
|
||||||
export function checkAndUpdateCooldown(
|
|
||||||
pluginData: GuildPluginData<AutomodPluginType>,
|
|
||||||
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;
|
|
||||||
}
|
|
8
backend/src/plugins/Automod/functions/checkCooldown.ts
Normal file
8
backend/src/plugins/Automod/functions/checkCooldown.ts
Normal file
|
@ -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,7 +7,8 @@ import { CleanAction } from "../actions/clean";
|
||||||
import { AutomodTriggerMatchResult } from "../helpers";
|
import { AutomodTriggerMatchResult } from "../helpers";
|
||||||
import { availableTriggers } from "../triggers/availableTriggers";
|
import { availableTriggers } from "../triggers/availableTriggers";
|
||||||
import { AutomodContext, AutomodPluginType } from "../types";
|
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) {
|
export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>, context: AutomodContext) {
|
||||||
const userId = context.user?.id || context.member?.id || context.message?.user_id;
|
const userId = context.user?.id || context.member?.id || context.message?.user_id;
|
||||||
|
@ -46,7 +47,7 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
|
||||||
}
|
}
|
||||||
if (!rule.affects_self && userId && userId === pluginData.client.user?.id) continue;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,6 +85,8 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matchResult) {
|
if (matchResult) {
|
||||||
|
if (rule.cooldown) applyCooldown(pluginData, rule, context);
|
||||||
|
|
||||||
contexts = [context, ...(matchResult.extraContexts || [])];
|
contexts = [context, ...(matchResult.extraContexts || [])];
|
||||||
|
|
||||||
for (const _context of contexts) {
|
for (const _context of contexts) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue