From b951feeabd82bd530b1a6897d45f4c5eeaa49dfd Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Tue, 5 Oct 2021 21:48:02 +0300 Subject: [PATCH] fix(automod): fix empty patterns list in match_regex matching every message --- .../src/plugins/Automod/triggers/matchRegex.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/backend/src/plugins/Automod/triggers/matchRegex.ts b/backend/src/plugins/Automod/triggers/matchRegex.ts index 42951920..53ae561b 100644 --- a/backend/src/plugins/Automod/triggers/matchRegex.ts +++ b/backend/src/plugins/Automod/triggers/matchRegex.ts @@ -46,6 +46,17 @@ export const MatchRegexTrigger = automodTrigger()({ return; } + if (trigger.patterns.length === 0) { + return; + } + + if (!combinedRegexCache.has(trigger)) { + const combinedPattern = trigger.patterns.map((p) => `(?:${p.source})`).join("|"); + const combinedRegex = new RegExp(combinedPattern, trigger.case_sensitive ? "" : "i"); + combinedRegexCache.set(trigger, combinedRegex); + } + const regex = combinedRegexCache.get(trigger)!; + for await (let [type, str] of matchMultipleTextTypesOnMessage(pluginData, trigger, context.message)) { if (trigger.strip_markdown) { str = stripMarkdown(str); @@ -55,13 +66,6 @@ export const MatchRegexTrigger = automodTrigger()({ str = normalizeText(str); } - if (!combinedRegexCache.has(trigger)) { - const combinedPattern = trigger.patterns.map((p) => `(?:${p.source})`).join("|"); - const combinedRegex = new RegExp(combinedPattern, trigger.case_sensitive ? "" : "i"); - combinedRegexCache.set(trigger, combinedRegex); - } - - const regex = combinedRegexCache.get(trigger)!; const matches = await pluginData.state.regexRunner.exec(regex, str).catch(allowTimeout); if (matches?.length) { return {