Skip to content

Commit

Permalink
new mention behavior, new filter behavior (#3553)
Browse files Browse the repository at this point in the history
* new mention behavior, new filter behavior

* and here too, ffs

* docs and reformat

* review handling
  • Loading branch information
Michael H committed Feb 14, 2020
1 parent a44047b commit 066bf51
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions redbot/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ async def is_automod_immune(
async def send_filtered(
destination: discord.abc.Messageable,
filter_mass_mentions=True,
filter_roles=True,
filter_invite_links=True,
filter_all_links=False,
**kwargs,
Expand All @@ -969,6 +970,8 @@ async def send_filtered(
content = kwargs.pop("content", None)

if content:
if filter_roles and isinstance(destination, discord.TextChannel):
content = common_filters.sanitize_role_mentions(content, destination.guild.roles)
if filter_mass_mentions:
content = common_filters.filter_mass_mentions(content)
if filter_invite_links:
Expand Down
9 changes: 8 additions & 1 deletion redbot/core/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ async def send(self, content=None, **kwargs):
:func:`~redbot.core.utils.common_filters.filter_mass_mentions`.
This must take a single `str` as an argument, and return
the sanitized `str`.
\*\*kwargs
sanitize_roles : bool
Whether or not role mentions should be sanitized for you.
Defaults to ``True``
**kwargs
See `discord.ext.commands.Context.send`.
Returns
Expand All @@ -86,6 +89,10 @@ async def send(self, content=None, **kwargs):
"""

_filter = kwargs.pop("filter", common_filters.filter_mass_mentions)
sanitize_roles = kwargs.pop("sanitize_roles", True)

if sanitize_roles and content and self.guild:
content = common_filters.sanitize_role_mentions(str(content), self.guild.roles)

if _filter and content:
content = _filter(str(content))
Expand Down
33 changes: 33 additions & 0 deletions redbot/core/utils/common_filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import re
from typing import Iterable

import discord

__all__ = [
"URL_RE",
Expand All @@ -11,6 +14,7 @@
"normalize_smartquotes",
"escape_spoilers",
"escape_spoilers_and_mass_mentions",
"sanitize_role_mentions",
]

# regexes
Expand Down Expand Up @@ -173,3 +177,32 @@ def escape_spoilers_and_mass_mentions(content: str) -> str:
The escaped string.
"""
return escape_spoilers(filter_mass_mentions(content))


def sanitize_role_mentions(content: str, roles: Iterable[discord.Role]) -> str:
"""
Swaps out role mentions for @RoleName
This should always be used prior to filtering everyone mentions
Parameters
----------
content: str
The string to make substitutions to
roles: Iterable[discord.Role]
The roles to make substitutions for
Returns
-------
str
The resulting string
"""
transformations = {re.escape(fr"<@&{role.id}>"): f"@{role.name}" for role in roles}

def repl(obj):
return transformations.get(re.escape(obj.group(0)), "")

pattern = re.compile("|".join(transformations.keys()))
result = pattern.sub(repl, content)

return result

0 comments on commit 066bf51

Please sign in to comment.