Making Discord bot commands resistant to letter case

How to Make Commands Case-Insensitive in Discord Bot

I’m developing a Discord bot, and I’m encountering a challenge regarding command recognition. Currently, it seems that my bot only reacts when commands are typed with the exact case. For instance, if a user types +help instead of +Help, the bot doesn’t respond as expected.

Here’s the code that I’m currently using for my bot:

const Discord = require('discord.js');
const bot = new Discord.Client();
const cmdPrefix = '+';

bot.once('ready', () => {
    console.log('Bot is now running!');
});

bot.on('message', msg => {
    const args = msg.content.slice(cmdPrefix.length).split(/ +/);
    
    // Username command
    if (msg.content.toLowerCase() === `${cmdPrefix}username`) {
        const userEmbed = new Discord.MessageEmbed()
            .setTitle('User Information')
            .addField('Your Username:', msg.author.username)
            .addField('Bugs', 'For bug reporting, please reach out to TxPsycho#1080')
            .setThumbnail(msg.author.displayAvatarURL())
            .setFooter('For Valorant cheats, join us!')
            .setColor('RANDOM');
        msg.channel.send(userEmbed);
    }
    
    // Help command
    if (msg.content.toLowerCase() === `${cmdPrefix}help`) {
        const helpEmbed = new Discord.MessageEmbed()
            .setTitle('Help Commands')
            .addField('Available Commands', 'Here’s a list of commands you can use!')
            .setThumbnail(msg.author.displayAvatarURL())
            .setDescription('This bot is designed to assist with server operations.')
            .setColor('RANDOM');
        msg.channel.send(helpEmbed);
    }
});

bot.login('your_token_here');

What changes can I make so that all variations of the commands like +info, +INFO, and +Info will trigger the same response?

I see the problem. You’re checking for exact command matches after toLowerCase(), but you’re slicing the content for args without using them for comparison. Extract the command separately: const command = args[0].toLowerCase(); then check if (command === 'username') instead of comparing the whole message. Way more flexible and handles edge cases better. Had the same issue when I started - this fixed my inconsistent command recognition.

you’re already using toLowerCase() on msg.content so that should work. maybe there’s extra spaces after the command? try msg.content.toLowerCase().trim() instead.

Your problem is using toLowerCase() on the whole message but comparing against the full string with prefix. Users add extra characters and spacing, which breaks things. I fixed this by normalizing only the command part after stripping the prefix. Use const command = msg.content.slice(cmdPrefix.length).toLowerCase().trim(); then check if (command === 'username') or if (command === 'help'). This separates prefix handling from command matching and handles user input variations way better.