diff --git a/backend/src/plugins/Automod/triggers/matchWords.ts b/backend/src/plugins/Automod/triggers/matchWords.ts index 7967c9de..764eb87c 100644 --- a/backend/src/plugins/Automod/triggers/matchWords.ts +++ b/backend/src/plugins/Automod/triggers/matchWords.ts @@ -31,7 +31,6 @@ const configSchema = z.strictObject({ export const MatchWordsTrigger = automodTrigger()({ configSchema, - async match({ pluginData, context, triggerConfig: trigger }) { if (!context.message) { return; @@ -39,21 +38,37 @@ export const MatchWordsTrigger = automodTrigger()({ if (!regexCache.has(trigger)) { const looseMatchingThreshold = Math.min(Math.max(trigger.loose_matching_threshold, 1), 64); + const patterns = trigger.words.map((word) => { - let pattern = trigger.loose_matching - ? [...word].map((c) => escapeStringRegexp(c)).join(`(?:\\s*|.{0,${looseMatchingThreshold}})`) - : escapeStringRegexp(word); + let pattern; + + if (trigger.loose_matching) { + pattern = [...word] + .map((c) => escapeStringRegexp(c)) + .join(`[\\s\\-_.,!?]{0,${looseMatchingThreshold}}`); + } else { + pattern = escapeStringRegexp(word); + } if (trigger.only_full_words) { - pattern = `\\b${pattern}\\b`; + if (trigger.loose_matching) { + pattern = `\\b(?:${pattern})\\b`; + } else { + pattern = `\\b${pattern}\\b`; + } } return pattern; }); - const mergedRegex = new RegExp(patterns.map((p) => `(?:${p})`).join("|"), trigger.case_sensitive ? "" : "i"); + const mergedRegex = new RegExp( + patterns.map((p) => `(${p})`).join("|"), + trigger.case_sensitive ? "" : "i" + ); + regexCache.set(trigger, [mergedRegex]); } + const regexes = regexCache.get(trigger)!; for await (let [type, str] of matchMultipleTextTypesOnMessage(pluginData, trigger, context.message)) { @@ -66,11 +81,14 @@ export const MatchWordsTrigger = automodTrigger()({ } for (const regex of regexes) { - if (regex.test(str)) { + const match = regex.exec(str); + if (match) { + const matchedWord = match.slice(1).find(group => group !== undefined) || ""; + return { extra: { type, - word: "", + word: matchedWord, }, }; } @@ -82,6 +100,7 @@ export const MatchWordsTrigger = automodTrigger()({ renderMatchInformation({ pluginData, contexts, matchResult }) { const partialSummary = getTextMatchPartialSummary(pluginData, matchResult.extra.type, contexts[0]); - return `Matched word in ${partialSummary}`; + const wordInfo = matchResult.extra.word ? ` (matched: "${matchResult.extra.word}")` : ""; + return `Matched word in ${partialSummary}${wordInfo}`; }, -}); +}); \ No newline at end of file