Repository: Cog-Creators/Red-DiscordBot

Issue:
[Core] set game no arg bug
# Command bugs

#### Command name

set game

#### What cog is this command from?
Core

#### What were you expecting to happen?

The game to be cleared.

#### What actually happened?

Command errored our due to game being none. We check len of game before we check that it exists due to it being optional. 

```py
Exception in command 'set game'
Traceback (most recent call last):
  File "/data/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "/data/venv/lib/python3.8/site-packages/redbot/core/core_commands.py", line 1082, in _game
    if len(game) > 128:
TypeError: object of type 'NoneType' has no len()

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/data/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "/data/venv/lib/python3.8/site-packages/redbot/core/commands/commands.py", line 799, in invoke
    await super().invoke(ctx)
  File "/data/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1234, in invoke
    await ctx.invoked_subcommand.invoke(ctx)
  File "/data/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/data/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object of type 'NoneType' has no len()
```

#### How can we reproduce this issue?

[p]set game



Discussion:

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
@@ -1079,21 +1079,19 @@ async def avatar(self, ctx: commands.Context, url: str):
     async def _game(self, ctx: commands.Context, *, game: str = None):
         """Sets [botname]'s playing status"""
 
-        if len(game) > 128:
-            await ctx.send("The maximum length of game descriptions is 128 characters.")
+        if game:
+            if len(game) > 128:
+                await ctx.send("The maximum length of game descriptions is 128 characters.")
+                return
+            game = discord.Game(name=game)
         else:
-            if game:
-                game = discord.Game(name=game)
-            else:
-                game = None
-            status = (
-                ctx.bot.guilds[0].me.status if len(ctx.bot.guilds) > 0 else discord.Status.online
-            )
-            await ctx.bot.change_presence(status=status, activity=game)
-            if game:
-                await ctx.send(_("Status set to ``Playing {game.name}``.").format(game=game))
-            else:
-                await ctx.send(_("Game cleared."))
+            game = None
+        status = ctx.bot.guilds[0].me.status if len(ctx.bot.guilds) > 0 else discord.Status.online
+        await ctx.bot.change_presence(status=status, activity=game)
+        if game:
+            await ctx.send(_("Status set to ``Playing {game.name}``.").format(game=game))
+        else:
+            await ctx.send(_("Game cleared."))
 
     @_set.command(name="listening")
     @checks.bot_in_a_guild()


Test patch: