使不和谐机器人的命令不区分大小写

时间:2021-02-22 15:41:48

标签: discord.py

我有一个名为 repeat 的命令,我希望用户能够通过键入 $Repeat 和 $repeat 来调用它,以防他们被大写字母混淆。如果可能,我希望他们也能够使用 $rep 和 $Rep 调用它。

@bot.command(name='repeat', help='type a sequence of words seperated by spaces and they will be repeated back to you as a sequence of messages')
async def repeat(ctx, *lines):
    print("repeating")
    await asyncio.gather(*[ctx.send(line) for line in lines])

我从文档中看不到这样做的方法 - 除了创建调用相同函数的命令 rep、Rep 和 Repeat - 但这感觉不雅。

1 个答案:

答案 0 :(得分:1)

假设这是一个 discord.ext.commands.Bot 实例,您可以通过在实例化您的机器人时指定 case_insensitive=True 来实现不区分大小写的匹配

from discord.ext import commands

bot = commands.Bot(prefix='$', case_insensitive=True)

第二种可以通过将 aliases 参数传递给命令装饰器来实现

@bot.command(name='repeat', aliases=['rep'], help='...')