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

JS中ListClaims数组存多值时匹配embed作者触发表情功能失效

问题根因

String.prototype.includes()方法仅支持传入单个字符串作为判断参数,你直接传入ListClaims数组时,JavaScript会自动将数组转换为逗号拼接的完整字符串,例如示例中的["rick sanchez","alex","juan"]会被转为"rick sanchez,alex,juan"。只有当embed的author名称恰好包含这一整串拼接结果时才会触发判断,因此仅数组内只有1个元素时逻辑可以正常运行,和你用数组还是txt文件存储名单没有关联。

另外原代码还有两处逻辑缺陷:

  • if(message.embeds.length >= 0)的判断永远成立(数组长度最小为0),无法起到“判断消息是否携带embed”的作用
  • 遇到无author的embed时直接return会终止整个函数执行,导致后续embed不会被处理
修复后代码
// 统一把名单转成小写,避免大小写匹配问题
const ListClaims = ["rick sanchez","alex","juan"].map(item => item.toLowerCase());

// 仅当存在embed时才执行逻辑
if(message.embeds.length > 0) {
  // 遍历所有embed
  for(let i = 0; i < message.embeds.length; i++) {
    const currentEmbed = message.embeds[i];
    // 无author则跳过当前embed,继续处理下一个
    if(!currentEmbed.author?.name) continue;
    
    const authorName = currentEmbed.author.name.toLowerCase();
    // 遍历名单判断是否有匹配项
    const isMatch = ListClaims.some(claim => authorName.includes(claim));
    if(isMatch) {
      // 匹配到就加反应,加catch避免反应添加失败报错
      message.react('🎉').catch(err => console.error('添加反应失败:', err));
      // 如果不需要给多个符合条件的embed重复加反应,可以在这里加break跳出循环
      break;
    }
  }
}
逻辑说明
  • 用Array.prototype.some()遍历名单数组,只要author名称包含任意一个名单内的元素就会返回匹配成功
  • 统一把名单和author名称都转成小写,避免大小写不一致导致匹配失败
  • 把return改为continue,仅跳过无author的embed,不会影响后续embed的处理
  • 增加了错误捕获逻辑,避免机器人没有添加反应权限、消息被删除等异常情况导致代码报错

内容的提问来源于stack exchange,提问作者Andres Avendaño Aguilar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 14:24:04