-2

I have a code that responds to the user if they type the word, but I need the word in a sentence to answer

eg:

User: Hi friends

BOT:

User: Hi

BOT: The bot responds but when it is in sentence no

const responseObject = {   "hi": "Hello!",
    
};
    
if(responseObject[message.content]) {
    message.channel.send(responseObject[message.content]);   }
PPG tv
  • 98
  • 2
  • 7
  • 2
    Possible duplicate of [How to check whether a string contains a substring in JavaScript?](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript) – JackRed Jul 28 '19 at 14:10

1 Answers1

0

You can use .includes("YOUR_STRING") to check if a string includes another string.

If you want to check if a string includes more than one string, do something like this:

var words = ["insert", "your", "words"]

for (var i=0; i < words.length; i++) {

 if (message.content.includes(words[i])) {

  // YOUR CODE

 }

}
PPG tv
  • 98
  • 2
  • 7
Xge
  • 450
  • 3
  • 14
  • Hi, this work but when a have a string that contain two words(in the array) like this "your words" I will get an error because it found two words, and it can't handle this, any idea how to avoid this? – ynroot Feb 16 '21 at 16:04
  • You could split every string in the words array: `words = words.map(w => w.split(" "))`, then you will have something like this: `["insert", "your", "words", ["your", "words"]]`. Then you can flatten it by using `words.flat()` so it becomes `["insert", "your", "words", "your", "words"]` – Xge Feb 17 '21 at 18:04