如何使Discord的bot命令不区分大小写?

时间:2020-06-03 20:01:41

标签: python discord discord.py

所以我有一个运行discord.py的Discord机器人,它回复Hello!机器人运行时向任何人打招呼的人。代码看起来像

if message.content == "Hello":
        await message.channel.send('Hello!')

它可以工作,但前提是该消息与原始消息完全相同。我想要这样,如果有人打招呼,那么它也会返回Hello!这是可能的,还是我只需要对Hello字符串的每种情况进行编码?

2 个答案:

答案 0 :(得分:1)

将消息内容简单地转换为小写,然后将其与小写命令进行比较。这样,无论输入如何大写,机器人都会看到“ hello”的相同结果。

if message.content.lower() == "hello":
        await message.channel.send('Hello!')

答案 1 :(得分:1)

如果您想使用命令修饰符来执行不区分大小写的命令,则在实例化bot时可以执行以下操作:

bot = commands.Bot(command_prefix="!", case_insensitive=True)

但是,当您使用on_message事件时,可以将其转换为小写:

async def on_message(message):
    if message.content.lower() == "hello": # make sure the comparison string is all lower-case
        await message.channel.send("Hello!")

参考: