Skip to content

Commit 8764a0e

Browse files
Add raw_mention utils (#1658)
* Add raw_mention utilities * Adjust regex for parity with the client Co-authored-by: BobDotCom <[email protected]>
1 parent 085b45a commit 8764a0e

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- New `View.get_item()` method. ([#1659](https://github.com/Pycord-Development/pycord/pull/1659))
1616
- Permissions support for bridge commands. ([#1642](https://github.com/Pycord-Development/pycord/pull/1642))
1717
- New `BridgeCommand.invoke()` method. ([#1642](https://github.com/Pycord-Development/pycord/pull/1642))
18+
- New `raw_mentions`, `raw_role_mentions` and `raw_channel_mentions` functions in `discord.utils`.
19+
([#1658](https://github.com/Pycord-Development/pycord/pull/1658))
1820

1921
### Deprecated
2022
- The `delete_message_days` parameter in ban methods is now deprecated. Please use `delete_message_seconds` instead.

discord/utils.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
"remove_markdown",
8989
"escape_markdown",
9090
"escape_mentions",
91+
"raw_mentions",
92+
"raw_channel_mentions",
93+
"raw_role_mentions",
9194
"as_chunks",
9295
"format_dt",
9396
"basic_autocomplete",
@@ -931,6 +934,50 @@ def escape_mentions(text: str) -> str:
931934
"""
932935
return re.sub(r"@(everyone|here|[!&]?[0-9]{17,20})", "@\u200b\\1", text)
933936

937+
def raw_mentions(text: str) -> List[int]:
938+
"""Returns a list of user IDs matching ``<@user_id>`` in the string.
939+
940+
Parameters
941+
-----------
942+
text: :class:`str`
943+
The string to get user mentions from.
944+
945+
Returns
946+
--------
947+
List[:class:`int`]
948+
A list of user IDs found in the string.
949+
"""
950+
return [int(x) for x in re.findall(r"<@!?([0-9]+)>", text)]
951+
952+
def raw_channel_mentions(text: str) -> List[int]:
953+
"""Returns a list of channel IDs matching ``<@#channel_id>`` in the string.
954+
955+
Parameters
956+
-----------
957+
text: :class:`str`
958+
The string to get channel mentions from.
959+
960+
Returns
961+
--------
962+
List[:class:`int`]
963+
A list of channel IDs found in the string.
964+
"""
965+
return [int(x) for x in re.findall(r"<#([0-9]+)>", text)]
966+
967+
def raw_role_mentions(text: str) -> List[int]:
968+
"""Returns a list of role IDs matching ``<@&role_id>`` in the string.
969+
970+
Parameters
971+
-----------
972+
text: :class:`str`
973+
The string to get role mentions from.
974+
975+
Returns
976+
--------
977+
List[:class:`int`]
978+
A list of role IDs found in the string.
979+
"""
980+
return [int(x) for x in re.findall(r"<@&([0-9]+)>", text)]
934981

935982
def _chunk(iterator: Iterator[T], max_size: int) -> Iterator[List[T]]:
936983
ret = []

0 commit comments

Comments
 (0)