From d060a62614dd788eef87ab89ccc08da9f87420b9 Mon Sep 17 00:00:00 2001 From: NeloBlivion <41271523+NeloBlivion@users.noreply.github.com> Date: Sat, 1 Oct 2022 03:52:40 +0100 Subject: [PATCH 1/3] Add raw_mention utilities --- discord/utils.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/discord/utils.py b/discord/utils.py index 86991bb25e..f9eea552e7 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -88,6 +88,9 @@ "remove_markdown", "escape_markdown", "escape_mentions", + "raw_mentions", + "raw_channel_mentions", + "raw_role_mentions", "as_chunks", "format_dt", "basic_autocomplete", @@ -931,6 +934,50 @@ def escape_mentions(text: str) -> str: """ return re.sub(r"@(everyone|here|[!&]?[0-9]{17,20})", "@\u200b\\1", text) +def raw_mentions(text: str) -> List[int]: + """Returns a list of user IDs matching ``<@user_id>`` in the string. + + Parameters + ----------- + text: :class:`str` + The string to get user mentions from. + + Returns + -------- + List[:class:`int`] + A list of user IDs found in the string. + """ + return [int(x) for x in re.findall(r"<@!?([0-9]{15,20})>", text)] + +def raw_channel_mentions(text: str) -> List[int]: + """Returns a list of channel IDs matching ``<@#channel_id>`` in the string. + + Parameters + ----------- + text: :class:`str` + The string to get channel mentions from. + + Returns + -------- + List[:class:`int`] + A list of channel IDs found in the string. + """ + return [int(x) for x in re.findall(r"<#([0-9]{15,20})>", text)] + +def raw_role_mentions(text: str) -> List[int]: + """Returns a list of role IDs matching ``<@&role_id>`` in the string. + + Parameters + ----------- + text: :class:`str` + The string to get role mentions from. + + Returns + -------- + List[:class:`int`] + A list of role IDs found in the string. + """ + return [int(x) for x in re.findall(r"<@&([0-9]{15,20})>", text)] def _chunk(iterator: Iterator[T], max_size: int) -> Iterator[List[T]]: ret = [] From a68b1d1f336746eca99b5ef4cbd0404e0c7eb7c9 Mon Sep 17 00:00:00 2001 From: NeloBlivion <41271523+NeloBlivion@users.noreply.github.com> Date: Sat, 1 Oct 2022 15:11:39 +0100 Subject: [PATCH 2/3] Adjust regex for parity with the client --- discord/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/discord/utils.py b/discord/utils.py index f9eea552e7..780d6cd75e 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -947,7 +947,7 @@ def raw_mentions(text: str) -> List[int]: List[:class:`int`] A list of user IDs found in the string. """ - return [int(x) for x in re.findall(r"<@!?([0-9]{15,20})>", text)] + return [int(x) for x in re.findall(r"<@!?([0-9]+)>", text)] def raw_channel_mentions(text: str) -> List[int]: """Returns a list of channel IDs matching ``<@#channel_id>`` in the string. @@ -962,7 +962,7 @@ def raw_channel_mentions(text: str) -> List[int]: List[:class:`int`] A list of channel IDs found in the string. """ - return [int(x) for x in re.findall(r"<#([0-9]{15,20})>", text)] + return [int(x) for x in re.findall(r"<#([0-9]+)>", text)] def raw_role_mentions(text: str) -> List[int]: """Returns a list of role IDs matching ``<@&role_id>`` in the string. @@ -977,7 +977,7 @@ def raw_role_mentions(text: str) -> List[int]: List[:class:`int`] A list of role IDs found in the string. """ - return [int(x) for x in re.findall(r"<@&([0-9]{15,20})>", text)] + return [int(x) for x in re.findall(r"<@&([0-9]+)>", text)] def _chunk(iterator: Iterator[T], max_size: int) -> Iterator[List[T]]: ret = [] From 8e4d4fc2a6652ec126addc38edf0a3ce844ac0b8 Mon Sep 17 00:00:00 2001 From: BobDotCom <71356958+BobDotCom@users.noreply.github.com> Date: Sun, 2 Oct 2022 15:37:11 -0500 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99083fc20a..cc722037c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `proxy` and `proxy_auth` params to many Webhook related methods. ([#1655](https://github.com/Pycord-Development/pycord/pull/1655)) - `delete_message_seconds` parameter in ban methods. ([#1557](https://github.com/Pycord-Development/pycord/pull/1557)) +- New `raw_mentions`, `raw_role_mentions` and `raw_channel_mentions` functions in `discord.utils`. + ([#1658](https://github.com/Pycord-Development/pycord/pull/1658)) ### Deprecated - The `delete_message_days` parameter in ban methods is now deprecated. Please use `delete_message_seconds` instead.