Skip to content

Commit

Permalink
Add raw_mention utils (#1658)
Browse files Browse the repository at this point in the history
* Add raw_mention utilities

* Adjust regex for parity with the client

Co-authored-by: BobDotCom <[email protected]>
  • Loading branch information
NeloBlivion and BobDotCom authored Oct 2, 2022
1 parent 085b45a commit 8764a0e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
47 changes: 47 additions & 0 deletions discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@
"remove_markdown",
"escape_markdown",
"escape_mentions",
"raw_mentions",
"raw_channel_mentions",
"raw_role_mentions",
"as_chunks",
"format_dt",
"basic_autocomplete",
Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit 8764a0e

Please sign in to comment.