automod: add cooldown support
This commit is contained in:
parent
5882bbda4e
commit
7fb7787583
4 changed files with 36 additions and 2 deletions
|
@ -5,7 +5,7 @@ import { GuildLogs } from "../../data/GuildLogs";
|
|||
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
||||
import { runAutomodOnMessage } from "./events/runAutomodOnMessage";
|
||||
import { Queue } from "../../Queue";
|
||||
import { configUtils } from "knub";
|
||||
import { configUtils, CooldownManager } from "knub";
|
||||
import { availableTriggers } from "./triggers/availableTriggers";
|
||||
import { StrictValidationError } from "../../validatorUtils";
|
||||
import { ConfigPreprocessorFn } from "knub/dist/config/configTypes";
|
||||
|
@ -161,6 +161,8 @@ export const AutomodPlugin = zeppelinPlugin<AutomodPluginType>()("automod", {
|
|||
30 * SECONDS,
|
||||
);
|
||||
|
||||
pluginData.state.cooldownManager = new CooldownManager();
|
||||
|
||||
pluginData.state.logs = new GuildLogs(pluginData.guild.id);
|
||||
pluginData.state.savedMessages = GuildSavedMessages.getGuildInstance(pluginData.guild.id);
|
||||
pluginData.state.antiraidLevels = GuildAntiraidLevels.getGuildInstance(pluginData.guild.id);
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
import { AutomodContext, AutomodPluginType, TRule } from "../types";
|
||||
import { PluginData } from "knub";
|
||||
import { AutomodTriggerMatchResult } from "../helpers";
|
||||
import { convertDelayStringToMS } from "../../../utils";
|
||||
|
||||
export function checkAndUpdateCooldown(
|
||||
pluginData: PluginData<AutomodPluginType>,
|
||||
rule: TRule,
|
||||
context: AutomodContext,
|
||||
) {
|
||||
const cooldownKey = 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;
|
||||
}
|
|
@ -4,6 +4,7 @@ import { availableTriggers } from "../triggers/availableTriggers";
|
|||
import { availableActions } from "../actions/availableActions";
|
||||
import { AutomodTriggerMatchResult } from "../helpers";
|
||||
import { CleanAction } from "../actions/clean";
|
||||
import { checkAndUpdateCooldown } from "./checkAndUpdateCooldown";
|
||||
|
||||
export async function runAutomod(pluginData: PluginData<AutomodPluginType>, context: AutomodContext) {
|
||||
const userId = context.user?.id || context.message?.user_id;
|
||||
|
@ -24,6 +25,10 @@ export async function runAutomod(pluginData: PluginData<AutomodPluginType>, cont
|
|||
if (rule.enabled === false) continue;
|
||||
if (!rule.affects_bots && user.bot) continue;
|
||||
|
||||
if (rule.cooldown && checkAndUpdateCooldown(pluginData, rule, context)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let matchResult: AutomodTriggerMatchResult<any>;
|
||||
let contexts: AutomodContext[];
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import * as t from "io-ts";
|
||||
import { tNullable, UnknownUser } from "../../utils";
|
||||
import { BasePluginType } from "knub";
|
||||
import { BasePluginType, CooldownManager } from "knub";
|
||||
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
||||
import { GuildLogs } from "../../data/GuildLogs";
|
||||
import { SavedMessage } from "../../data/entities/SavedMessage";
|
||||
|
@ -65,6 +65,8 @@ export interface AutomodPluginType extends BasePluginType {
|
|||
|
||||
cachedAntiraidLevel: string | null;
|
||||
|
||||
cooldownManager: CooldownManager;
|
||||
|
||||
savedMessages: GuildSavedMessages;
|
||||
logs: GuildLogs;
|
||||
antiraidLevels: GuildAntiraidLevels;
|
||||
|
|
Loading…
Add table
Reference in a new issue