关键字消息删除事件未触发

时间:2019-11-17 18:15:22

标签: javascript node.js discord discord.js

我正在尝试编写一个事件,该事件将删除带有特定关键字的任何消息,但是它什么也没做。

client.on("message", (message) => {
  if (message.author.bot) return;
  if (message.member.roles.some(r => ["DEAN!"].includes(r.name))) return;
  let words = ["crack", "hack", "hacked", "patch", "patched", "crackd"]
  if (message.content.includes(words)) {
    message.channel.send(`${message.author} You cannot say such words!`);
    message.delete();
    console.log(chalk.bgYellow("INFO") + (` message containing restricted words by ${message.author} was deleted`));
  }
});

1 个答案:

答案 0 :(得分:0)

Array.prototype.includes方法不接受数组作为参数。此方法应在具有值的数组上调用,并作为第一个参数-您想知道它是否存在于数组中的值。

要识别包含被禁止的关键词的邮件,您应该检查该邮件中的所有单词以查找关键字。像这样:

function isMessageContainBannedWord(msg) {
  const bannedWords = [ 'banana', 'waterlemon' ]
  const words = msg.split(' ')

  return words.some((word) => bannedWords.includes(word))
}

console.log(isMessageContainBannedWord('I like apples'))
console.log(isMessageContainBannedWord('I want you to feel my banana power!'))