You need to enable JavaScript to run this app. Discord.JS调用MessageReaction报animated未定义错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Discord.JS调用MessageReaction报animated未定义错误如何解决

问题根因

  • 报错message.react() is not a function:channel.send()是异步方法,返回值是Promise<Message>,你没有等待Promise执行完成就直接调用方法,拿到的thing是Promise对象而非Discord消息实例,自然不存在react方法。
  • 报错Cannot read property 'animated' of undefined:MessageReaction构造函数的第二个参数要求传入Discord API返回的标准反应数据对象,你直接传入表情字符串不符合参数要求,且正常场景下完全不需要手动实例化MessageReaction,直接调用消息实例的react()方法即可实现添加反应。
  • 额外逻辑问题:你代码中用逗号运算符写的判断条件全部失效,比如if(args[1] === 'yes', waitingonYes === true)只会判断最后一个表达式的值,前面的条件直接被忽略;用setInterval轮询等待用户确认的逻辑完全错误,异步流程执行顺序不对,你写的确认逻辑根本不会生效,发送投票的代码会直接执行,不会等用户发yes确认。

修正后参考代码

// 提前定义可用的数字表情,避免拼写错误
const numberEmojis = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣']
// 全局存储的投票数据,你之前定义的polls、pollMsgs自己保留
bot.on('message', async message => {
  if (message.channel.type === 'dm') return
  if (!message.content.startsWith(PREFIX)) return
  let args = message.content.substring(PREFIX.length).split(' ')
  switch (args[0]) {
    case 'poll':
      // 参数不足判断
      if (args.length <= 3 && args[1] !== 'yes' && args[1] !== 'end' && args[1] !== 'cancel') {
        return message.reply('缺少参数!命令格式:\poll <标题> <选项1> <选项2> [可选选项3-9]')
      }
      // 结束投票逻辑
      if (args[1] === 'end') {
        const pollId = args[2]
        if (polls.includes(pollId) && pollMsgs.includes(pollId)) {
          polls.splice(polls.indexOf(pollId), 1)
          pollMsgs.splice(pollMsgs.indexOf(pollId), 1)
          return message.channel.send(`已结束编号为${pollId}的投票`)
        } else {
          return message.reply('该投票不存在!')
        }
      }
      // 创建投票的主逻辑
      const title = args[1]
      // 过滤掉空的选项
      const options = args.slice(2).filter(opt => opt)
      if (options.length < 2) return message.reply('投票至少需要2个选项')
      if (options.length > 9) return message.reply('投票最多支持9个选项')
      
      // 生成预览embed
      const previewEmbed = new Discord.MessageEmbed()
        .setTitle('投票预览')
        .addField('投票标题', title)
      options.forEach((opt, idx) => {
        previewEmbed.addField(`选项 ${numberEmojis[idx]}`, opt, true)
      })
      await message.channel.send(previewEmbed)
      const confirmMsg = await message.channel.send(`请在30秒内发送 \`${PREFIX}poll yes\` 确认创建投票,发送 \`${PREFIX}poll cancel\` 取消`)
      
      // 创建消息收集器等待用户确认,不用轮询
      const filter = msg => msg.author.id === message.author.id && 
        (msg.content === `${PREFIX}poll yes` || msg.content === `${PREFIX}poll cancel`)
      const collected = await message.channel.awaitMessages(filter, { max: 1, time: 30000, errors: ['time'] })
        .catch(() => {
          confirmMsg.edit('确认超时,投票已取消')
          return null
        })
      if (!collected) return
      const reply = collected.first()
      if (reply.content === `${PREFIX}poll cancel`) {
        return message.channel.send('已取消创建投票')
      }
      
      // 确认后发送投票到指定频道,加await拿到消息实例
      const pollChannel = message.guild.channels.cache.get('858821179192377345')
      const pollEmbed = new Discord.MessageEmbed()
        .setTitle(title)
      options.forEach((opt, idx) => {
        pollEmbed.addField(`选项 ${numberEmojis[idx]}`, opt, true)
      })
      const pollMsg = await pollChannel.send(pollEmbed)
      // 批量添加对应表情
      for (let i = 0; i < options.length; i++) {
        await pollMsg.react(numberEmojis[i])
      }
      // 你自己要存的投票数据自己加逻辑存
      message.channel.send('投票已成功创建')
      console.log('投票创建完成')
      break
  }
})

注意事项

  • 所有异步方法(send、react、awaitMessages)都需要加await,且外层函数要标记async才能正常使用
  • 不要手动实例化MessageReaction,直接调用消息实例的react方法传入表情字符即可实现添加反应
  • 判断多个条件要用&&/||逻辑运算符,不要用逗号运算符,会导致前序条件失效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 11:00:14