Discord.py |命令名中是否可以有空格?

2024-09-29 19:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想问,我可以在discord.py bot的命令名中留出空间吗,就像命令是-slowmode off


Tags: py命令bot空间discordoffslowmode
2条回答

如果您使用的是^{},那么据我所知,命令名中不能有空格。但是,您可以:

  • 命令with parameters(在本例中,这可能是您想要的),或者
  • 命令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将导致^{} exception

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

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

它将被调用为-slowmode off-slowmode any

这可能是最好的办法

相关问题 更多 >

    热门问题