Command Case Insensitve

2019-08-02 06:44发布

问题:

How to make below command to work if members use below command in lower or upper or mixing. If members use ping it works. but if members use Ping it not works.

@bot.event
async def on_message(message):
    message.content = message.content.lower()
    await bot.process_commands(message)

    @bot.command(pass_context=True)
    async def ping(ctx):
        msg = 'Pong {0.author.mention}'.format(ctx.message)
        await bot.say(msg)

Update:

above on_message is working correctly in single file but i splitted main file to multiple files. now how to make it work for cog in all files.

回答1:

You can pass a case_insensitive option to your Bot when you create it.

from discord.ext import commands

bot = commands.Bot('!', case_insensitive=True)

@bot.command(pass_context=True)
async def ping(ctx):
    msg = 'Pong {0.author.mention}'.format(ctx.message)
    await bot.say(msg)