簡體   English   中英

discord.js 響應句子中的特定單詞

[英]discord.js respond to specific words in sentence

所以我正在嘗試向我的不和諧機器人添加一個幫助檢測器,它會自動響應某些東西。 例如,如果有人I want help with login它應該自動響應,但如果有人說I want help它應該什么都不做,所以它只在檢測到句子包含幫助和登錄並且我試圖制作代碼時才響應為此,但它根本沒有響應,這是代碼:

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)
}

我也嘗試使用 .indexOf() 而不是 .includes() 但是當我使用它時,它會響應輸入的每條消息,這不是我想要的。 我在控制台中也沒有收到任何錯誤或任何錯誤。 所以問題是,我將如何讓不和諧機器人響應句子中的特定單詞

您正在嘗試檢查整個陣列。 該函數會自動將其轉換為字符串,這意味着您實際上是在執行以下操作:

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

我認為您想要的是檢查消息是否包含數組中的任何內容。 你可以使用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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM