【发布时间】:2023-03-21 15:32:01
【问题描述】:
我想问一下,我的 discord.py 机器人的命令名称中是否可以有空格,例如命令是 -slowmode off
【问题讨论】:
-
你已经尝试过什么?
标签: python python-3.x discord discord.py
我想问一下,我的 discord.py 机器人的命令名称中是否可以有空格,例如命令是 -slowmode off
【问题讨论】:
标签: python python-3.x discord discord.py
如果您使用的是ext.commands,那么据我所知,您不能使用包含空格的命令名称。但是,您可以:
对于第一种情况,你可以这样做:
@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。
【讨论】:
因为看起来你想添加一个参数,你可以这样做:
@bot.command()
async def slowmode(ctx, mode):
if mode.lower() == "off":
# do something
将被调用为-slowmode off 或-slowmode any
这可能是最好的方法
【讨论】: