You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

内容审核场景下数组值匹配问题求助:无法检测消息含数组元素

解决内容审核中的词库匹配问题

我明白你在做内容审核开发时遇到的困扰——你的代码里尝试用includes来检查消息拆分后的词是否匹配黑名单数组,但这显然行不通,因为includes只能判断数组是否包含单个特定值,没法直接对比两个数组的交集。

咱们来修正这个问题,核心思路是检查两个数组是否存在交集,用some()方法就非常合适,它会在找到第一个匹配项时就停止遍历,性能也不错。

基础版修正代码

假设lang.blacklisted本身就是一个数组(比如["bad", "worse"]),直接用下面的代码:

let words = message.content.split(' ');
// 检查消息中的词是否有任意一个在黑名单里
const hasBlacklistedWord = words.some(word => lang.blacklisted.includes(word));
if (hasBlacklistedWord) {
  message.channel.send('word');
}

进阶版:忽略大小写匹配

如果需要忽略大小写(比如黑名单是"BadWord",但消息里是"badword"也需要触发检测),可以统一转成小写再对比:

let words = message.content.split(' ');
const hasBlacklistedWord = words.some(word => 
  lang.blacklisted.some(blacklistedWord => 
    blacklistedWord.toLowerCase() === word.toLowerCase()
  )
);
if (hasBlacklistedWord) {
  message.channel.send('word');
}

特殊情况:如果lang.blacklisted是Map/对象

如果lang.blacklisted是一个Map或者键值对对象,那你需要先把它的值转成数组:

// 如果是Map
const blacklistedArray = Array.from(lang.blacklisted.values());
// 如果是普通对象
const blacklistedArray = Object.values(lang.blacklisted);

let words = message.content.split(' ');
const hasBlacklistedWord = words.some(word => blacklistedArray.includes(word));
if (hasBlacklistedWord) {
  message.channel.send('word');
}

这样就能正确判断消息中是否包含黑名单里的任一词汇了。

内容的提问来源于stack exchange,提问作者Spyynz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 23:27:35