首页 > 解决方案 > 不和谐.py | 我可以在命令名中有空格吗?

问题描述

我想问一下我的 discord.py 机器人的命令名称中是否可以有空格,就像命令一样-slowmode off

标签: pythonpython-3.xdiscorddiscord.py

解决方案


如果您使用的是ext.commands,那么据我所知,您不能使用其中包含空格的命令名称。但是,您可以拥有:

  • 带参数的命令(在这种情况下您可能想要),或者
  • 命令,允许使用多字前缀创建命令。

对于第一种情况,您可以执行以下操作:

@bot.command()
async def slowmode(ctx, arg):
    # do something...
    await ctx.send('slowmode set to ' + str(arg))

...并使用-slowmode offor调用它-slowmode hello

对于第二种情况:

@bot.group(invoke_without_command=True)
async def slowmode(ctx):
    await ctx.send('You must provide a subcommand, for example `-slowmode on` or `-slowmode off`; see `-help` for more')

@slowmode.command(name='on')
async def slowmode_enable(ctx):
    # do something...
    await ctx.send('slowmode is set to on')

@slowmode.command(name='off')
async def slowmode_disable(ctx):
    # do something...
    await ctx.send('slowmode is set to off')

...并且调用-slowmode将显示错误消息,-slowmode on或者-slowmode off将运行适当的命令,并-slowmode hello会导致CommandNotFound异常


推荐阅读