首页 > 解决方案 > 子命令因两个相同的单词而出错

问题描述

我尝试在使用!canada bob!denmark bob命令时获得结果。但它仅适用于 1 个命令,而另一个无法正常工作并出现错误。

错误

in transform
    raise MissingRequiredArgument('{0.name} is a required argument that is missing.'.format(param))
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

代码

@commands.group(pass_context=True)
async def canada(self, ctx):
    if ctx.invoked_subcommand is None:
        await self.bot.say("No subcommand".format(ctx.message))

@commands.group(pass_context=True)
async def denmark(self, ctx):
    if ctx.invoked_subcommand is None:
        await self.bot.say("No subcommand".format(ctx.message))

@canada.command(pass_context=True)
async def bob(self, ctx):
    await self.bot.say("Pong".format(ctx.message))

@denmark.command(pass_context=True)
async def bob(self, ctx):
    await self.bot.say("Pong".format(ctx.message))

标签: python-3.xdiscord.py

解决方案


这可能是因为您为每个子命令指定了相同的名称,因此一个被另一个覆盖。给他们唯一的名字,并使用装饰器的name字段command来分配你希望用户与之交互的名字:

@canada.command(pass_context=True, name='bob')
async def canada_bob(self, ctx):
    await self.bot.say("Pong".format(ctx.message))

@denmark.command(pass_context=True, name='bob')
async def denamrk_bob(self, ctx):
    await self.bot.say("Pong".format(ctx.message))

推荐阅读