From f265e12cb45988c407fb17787295edf2ebca1ce9 Mon Sep 17 00:00:00 2001 From: "Nyeki.py" <84998222+Nekidev@users.noreply.github.com> Date: Fri, 18 Oct 2024 01:28:10 -0300 Subject: [PATCH] fix(bot): :bug: Censorship tagging normal words with different safe meaning but bad words in them. --- payphone/core/censorship.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/payphone/core/censorship.py b/payphone/core/censorship.py index 39be68d..6db4307 100644 --- a/payphone/core/censorship.py +++ b/payphone/core/censorship.py @@ -29,7 +29,7 @@ def create_pattern(text: str) -> str: for r in replacements: text = text.replace(r[0], f"({r[1]})") - return f"({text})" + return f"(?:{text})" BLOCKED_WORDS = [ @@ -228,7 +228,8 @@ BLOCKED_WORDS = [ ] ] -PATTERN = re.compile("|".join(BLOCKED_WORDS), re.IGNORECASE) +WORDS = re.compile(r"\w+") +PATTERN = re.compile(f"^({"|".join(BLOCKED_WORDS)})$", re.IGNORECASE) URL_PATTERN = re.compile(r"https?://[^\s]+", re.IGNORECASE) INVITES_PATTERN = re.compile(r"discord.gg/[^\s]+", re.IGNORECASE) @@ -243,10 +244,10 @@ def moderate(message: str) -> str: str | None: The blocked pattern found as it was found. """ - match = next(PATTERN.finditer(message), None) - - if match is not None: - return message[match.start() : match.end()] + for word in WORDS.finditer(message): + word = message[word.start() : word.end()] + if PATTERN.match(word): + return word match = next(URL_PATTERN.finditer(message), None)