Discord.py: commands.Bot as subclass is not sending context

Created on 23 Jul 2020  路  4Comments  路  Source: Rapptz/discord.py

Summary

I try to create a subclass of commands.Bot and add there methods to the command. But when I run the bot and write a command it looks like the command is not passing a context.

class Bot(commands.Bot):
    def init (self):
        super().init(commandprefix="*", description=None)

        members = inspect.getmembers(self)
        for _, member in members:
            if isinstance(member, commands.Command):
                if member.parent is None:
                    self.add_command(member)

    async def onready(self):
        print('Logged in as ' + self.user.name + '  ID: ' + str(self.user.id))
        ...

    @commands.command()
    async def command1(self, ctx, name: str):
        print(ctx)
        # Do something...

Reproduction Steps

  • When I am writing the command command1 micha I am getting the error, that the parameter "name" is required.
  • When I am sending command1 micha test, then it prints at the line print(ctx): "micha".

Expected Results

I expected that I am getting the context as the first parameter (after self)

Actual Results

I am not getting the context, I am getting the first parameter written in the chat command.

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 790, in invoke
    await self.prepare(ctx)
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 751, in prepare
    await self._parse_arguments(ctx)
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 670, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 516, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: name is a required argument that is missing.

Checklist

  • [x] I have searched the open issues for duplicates.
  • [x] I have shown the entire traceback, if possible.
  • [x] I have removed my token from display, if visible.

System Information

discord.py version 1.3.4

duplicate

Most helpful comment

I ran into the same issue. My fix was to do something like this:

class MyBot(commands.Bot):
    def __init__(self, *args, **kwargs):
        ...
        self.command_func = commands.command(name="...", ...)(self.command_func)
        ...


    def command_func(self, ctx, arg: str):
        pass

All 4 comments

Commands inside a bot subclass are unsupported due to this bug. A workaround is to remove the self argument and get the bot via ctx.bot instead.

I ran into the same issue. My fix was to do something like this:

class MyBot(commands.Bot):
    def __init__(self, *args, **kwargs):
        ...
        self.command_func = commands.command(name="...", ...)(self.command_func)
        ...


    def command_func(self, ctx, arg: str):
        pass

Thanks @PythonCoderAS. You helped me to find a way. I did it now like that:

class MyBot(commands.Bot):
    def __init__(self, *args, **kwargs):
        ...
        self.add_command(commands.command(self.command_func, name="..."))
        ...

    def command_func(self, ctx, arg: str):
        pass

This is a duplicate of https://github.com/Rapptz/discord.py/issues/2031 which is a wontfix. Making this work is not really something I plan on doing -- if you want commands to be contained you're better off using cogs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

j0hnmeow picture j0hnmeow  路  3Comments

PicklesJars picture PicklesJars  路  3Comments

jzburda picture jzburda  路  3Comments

Be4Vision picture Be4Vision  路  3Comments

superloach picture superloach  路  3Comments