Astic
Astic

Reputation: 29

How to make commands for discord bot non-case sensitive

const Discord = require("discord.js");
//ignore 1 up and 1 down
const client = new Discord.Client();
//prefix 
const prefix = `+`;
//log for me
client.once(`ready`, () => {
    console.log("IM POWERED UP")
});


//commands

client.on(`message`, message =>{
    const args = message.content.slice(prefix.lengh).split(/ +/);

    //Username..(embed)
    if (message.content === `${prefix}Username`){
        let messageArray = message.content.split(" ");
        let args = message.content.split(` `);
        let command = messageArray[1].toLowerCase();
        const embed = new Discord.MessageEmbed()
        .setTitle(`user information`)
        .addField(`Your Username is`, message.author.username)
        .addField(`Bugs`, `To report bugs please go to TxPsycho#1080 and you may earn a reward!`)
        .setThumbnail(message.author.displayAvatarURL())
        .setFooter(`Wanna get Valorant cheats? Join here`)
        .setColor(`RANDOM`)
        message.channel.send(embed);
    }
    
    //help command
    if (message.content === `${prefix}Help`){
        const embed = new Discord.MessageEmbed()
        .setTitle(`Help`)
        .addField(`A list of commands are below!`)
        .setThumbnail(message.author.displayAvatarURL())
        .setDescription(`AMOTHERFUCKINGTESTYOUMOTHERFUCKAYOUTHINKITSFUNNYCALLINGAT2AM`)
        .setColor(`RANDOM`)
        message.channel.send(embed);
    }
});


//ignore this and leave it at bottom
client.login(`Nz`)


I am not sure how to make the commands caps insensitive, this is the error it’s been giving me. I have been searching but found no information. Down below is the link to an error that I have been receiving when I try to debug the code.

Click here to see the error picture

Upvotes: 3

Views: 99

Answers (2)

user13429955
user13429955

Reputation:

Like rogue said the issue is calling toLowerCase on undefined here:

let command = messageArray[1].toLowerCase();

The real command would be at 0, since arrays are 0 indexed

You might want to shift it instead so you can get the args easier:

Message: !help 12 24 36

const args = message.content.split(" ");
const command = args.shift().toLowerCase();

console.log(args);
// => [12, 24, 36]

console.log(args[0]);
// => 12

After that you just compare command instead of message.content:

if(command === "!help") {


}

Upvotes: 1

N M
N M

Reputation: 151

You can sanitise messages with toLowerCase

console.log('ALPHABET'.toLowerCase()); // 'alphabet'
console.log('aLPhABeT'.toLowerCase()); // 'alphabet'
console.log('alphabet'.toLowerCase()); // 'alphabet'

Upvotes: 0

Related Questions