首页 > 解决方案 > 命令大小写不敏感

问题描述

如果成员在下部或上部或混合中使用以下命令,如何使以下命令起作用。如果会员使用ping 它就可以了。但如果成员使用Ping它不起作用。

@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)

更新:

以上on_message在单个文件中正常工作,但我将主文件拆分为多个文件。现在如何使它适用于所有文件中的 cog。

标签: pythonpython-3.xdiscorddiscord.py

解决方案


您可以在创建选项时将case_insensitive选项传递给您的选项Bot

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)

推荐阅读