javascript - 如何检查消息内容是否包含数组中的任何项目?

标签 javascript arrays discord

我正在制作一个不和谐的机器人,我正在尝试使用数组制作一个禁用词列表。我似乎无法找到如何使这项工作。这是我当前的代码:

if (forbidenWords.includes(message.content)) {
  message.delete();
  console.log(colors.red(`Removed ${message.author.username}'s Message as it had a forbidden word in it.`));
}

如果你不知道,我正在尝试检查用户的消息是否包含数组 forbidenWords 中的任何内容并将其删除。我该怎么做呢?

最佳答案

在“现代” JS 中:

forbiddenWords.some(word => message.content.includes(word))

以注释的逐行格式:
forbiddenWords               // In the list of forbidden words,
  .some(                     // are there some
    word =>                  // words where the 
      message.content        // message content
        .includes(           // includes
          word))             // the word?

关于javascript - 如何检查消息内容是否包含数组中的任何项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41115137/

相关文章:

javascript - 如何在 React 组件中使用 pickr 颜色选择器 vanilla JS 库?

javascript - 从其他数组中删除数组

jquery - 循环遍历json数组Jquery

node.js - 下载youtube播放列表

python - 如何让机器人在 discord.py 重写中为 channel 设置慢速模式?

javascript - ES6 中已有的 getter 方法在 ES6 之前有什么替代方案

javascript - backbone.js 和 el 属性

python - 我的基于 python 的不和谐机器人不响应输入的任何命令,并且初始方法不打印任何内容

javascript - strophe.js PEP 处理程序未正确附加

arrays - 赋值左侧的函数调用必须返回 Variant 或 Object,在两个数组中查找公共(public)值