discord.js upper case & lower case embed message not working

Tags: , ,



So I wrote a “test” command and wanted to make it in a way where when people type -test, the upper cases and lower cases don’t matter. The command should work with -test, -Test, -TEST, etc. Currently, the command only works with -test and no upper cases. I also tried changing it from toLowerCase(); to toUpperCase(); and the (command === 'test') to (command === 'TEST') but it didn’t help. Only the -test command with no upper cases worked.

Here’s my code:

client.on('message', message =>{

    if (!message.content.startsWith(prefix + 'test') || message.author.bot) return;

    var pfpMember = message.mentions.members.first() || message.member;

    const args = message.content.slice(prefix.length).trim().split('  ');
    const command = args.shift().toLowerCase();

    if (command === 'test'){

        const testEmbed = new Discord.MessageEmbed()
            .setColor('#6567AD')
            .setTitle('Some title')
            .setAuthor(pfpMember.user.username, pfpMember.user.displayAvatarURL())
            .setDescription('Some description here')
            .setThumbnail(pfpMember.user.displayAvatarURL())
            .addField('something', 'Some value here')
            .addField('something', 'Some value here', true)
            .addField('something', 'Some value here', true)
            .addField('something', 'Some value here', true)
            .setImage(pfpMember.user.displayAvatarURL())
            .setFooter('Some footer text here', pfpMember.user.displayAvatarURL());

        message.channel.send(testEmbed);
    }
});

Answer

If you want your command to be case insensitive, you should coerce the user input to one case (lowercase). You’ve already done that with const command = args.shift().toLowerCase(). However, at the beginning of your code you stick an unnecessary and inhibiting

if (!message.content.startsWith(prefix + 'test') || message.author.bot) return;

Remove that “+ ‘test'”.

Also don’t split by two spaces, you could probably split by a whitespace regular expression:

Your polished code should now be

client.on('message', message =>{

     if(!message.content.startsWith(prefix) || message.author.bot) return;
    var pfpMember = message.mentions.members.first() || message.member;

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if (command === 'test'){

        const testEmbed = new Discord.MessageEmbed()
            .setColor('#6567AD')
            .setTitle('Some title')
            .setAuthor(pfpMember.user.username, pfpMember.user.displayAvatarURL())
            .setDescription('Some description here')
            .setThumbnail(pfpMember.user.displayAvatarURL())
            .addField('something', 'Some value here')
            .addField('something', 'Some value here', true)
            .addField('something', 'Some value here', true)
            .addField('something', 'Some value here', true)
            .setImage(pfpMember.user.displayAvatarURL())
            .setFooter('Some footer text here', pfpMember.user.displayAvatarURL());

        message.channel.send(testEmbed);
    }
});


Source: stackoverflow