如何让我的discord.js聊天机器人回复一个句子中的单词



我试图建立一个聊天回复机器人帮助管理一个游戏不和谐服务器。我有一个单词列表让它回复,可以让他回复他们没有问题。我遇到的问题是,其中一个词是ESP,所以当有人输入单词response时,它会在单词response中选择ESP,并在我不想让他回复的时候回复。另一个例子是工作黑客。当有人在聊天中输入钢锯岭时,他会回复到。我希望它只查找单词esp和其他一些作弊术语,而不是在较长的单词中查找它。

let cheater = ["cheating", "cheater", "aim_bot", 'esp', "hack"]
client.on('messageCreate', (message) => {
if (message.author.bot) return false;
if (cheater.some(w => message.content.toLowerCase().includes(w))) {
message.reply('if you think someone is cheating report it here <#976517390701563955>');
}
})

任何帮助都将非常感激谢谢。

解决这个问题的方法很简单,事实上,如果你想让你的机器人只对完整的单词做出反应,你可以使用:

if(message.content.toLowerCase().includes(` ${your_variable} `)) {
// your code
}

而不是你代码中的

您需要查找Array.prototype.some()以获得array中的message

client.on('messageCreate', async(message) => {
if (message.author.bot) return false;
let messages = ["cheating", "cheater", "aim_bot", 'esp', "hack"]
if(messages.some(element => message.content.toLowerCase().includes(element))) {
message.reply("if you think someone is cheating report it here <#976517390701563955>")
}
})

最新更新