discord.py 中的多字命令(命令扩展)

Multiple word command in discord.py (commands extension)

我正在为我的 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

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

非常感谢你的帮助:)

您可能想要使用命令组


@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

注意:请原谅我在移动设备上的丑陋链接,当我到达计算机时,我会编辑它们作为参考

只是把我的评论写成提案。我不认为你可以制作多名称命令。可能是设计使然,因为我认为这会带来安全风险并增加大量处理。在后端,每条消息都必须提前知道命令有多少部分。

我建议你这样做

And yeah I know, I could take "prefix" as an additional argument.

但不是

But then I have to do all setup commands using one function.

会是这样的:

# all of this is in the same class whatever that's called. 

async def handle_setup_giraffe(self, ctx: Context):
    pass

@commands.command(name="setup")
async def setup(self, ctx: Context):
    setup_command = ctx.args[0]
    if hasattr(self, f"handle_setup_{setup_command}"):
        return getattr(self, f"handle_setup_{setup_command}")(ctx)
    else:
        raise SomeException("Setup command not found", setup_command)