From 6840fb464632aafd1359020df5f8a8b40cde0243 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sat, 27 Jan 2024 14:57:57 +0200 Subject: [PATCH] fix: clamp counter values in config --- backend/src/data/GuildCounters.ts | 9 ++++++--- backend/src/plugins/Automod/actions/setCounter.ts | 3 ++- backend/src/plugins/Counters/types.ts | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/backend/src/data/GuildCounters.ts b/backend/src/data/GuildCounters.ts index 0c8fde5b..18c9b5e6 100644 --- a/backend/src/data/GuildCounters.ts +++ b/backend/src/data/GuildCounters.ts @@ -12,7 +12,8 @@ import { CounterValue } from "./entities/CounterValue"; const DELETE_UNUSED_COUNTERS_AFTER = 1 * DAYS; const DELETE_UNUSED_COUNTER_TRIGGERS_AFTER = 1 * DAYS; -const MAX_COUNTER_VALUE = 2147483647; // 2^31-1, for MySQL INT +export const MIN_COUNTER_VALUE = 0; +export const MAX_COUNTER_VALUE = 2147483647; // 2^31-1, for MySQL INT const decayQueue = new Queue(); @@ -115,7 +116,9 @@ export class GuildCounters extends BaseGuildRepository { userId = userId || "0"; const rawUpdate = - change >= 0 ? `value = LEAST(value + ${change}, ${MAX_COUNTER_VALUE})` : `value = GREATEST(value ${change}, 0)`; + change >= 0 + ? `value = LEAST(value + ${change}, ${MAX_COUNTER_VALUE})` + : `value = GREATEST(value ${change}, ${MIN_COUNTER_VALUE})`; await this.counterValues.query( ` @@ -173,7 +176,7 @@ export class GuildCounters extends BaseGuildRepository { const rawUpdate = decayAmountToApply >= 0 - ? `GREATEST(value - ${decayAmountToApply}, 0)` + ? `GREATEST(value - ${decayAmountToApply}, ${MIN_COUNTER_VALUE})` : `LEAST(value + ${Math.abs(decayAmountToApply)}, ${MAX_COUNTER_VALUE})`; // Using an UPDATE with ORDER BY in an attempt to avoid deadlocks from simultaneous decays diff --git a/backend/src/plugins/Automod/actions/setCounter.ts b/backend/src/plugins/Automod/actions/setCounter.ts index dea4bdd6..604d9bf4 100644 --- a/backend/src/plugins/Automod/actions/setCounter.ts +++ b/backend/src/plugins/Automod/actions/setCounter.ts @@ -1,4 +1,5 @@ import z from "zod"; +import { MAX_COUNTER_VALUE, MIN_COUNTER_VALUE } from "../../../data/GuildCounters"; import { zBoundedCharacters } from "../../../utils"; import { CountersPlugin } from "../../Counters/CountersPlugin"; import { LogsPlugin } from "../../Logs/LogsPlugin"; @@ -7,7 +8,7 @@ import { automodAction } from "../helpers"; export const SetCounterAction = automodAction({ configSchema: z.strictObject({ counter: zBoundedCharacters(0, 100), - value: z.number(), + value: z.number().min(MIN_COUNTER_VALUE).max(MAX_COUNTER_VALUE), }), async apply({ pluginData, contexts, actionConfig, ruleName }) { diff --git a/backend/src/plugins/Counters/types.ts b/backend/src/plugins/Counters/types.ts index 8e70c4ab..ef322e7b 100644 --- a/backend/src/plugins/Counters/types.ts +++ b/backend/src/plugins/Counters/types.ts @@ -1,7 +1,7 @@ import { EventEmitter } from "events"; import { BasePluginType } from "knub"; import z from "zod"; -import { GuildCounters } from "../../data/GuildCounters"; +import { GuildCounters, MAX_COUNTER_VALUE, MIN_COUNTER_VALUE } from "../../data/GuildCounters"; import { CounterTrigger, buildCounterConditionString, @@ -93,7 +93,7 @@ export const zCounter = z.strictObject({ pretty_name: zBoundedCharacters(0, 100).nullable().default(null), per_channel: z.boolean().default(false), per_user: z.boolean().default(false), - initial_value: z.number().default(0), + initial_value: z.number().min(MIN_COUNTER_VALUE).max(MAX_COUNTER_VALUE).default(0), triggers: zBoundedRecord(z.record(zBoundedCharacters(0, 100), zTriggerInput), 1, MAX_TRIGGERS_PER_COUNTER), decay: z .strictObject({