Creating case-insensitive commands for a Discord bot in Python

I’m trying to make my Discord bot commands case-insensitive. I’ve set case_insensitive=True in the bot initialization, but it doesn’t seem to work. Here’s my current code:

import discord
from discord.ext import commands

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

@bot.command()
@commands.has_role('Moderator')
async def user_info(ctx, member: discord.Member):
    embed = discord.Embed(title=f'{member.name} Profile', description='User details', color=0x3498db)
    embed.add_field(name='Username', value=member.name, inline=True)
    embed.add_field(name='User ID', value=member.id, inline=True)
    embed.add_field(name='Account Created', value=member.created_at.strftime('%Y-%m-%d'), inline=True)
    embed.set_thumbnail(url=member.avatar_url)
    await ctx.send(embed=embed)

Is there another way to make commands case-insensitive without using on_message? I’ve heard that’s not the best approach. Any help would be appreciated!

I’ve dealt with this issue in my Discord bot projects. One approach that’s worked well for me is using the commands.CommandNotFound error handler. You can implement it like this:

@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
cmd = ctx.invoked_with.lower()
for command in bot.commands:
if cmd == command.name.lower() or cmd in [alias.lower() for alias in command.aliases]:
await ctx.invoke(command)
return

This method catches command not found errors and checks if a matching command exists in a case-insensitive manner. If found, it invokes the correct command. It’s efficient and doesn’t require modifying each command individually.

I’ve encountered this issue before, and there’s actually a simple solution that worked for me. The case_insensitive=True parameter should be working correctly, but sometimes it doesn’t take effect immediately. Try restarting your bot after making this change.

If that doesn’t solve it, you might want to check your Discord.py version. Older versions had some bugs with case insensitivity. Updating to the latest stable release could potentially fix the problem.

Another thing to consider is your command registration method. If you’re using cogs, make sure you’re passing the case_insensitive parameter when setting up each cog as well.

Lastly, double-check that you’re not accidentally overriding the case insensitivity setting somewhere else in your code. It’s easy to miss, especially in larger projects.

hey mate, have u tried using aliases for ur commands? u can add different case versions as aliases. like this:

@bot.command(aliases=[‘USER_INFO’, ‘User_Info’])
async def user_info(ctx, member: discord.Member):
# ur existing code here

this way u cover different cases without changing much. hope it helps!