【问题标题】:discord.js respond to specific words in sentencediscord.js 响应句子中的特定单词
【发布时间】:2021-12-18 23:38:10
【问题描述】:

所以我正在尝试向我的不和谐机器人添加一个帮助检测器,它会自动响应某些内容。例如,如果有人输入 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() 但当我使用它时,它会响应每条输入的消息,这不是我想要的。我在控制台中也没有收到任何错误或任何内容。所以问题是,我如何让不和谐机器人响应句子中的特定单词

【问题讨论】:

  • 您是否在控制台中遇到任何错误?还是没有错误?
  • 哦,没有错误,控制台中没有任何内容

标签: javascript discord discord.js


【解决方案1】:

您正在尝试检查 整个 数组。该函数会自动将其转换为字符串,这意味着您实际上正在这样做:

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
}

【讨论】:

  • 哦,非常感谢,工作就像一个魅力!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多