如何使 Discord 的机器人命令不区分大小写?

The*_*ide 1 python discord discord.py

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

if message.content == "Hello":
        await message.channel.send('Hello!')
Run Code Online (Sandbox Code Playgroud)

它有效,但前提是消息的大小写与原始消息完全相同。我希望如果有人说你好,那么它也会返回你好!这是可能的,还是我只需要对 Hello 字符串的每一个案例进行编码?

Dig*_*gy. 5

如果您想使用命令装饰器创建不区分大小写的命令,则在实例化机器人时可以执行以下操作:

bot = commands.Bot(command_prefix="!", case_insensitive=True)
Run Code Online (Sandbox Code Playgroud)

但是当您使用该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!")
Run Code Online (Sandbox Code Playgroud)

参考: