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?