简体   繁体   中英

discord.js respond to specific words in sentence

So I'm trying to add a help detector to my discord bot which auto responds to certain stuff. For example if someone types I want help with login it should auto respond, but if someone says I want help it should do nothing, so it only responds if it detects that the sentence contain help and login and I've tried to make a code for this but it doesn't respond at all, here is the code:

let help_words2 = ["help", "problems", "problem", "error", "issue"]
let help_words = ["psutil", "pyinstaller"]
if (message.content.toLowerCase().includes(help_words) && message.content.toLowerCase().includes(help_words2)) {
    const embed = new MessageEmbed()
    .setAuthor(`I'm here to help ${message.author.tag}!`,'https://img.icons8.com/fluency/48/000000/help.png',`${client.config.links.website}`)
    .setDescription(`**I think you need help with python and before asking more questions make sure to check out these pages/channels**\n\n> <#905804981368664075>\n> <#905224439736705087>`)
    message.channel.send(embed)
}

I've also tried to use .indexOf() instead of .includes() but when I used that it responded to every message that was typed which is not what I want. I don't get any errors either or anything in the console. So question is, how would I make the discord bot respond to specific words in sentance

You are trying to check with the whole array. The function automatically converts it to a string, meaning you are actually doing this:

message.content.toLowerCase().includes("psutil,pyinstaller")

What I think you want is to check if the message includes anything from the array. You can do this using Array.prototype.some :

if (help_words.some(w => message.content.toLowerCase().includes(w)) &&
help_words2.some(w => message.content.toLowerCase().includes(w))) {
 //rest of your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM