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

如何让JS编写的Discord计数机器人对正确消息添加✅反应

实现Discord计数机器人回复✅表情的方法

要实现正确计数时回复✅表情,只需在正确计数的逻辑分支里添加消息反应代码即可。另外你的代码存在几处未定义变量的问题,以下是修正并添加表情功能后的完整代码:

let count = 1
let timeout
let lastMsgAuthor = null // 初始化上一条消息作者ID

client.on('messageCreate', (message) => {
  const { channel, content, member } = message; // 解构所需属性
  // 仅在指定计数频道执行逻辑
  if (channel.id === channelid) {
    if (member.user.bot) return

    // 检查是否为同一用户连续发送消息
    if (lastMsgAuthor && message.author.id === lastMsgAuthor) {
      message.delete().catch(console.error);
      return;
    }

    if (Number(content) === count + 1) {
      // 对正确计数的消息添加✅表情反应
      message.react('✅').catch(console.error);
      
      count++     
      if (timeout) clearTimeout(timeout)

      timeout = setTimeout(
        () => channel.send(++count).catch(console.error),
        30000
      );
      lastMsgAuthor = message.author.id; // 更新上一条消息作者记录
    } else if (member.id !== client.user.id) {
      channel.send(`${member} messed up!`).catch(console.error);
      count = 0;
      if (timeout) clearTimeout(timeout);
      lastMsgAuthor = null; // 重置上一条作者记录
    }
  }
});

关键修改说明:

  • 调整messageCreate事件参数,直接传入message对象,方便调用react和delete方法
  • 初始化lastMsgAuthor变量,修复原代码未定义的问题
  • 在正确计数分支中添加message.react('✅'),实现表情回复,同时用catch处理可能的错误
  • 调整连续发消息的判断逻辑顺序,确保先拦截重复发送再处理计数
  • 计数出错时重置lastMsgAuthor,避免影响后续正常计数流程

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 15:40:27