【问题标题】:Discord.py | Can I have space in command name?不和谐.py |我可以在命令名中有空格吗?
【发布时间】:2023-03-21 15:32:01
【问题描述】:

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

【问题讨论】:

  • 你已经尝试过什么?

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


【解决方案1】:

如果您使用的是ext.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

【讨论】:

    【解决方案2】:

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

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

    将被调用为-slowmode off-slowmode any

    这可能是最好的方法

    【讨论】:

    • 等我试试
    • 将答案标记为正确的:> 欢迎来到 Stack Overflow
    • 这个也可以,但我标记为答案的那个是更好的方法
    猜你喜欢
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    相关资源
    最近更新 更多