Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add raw_mention utils #1658

Merged
merged 7 commits into from
Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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