Repository: Cog-Creators/Red-DiscordBot

Issue:
Help command issue with / at at start of bot nickname
# Other bugs

<!-- 
Did you find a bug with something other than a command? Fill out the following:
-->

#### What were you trying to do?

Run the help command.

#### What were you expecting to happen?

DM with list of commands

#### What actually happened?

Error in command 'help'. Check your console or logs for details.
Log attached

#### How can we reproduce this issue?

Nickname bot starting with \
Potentially also use \ as bot prefix

#### Potential issue:
https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/core/commands/context.py#L261
No input validation

[message.txt](https://github.com/Cog-Creators/Red-DiscordBot/files/4588155/message.txt)



Discussion:
> Error in command 'help'. Check your console or logs for details.

IT would be nice if in the future you just post the traceback instead of a file thats forced to download.

can you show the output of `[p]cogs` also go to the support server so that we can do some debugging https://discord.gg/red

Resolving patch:
diff --git a/redbot/core/commands/context.py b/redbot/core/commands/context.py
--- a/redbot/core/commands/context.py
+++ b/redbot/core/commands/context.py
@@ -262,7 +262,7 @@ def clean_prefix(self) -> str:
         """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
--- a/redbot/core/core_commands.py
+++ b/redbot/core/core_commands.py
@@ -1585,7 +1585,7 @@ async def contact(self, ctx: commands.Context, *, message: str):
             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 {} <text>` to reply to this user").format(prefix, author.id)
 
@@ -1690,7 +1690,7 @@ async def dm(self, ctx: commands.Context, user_id: int, *, message: str):
             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
--- a/redbot/core/utils/_internal_utils.py
+++ b/redbot/core/utils/_internal_utils.py
@@ -278,7 +278,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)


Test patch: