Skip to content

"Fake" (test) bot is not responding like the "real" one #35

@ctmbl

Description

@ctmbl

Context

To perform unit testing I use dpytest which honestly works pretty well even being not really completed nor always practical.

it basically adds a configure fucntion that can "configure" the bot by replacing its HTTP object by a fake one, mocking the message sending etc.
Everything is stored in queue and can be tested and checked, we send a fake msg and then check it with:

def some test:
  dpytest.message("!command")
  
  assert dpytest.verify().message().peek().contains().content("g0uZ")

cf code
but the bot configuration is really done by the lib

The problem

I add a test for #30 to test the new help command
Here is how the "real" bot reacts to a !help without this PR

2023-10-15 18:04:31 ERROR    bot.root_pythia_cogs 'help' command failed
Traceback (most recent call last):
 File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/core.py", line 229, in wrapped
   ret = await coro(*args, **kwargs)
 File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/help.py", line 948, in command_callback
   return await self.send_bot_help(mapping)
 File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/help.py", line 1257, in send_bot_help
   note = self.get_ending_note()
 File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/help.py", line 1070, in get_ending_note
   f'Type {self.context.clean_prefix}{command_name} command for more info on a command.\n'
 File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/context.py", line 424, in clean_prefix
   return pattern.sub("@%s" % user.display_name.replace('\\', r'\\'), self.prefix)
AttributeError: 'NoneType' object has no attribute 'display_name'

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

Traceback (most recent call last):
 File "/opt/root-pythia/src/bot/root_pythia_cogs.py", line 156, in on_command_error
   raise error
 File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/bot.py", line 1350, in invoke
   await ctx.command.invoke(ctx)
 File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/core.py", line 1023, in invoke
   await injected(*ctx.args, **ctx.kwargs)  # type: ignore
 File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/core.py", line 238, in wrapped
   raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'display_name'

BUT here is how reacts the fake bot:

Note: I modified the test to print:

def some test:
  dpytest.message("!command")
  
  print(dpytest.get_message().content)
  
  assert dpytest.verify().message().peek().contains().content("g0uZ")
$ pytest -s
...
le msg est: 

RootPythiaCommands:
  adduser
  addusers
  getuser
  resume
  status
No Category:
  help     Shows this message

Type !help command for more info on a command.
You can also type !help category for more info on a category.

conclusion

It doesn't crash and print an incomplete help but still!!
I still haven't figure out why though...
still don't know if the bug is on our side, discordpy's one or dpytest further research will be needed

more on that

To convince ourselves that this is not just the fake bot not caring about custom help, here is what it sends WITH the pr #30

$ pytest -s
...
le msg est: 

RootPythiaCommands:
  adduser  Add rootme user with <user_id> to the tracked users
  addusers Add several rootme users with [user_ids...] to the tracked users
  getuser  Get rootme user info with <user_id>
  resume   The bot leaves its idle state
  status   Give info on bot status
No Category:
  help     Shows this message

Type !help command for more info on a command.
You can also type !help category for more info on a category.
...

totally expected

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions