python-3.x - 子命令因两个相同的单词而出错
问题描述
我尝试在使用!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))
解决方案
这可能是因为您为每个子命令指定了相同的名称,因此一个被另一个覆盖。给他们唯一的名字,并使用装饰器的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))
推荐阅读
- sql-server - 如何删除字符';'左侧的字符串和包含的字符串'U'然后显示它?
- json - 如何从响应中获得类似的计数值?
- javascript - 在服务器上部署 React App 时 MIME 类型检查失败
- algorithm - 如何从点列表中获取折线的顶点?
- ssl - 使用过时的密码套件对连接进行加密
- java - Maven:项目设置
- c# - 如何在 c++/cli 中使用 Runspace.StateChanged 事件?
- python - 使用 ImageGrab 的 Python 屏幕记录
- javascript - 给定来自 API 的 JSON 数据,在 React 中动态呈现数据网格/表格
- realm - 高 CPU 使用领域