From ed89f70f984cf1e314e3215bd253ea4fe7545474 Mon Sep 17 00:00:00 2001 From: jack1142 <6032823+jack1142@users.noreply.github.com> Date: Thu, 28 May 2020 23:51:53 +0200 Subject: [PATCH] Fix uses of `re.sub()` (#3826) * Fix uses of `re.sub()` (pt. 1) * Fix uses of `re.sub()` (pt. 2) * Fix uses of `re.sub()` (pt. 3) * Fix uses of `re.sub()` (pt. 4) * Revert commands.py --- redbot/core/commands/context.py | 2 +- redbot/core/core_commands.py | 4 ++-- redbot/core/utils/_internal_utils.py | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/redbot/core/commands/context.py b/redbot/core/commands/context.py index 5fdef67d0..ca40e3e7f 100644 --- a/redbot/core/commands/context.py +++ b/redbot/core/commands/context.py @@ -262,7 +262,7 @@ class Context(DPYContext): """str: The command prefix, but a mention prefix is displayed nicer.""" me = self.me pattern = re.compile(rf"<@!?{me.id}>") - return pattern.sub(f"@{me.display_name}", self.prefix) + return pattern.sub(f"@{me.display_name}".replace("\\", r"\\"), self.prefix) @property def me(self) -> Union[discord.ClientUser, discord.Member]: diff --git a/redbot/core/core_commands.py b/redbot/core/core_commands.py index 5697c2fd8..28e389934 100644 --- a/redbot/core/core_commands.py +++ b/redbot/core/core_commands.py @@ -1585,7 +1585,7 @@ class Core(commands.Cog, CoreLogic): footer += _(" | Server ID: {}").format(guild.id) prefixes = await ctx.bot.get_valid_prefixes() - prefix = re.sub(rf"<@!?{ctx.me.id}>", f"@{ctx.me.name}", prefixes[0]) + prefix = re.sub(rf"<@!?{ctx.me.id}>", f"@{ctx.me.name}".replace("\\", r"\\"), prefixes[0]) content = _("Use `{}dm {} ` to reply to this user").format(prefix, author.id) @@ -1690,7 +1690,7 @@ class Core(commands.Cog, CoreLogic): return prefixes = await ctx.bot.get_valid_prefixes() - prefix = re.sub(rf"<@!?{ctx.me.id}>", f"@{ctx.me.name}", prefixes[0]) + prefix = re.sub(rf"<@!?{ctx.me.id}>", f"@{ctx.me.name}".replace("\\", r"\\"), prefixes[0]) description = _("Owner of {}").format(ctx.bot.user) content = _("You can reply to this message with {}contact").format(prefix) if await ctx.embed_requested(): diff --git a/redbot/core/utils/_internal_utils.py b/redbot/core/utils/_internal_utils.py index 05957a0f9..9e4876efb 100644 --- a/redbot/core/utils/_internal_utils.py +++ b/redbot/core/utils/_internal_utils.py @@ -287,7 +287,9 @@ async def send_to_owners_with_prefix_replaced(bot: Red, content: str, **kwargs): async def preprocessor(bot: Red, destination: discord.abc.Messageable, content: str) -> str: prefixes = await bot.get_valid_prefixes(getattr(destination, "guild", None)) - prefix = re.sub(rf"<@!?{bot.user.id}>", f"@{bot.user.name}", prefixes[0]) + prefix = re.sub( + rf"<@!?{bot.user.id}>", f"@{bot.user.name}".replace("\\", r"\\"), prefixes[0] + ) return content.replace("[p]", prefix) await send_to_owners_with_preprocessor(bot, content, content_preprocessor=preprocessor)