Dmitry T
Dmitry T

Reputation: 54

How can I add multiple parameters to message.content?

Need some help with my code. I started coding my first Discord bot today using Node.JS and the Discord.JS library, and others soon. I am currently using a YouTube video to guide me through Node.JS. Here's my question.

Here is my current code.

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

bot.on('message', (message) => {

    if(message.content == 'ping') {
        message.reply('pong');
    }

    if(message.content == 'xd') {
        message.reply('smh');
    }

});

This code works fine. If you go over to the second message.content, it has 'xd' written inside it. When I write the word xd in my Discord server, the bot will return smh. The problem with this is that the bot will only return smh when the capitalization is exactly like it is in xd, but I want it to work for all capitalization.

Like this.

    if(message.content == 'xd', 'xD', 'XD', 'Xd',) {
        message.reply('pong');
}

Obviously, the above code doesn't work, but is there any easy way to do this?

Cheers.

Upvotes: 1

Views: 7565

Answers (7)

Hello
Hello

Reputation: 1

you can use something like || to have multiple parameters. In your case it would look like this

if(message.content == 'xd' || 'Xd' || 'xD' || 'XD') {
    message.reply('idk');
}

But in your case (because it is always the two same letters in the same order) you can simply add .toLowercase() and then the message will be treated as if it had only lowercase letters and you won't have to worry.

example:

if(message.content.toLowercase() === 'xd') {
    message.reply('idk');
}

However there's also a third option to have multiple parameters, you can simply use a constant (const)

example:

const 1 = [ "xd" , "lmao" , "lmfao" ]

if(message.content == 1) {
    message.reply('idk');
}

You can change "1" for info

Have a nice day :)

oh and thanks to the moderator that corrected me and sorry for my mistake.

Upvotes: -1

Noobly387
Noobly387

Reputation: 43

There are 2 other main ways to allow for multiple parameters besides the toLowerCase() function.

The first way would be to use the || operator or the or operator. This method allows for you to check for completely different words while being able to have the same outcome. For example:

if (message.content == "xd" || message.content == "xD" || message.content == "XD") { 
     message.reply("Why would you do that");
}

The second way would be to use regular expressions like regexes. Regexes check for words inside strings by using the .test() method. For example:

let myRegex = /xd/i;
if (myRegex.test(message.content)) { //We are assuming that message.content = "XD"
   message.reply("smh");
}

This method works because the regex checks whether the word is equal to the regex it is assigned to. The i after the /xd/ means that it doesn't care about the casing so this is a shorter way to check for certain words without caring about letter case.

Upvotes: 1

Frustrated programmer
Frustrated programmer

Reputation: 711

A better way todo this is have aliases...

//defined early on.
let commands = [
    {
        aliases:["xd","lol",":smile:"],
        run:function(msg){
            msg.send(`pong`);
        }
    },
    {
        aliases:[":("],
        run:function(msg){
            msg.send(`why the long face?`);
        }
    }
]
//later
client.on(`message`,function(msg){
    for(let i =0;i<commands;i++){
        if(commands[i].includes(msg.content.toLowerCase())){
            commands[i].run();
        }
    }
});

string.toLowerCase as answered above doesn't always work when you want multiple things to run the same command.
The code above allows aliases for every commands

Upvotes: 0

Eray Chumak
Eray Chumak

Reputation: 216

Before I answer the question, make sure hide your bot's token when sharing your source code because then people can use your bot to do some harmful stuff if it has the right permissions.

Okay so the answer:

At the very beginning, declare a variable called msg or something that stores message.content.toLowerCase();. So no matter how the person types it, it will always be lower case.

Be aware that it will only work if message.content is "xd" only so if a person type "xD something", this wouldn't work.

Additional stuff to your code:

If you want to check if a person used a certain prefix to signify a command, you can check if message.content.startsWith("prefix"); - this will return true or false.

It's always good to break down the content of the message into separate variables allowing you to easily do what you wanna do with the message, for example, a command and args variable (convert message.content into an array first).

Upvotes: 1

David Saputra
David Saputra

Reputation: 11

You need to lowercase the message.content using method toLowerCase() method then compare it with "xd string using === operator

ex:

if(message.content && message.content.toLowerCase() === 'xd') {
    message.reply("smh");
}

Upvotes: 1

Amit Wagner
Amit Wagner

Reputation: 3264

use toLowerCase on the content and then test it

if(message.content.toLowerCase() === 'xd') {
   message.reply('pong');
}

Upvotes: 0

Lucas S.
Lucas S.

Reputation: 2401

You can call .toLowerCase() on message.content to transform it to all lower case:

if (message.content.toLowerCase() == 'xd') {
  message.reply('pong');
}

This way the message's casing is practically ignored.

Upvotes: 1

Related Questions