From cad2251cbbd9a2a8e8b682795fca7d20c6cbc8d3 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Sun, 10 Aug 2025 22:23:39 +0100 Subject: Update Regex to include Discord protocol format (#304) Chris is traveling abroad and is unable to access. Therefore, Fisher have become his proxy. An 'apprentice' if you will. Shame because Fisher have years more software engineering experience compared to him. Unfortunately, neither Fisher nor Chris cared to separate the version bump into a separate commit so SENIOR Engineer Joe Banks to JUNIOR Engineer Chris Lovering with INTERN Fisher has had to Get Involved. Co-authored-by: ChrisLovering Co-authored-by: fisher60 --- pydis_core/utils/regex.py | 6 ++++-- tests/pydis_core/utils/test_regex.py | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pydis_core/utils/regex.py b/pydis_core/utils/regex.py index ba6b9b01..26b1a547 100644 --- a/pydis_core/utils/regex.py +++ b/pydis_core/utils/regex.py @@ -3,14 +3,16 @@ import re DISCORD_INVITE = re.compile( - r"(https?:\/\/)?(www\.)?" # Optional http(s) and www. + r"(https?:\/\/)?(discord:\/*)?" # Optional protocols + r"(www\.)?" # Optional www + r"[@#]*" # Optional @ or # symbols r"(\B|discord(app)?)" # Optional discord(app) r"([.,]|dot)" # Various characters to cover dots r"(" r"(gg|me)" # TLDs that embed within discord r"|com(\/|slash|\\)invite" # Only match com/invite r")" - r"(/|slash|\\+)" # / or 'slash' or 1+ of \ + r"(/|slash|\\+)" # / or 'slash' or 1+ of \ r"(?P\S+)", # the invite code itself flags=re.IGNORECASE ) diff --git a/tests/pydis_core/utils/test_regex.py b/tests/pydis_core/utils/test_regex.py index 79c1d743..26fe23ea 100644 --- a/tests/pydis_core/utils/test_regex.py +++ b/tests/pydis_core/utils/test_regex.py @@ -49,6 +49,11 @@ class UtilsRegexTests(unittest.TestCase): self.assertEqual(search_regex("https://discord.gg/python with whitespace"), "python") self.assertEqual(search_regex(" https://discord.gg/python "), "python") + self.assertEqual(search_regex("discord:#@discordapp.com/invite/python"), "python") + self.assertEqual(search_regex("discord:/#@discordapp.com/invite/python"), "python") + self.assertEqual(search_regex("discord://#@discordapp.com/invite/python"), "python") + self.assertEqual(search_regex("discord://@#discordapp.com/invite/python"), "python") + def test_discord_invite_negatives(self): """Test the DISCORD_INVITE regex on a set of strings we would expect to not capture.""" -- cgit v1.2.3