不和谐.py |命令名称中可以有空格吗?

时间:2021-05-17 12:57:08

标签: python python-3.x discord discord.py

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

2 个答案:

答案 0 :(得分:3)

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

  • commands with parameters(在本例中可能是您​​想要的),或
  • command groups 允许使用多字前缀创建命令。

对于第一种情况,您可以:

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

...并使用 -slowmode off-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 exception .

答案 1 :(得分:1)

因为看起来你想添加一个参数,你可以这样做:

@bot.command()
async def slowmode(ctx, mode):
    if mode.lower() == "off":
        # do something

将作为 -slowmode off-slowmode any 调用

这可能是最好的方法