Repository: Cog-Creators/Red-DiscordBot

Issue:
[Core] Set name and nickname should handle length errors in a user friendly way
# Command bugs
#### Command name
`[p]set name` and `[p]set nickname`
#### What cog is this command from?
Core
#### What were you expecting to happen?
The bot to friendly say the name was too long
#### What actually happened?
`[p]set name` sent "Failed to change name. Remember that you can only do it up to 2 times an hour.", which is an unrelated error message
`[p]set nickname` fully errored
#### How can we reproduce this issue?
Use the commands above with a really long name


Discussion:
Please don't break this Github, I'm on mobile

```py
Exception in command 'set nickname'
Traceback (most recent call last):
  File "C:\Users\NeuroAssassin\modok\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\NeuroAssassin\modok\lib\site-packages\redbot\core\core_commands.py", line 1941, in _nickname
    await ctx.guild.me.edit(nick=nickname)
  File "C:\Users\NeuroAssassin\modok\lib\site-packages\discord\member.py", line 552, in edit
    await http.change_my_nickname(guild_id, nick, reason=reason)
  File "C:\Users\NeuroAssassin\modok\lib\site-packages\discord\http.py", line 245, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In nick: Must be 32 or fewer in length.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\NeuroAssassin\modok\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\NeuroAssassin\modok\lib\site-packages\redbot\core\commands\commands.py", line 825, in invoke
    await super().invoke(ctx)
  File "C:\Users\NeuroAssassin\modok\lib\site-packages\discord\ext\commands\core.py", line 1307, in invoke
    await ctx.invoked_subcommand.invoke(ctx)
  File "C:\Users\NeuroAssassin\modok\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\NeuroAssassin\modok\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In nick: Must be 32 or fewer in length.

```
Fixing this should only require a length check before doing the API request in both `[p]set name` and `[p]set nickname`. PRs welcome.

Resolving patch:
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
@@ -1919,6 +1919,9 @@ async def stream(self, ctx: commands.Context, streamer=None, *, stream_title=Non
     async def _username(self, ctx: commands.Context, *, username: str):
         """Sets [botname]'s username."""
         try:
+            if len(username) > 32:
+                await ctx.send(_("Failed to change name. Must be 32 characters or fewer."))
+                return
             await self._name(name=username)
         except discord.HTTPException:
             await ctx.send(
@@ -1938,6 +1941,9 @@ async def _username(self, ctx: commands.Context, *, username: str):
     async def _nickname(self, ctx: commands.Context, *, nickname: str = None):
         """Sets [botname]'s nickname."""
         try:
+            if len(nickname) > 32:
+                await ctx.send(_("Failed to change nickname. Must be 32 characters or fewer."))
+                return
             await ctx.guild.me.edit(nick=nickname)
         except discord.Forbidden:
             await ctx.send(_("I lack the permissions to change my own nickname."))


Test patch: