python-3.x - python子命令cog文件
问题描述
您好,我在主页中的子命令一切正常。但是,如果我将命令移动到 cog 文件子命令不起作用,我添加了self, ctx
,self.bot
但它仍然不知道我在哪个地方做错了。
@commands.group(pass_context=True)
async def first(self, ctx):
if ctx.invoked_subcommand is None:
await self.bot.say("Ping 1")
@first.group(pass_context=True)
async def second(self, ctx):
if ctx.invoked_subcommand is second:
await self.bot.say("Ping 2")
@second.command(pass_context=True)
async def third(self, ctx):
await self.bot.say("Ping 3")
解决方案
second
second
评估主体时未定义。此外,invoked_subcommand
将永远在second
这里,即使你也调用third
.
您应该将invoke_without_command
属性传递给您的group
装饰器。
@commands.group(pass_context=True, invoke_without_command=True)
async def first(self, ctx):
await self.bot.say("Ping 1")
@first.group(pass_context=True, invoke_without_command=True)
async def second(self, ctx):
await self.bot.say("Ping 2")
@second.command(pass_context=True)
async def third(self, ctx):
await self.bot.say("Ping 3")
编辑:
仔细想想,这可能是多虑了。你只需要second
通过类来解决它是一种方法:
class MyCog:
@commands.group(pass_context=True)
async def first(self, ctx):
if ctx.invoked_subcommand is None:
await self.bot.say("Ping 1")
@first.group(pass_context=True)
async def second(self, ctx):
if ctx.invoked_subcommand is MyCog.second: # Possibly self.second instead, but I'm not sure.
await self.bot.say("Ping 2")
@second.command(pass_context=True)
async def third(self, ctx):
await self.bot.say("Ping 3")
推荐阅读
- r - R根据另一个值创建和填充新列
- javascript - 使用 JSON 初始化时,Google 地图不再提供图块
- c# - 在 Listview 列而不是行的 Xamarin.Forms 中显示数据
- python - Google Cloud Dataproc OOM 问题
- java - Android Studio:突然的 Gradle 同步错误 - 拼写错误的类名?
- java - 等待特定的时间,但不要打扰应用程序的任何其他功能
- spring - Tomcat 显示错误的 http 状态
- excel - 如何将一个单元格的内容复制到另一个单元格中的特定字符串
- python - Python 云数据?
- javascript - 使用值获取 Javascript 对象