首页 > 解决方案 > discord.py 中的多字命令(命令扩展)

问题描述

我正在为我的 Discord 机器人使用 discord.ext.commands 模块,我想要一个名为“setup prefix”的多字命令。

当我使用该命令时,它会抛出一个 CommandNotFound 错误,它表示“setup”不是命令。所以看起来 discord.py 只检查第一个单词。

有没有办法解决这个问题?

这是我的代码片段:

@commands.command(name="setup prefix")
async def set_prefix(self, ctx: Context, prefix: str):
    pass

是的,我知道,我可以将“前缀”作为附加参数。但是我必须使用一个功能来完成所有设置命令。

我会很感激你的帮助:)

标签: pythondiscordcommanddiscord.py

解决方案


您可能想要使用命令组


@commands.group(invoke_without_subcommand=True)
async def setup(self, ctx, *args):
    # general functionality, help, or whatever.
    pass

@setup.command()
async def prefix(self, ctx, prefix):
    #logic
    pass

请查看文档以获取更多信息和示例

https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=commands%20group#discord.ext.commands.group

另请阅读库常见问题解答,其中包含此用例的一些示例。https://discordpy.readthedocs.io/en/latest/faq.html

注意:请原谅我在手机上的丑陋链接,当我到达电脑时,我会将它们编辑为参考


推荐阅读