diff --git a/CHANGELOG.md b/CHANGELOG.md index b2ddff7104..f1f8c1e88d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - New `View.get_item()` method. ([#1659](https://github.com/Pycord-Development/pycord/pull/1659)) - Permissions support for bridge commands. ([#1642](https://github.com/Pycord-Development/pycord/pull/1642)) - New `BridgeCommand.invoke()` method. ([#1642](https://github.com/Pycord-Development/pycord/pull/1642)) +- 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. diff --git a/discord/utils.py b/discord/utils.py index 86991bb25e..780d6cd75e 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]+)>", 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]+)>", 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]+)>", text)] def _chunk(iterator: Iterator[T], max_size: int) -> Iterator[List[T]]: ret = []