0

I've been working on this for the better part of 3 hours, and I'm about to just throw this part away, but I still want to know how to fix it.

DISCLAIMER- I'm new to node, and I'm still teaching myself js. This bot is my first major working project.

Basically I want this bot to respond with a random answer from an array based on what was said.

So the top 4 arrays are things the user would say + the bot's name. (Markos)

The next 5 arrays are potential responses that the bot should choose randomly. There are ~20 responses per array, so I won't include the responses.

The MENTION array should be used if none of the words in the first four arrays are used with "Markos".

Right now, ONLY the MENTION array is being accessed, and I'm not sure why. Insight would be great. If you could just point me in the right direction/explain why what I'm doing is wrong, I'd be very thankful.

let posGreet = ["hi", "hello", "hey", "yo", "greetings", "hallo", "Grüße"]
let posBye = ["bye", "goodbye", "later", "peace", "cya", "see you later", "tschüss", "auf wiedersehen", "bis später"]
let posThanks = ["thanks", "thank you", "vielen dank", "danke", "gracias"]
let posInsults = ["insults"]

let GREETINGS = ["too many to list"]
let THANKS = ["too many to list"]
let BYE = ["too many to list"]
let MENTION = ["too many to list"]
let INSULTS = ["too many to list"]

else if (message.content.toLowerCase().includes("markos")) {
    if (message.content.toLowerCase().includes(posGreet)) {
      message.reply(GREETINGS[Math.floor(Math.random() * GREETINGS.length)]);
    } else if (message.content.toLowerCase().includes(posThanks)) {
      message.reply(THANKS[Math.floor(Math.random() * THANKS.length)]);
    } else if (message.content.toLowerCase().includes(posBye)) {
      message.reply(BYE[Math.floor(Math.random() * BYE.length)]);
    } else if (message.content.toLowerCase().includes(posInsults)) {
      message.reply(INSULTS[Math.floor(Math.random() * INSULTS.length)]);
    } else { 
      message.reply(MENTION[Math.floor(Math.random() * MENTION.length)]);
    }
  }
André
  • 4,417
  • 4
  • 29
  • 56
Melmsie
  • 196
  • 1
  • 1
  • 12

1 Answers1

1

includes expect a substring, not an array.

Refer to String.prototype.includes()

In your program, you are passing an array to the includes instead of a substring. So it will return a falsy value and will go to the last else clause - which prints from the MENTION array.

If you want to check if the message.content contains any of the string from an array, you have to write custom logic for it. You can refer Javascript. Checking if string contains text from an array of substrings

Community
  • 1
  • 1
raghu
  • 388
  • 3
  • 10