代码无法扫描数组内词条,仅能识别引号内字符串匹配embed内容的问题
问题原因
你当前的逻辑仅固定校验单个写死在引号内的字符串,没有遍历你定义的词条数组做批量匹配,所以数组内的词条不会被识别。
修复步骤
- 把你需要扫描的所有词条统一放到一个数组内:
// 自定义需要匹配的所有词条 const targetKeywords = [ "Enjoy the videos and music you love on YouTube.", // 剩余要扫描的词条依次写入即可 ];
- 替换原有匹配逻辑,先做非空校验避免空embed报错,再遍历数组做匹配:
// 先判断embed和description存在,避免空值报错 if (message.embeds?.[0]?.description) { const descContent = message.embeds[0].description; // 只要匹配到数组中任意一个词条就返回true const hasMatched = targetKeywords.some(keyword => descContent.includes(keyword)); if (hasMatched) { // 此处写匹配成功后的业务逻辑 } }
补充优化说明
- 要是需要实现大小写不敏感的匹配,可以将内容统一转小写后再校验:
const hasMatched = targetKeywords.some(keyword => descContent.toLowerCase().includes(keyword.toLowerCase()));
- 要是匹配经常因为多余空格、特殊字符失败,可以先对
descContent做预处理,比如去除多余换行、全角符号等之后再做匹配。
内容的提问来源于stack exchange,提问作者wqtermelcn
相关产品推荐
相关产品推荐

