0

I'm trying to make my bot answer with a one word to multiple variants of message:

const bomba = new Discord.Client();
    
const a = "bomba" || "bomb" || "bob";
const b = "hey" || "sup" || "hello" || "hi";

bomba.on("message", message => {
    if (message.author == bomba.user) return;
    if (message.content.toLowerCase() === a + b) {
        bomba.channels.cache.get(`${message.channel.id}`).send("Hi!");
    };
});

How do I make this work?

Skulaurun Mrusal
  • 2,792
  • 1
  • 15
  • 29
Pikshet
  • 3
  • 1

2 Answers2

2

You can use Array.includes():

if (["bomba", "bomb", "bob"].includes(message.content.toLowerCase())) {
    message.channel.send("Hi!");
};

Note that it would be better to compare the users by their User.id property, rather than checking if they refer to the same instance like you do in your code.

if (message.author.id == bomba.user.id) return;

From MDN docs about the == operator:

If the operands are both objects, return true only if both operands reference the same object.

Skulaurun Mrusal
  • 2,792
  • 1
  • 15
  • 29
0

You can use a Regex and the .match() function to check the content of the message against several words. Take a look at the code below and give it a try:

const bomba = new Discord.Client();
    
const clientNames = ["bomba", "bomb", "bob"].join('|');
const greetings = ["hey", "sup", "hello", "hi"].join('|');

const regex = new RegExp(`^(${clientNames})\\s(${greetings})$`, 'gi');

bomba.on("message", message => {
    if (message.author == bomba.user) return;
    if (message.content.match(regex)) {
        bomba.channels.cache.get(`${message.channel.id}`).send("Hi!");
    }
});

For more information about the regex, take a look at this stackoverflow question/answer

T. Dirks
  • 3,566
  • 1
  • 19
  • 34
  • It works just like I wanted, but there's one problem: https://imgur.com/qCf5OAY It doesn't reply when sees any other symbols or words beetween or with necessary words – Pikshet Aug 17 '21 at 18:19