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...
command1 micha I am getting the error, that the parameter "name" is required.command1 micha test, then it prints at the line print(ctx): "micha". I expected that I am getting the context as the first parameter (after self)
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.
discord.py version 1.3.4
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.
Most helpful comment
I ran into the same issue. My fix was to do something like this: