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

discord.js按钮交互报错:Interaction has already been acknowledged

问题原因分析

你遇到的「Interaction has already been acknowledged」错误,核心问题出在消息组件收集器的作用范围错误:
每次执行$test命令时,你创建的是频道级收集器(message.channel.createMessageComponentCollector()),它会监听整个频道内所有组件交互。当你第二次点击新Embed的按钮时,之前创建的旧收集器也会捕获这个交互并尝试处理,导致同一个交互被多次应答,触发错误。

解决方案

不需要用.reply()替代.update()——.update()是更新当前Embed的正确方式,你只需要调整收集器的作用范围,让它只监听当前发送的Embed按钮即可:

修改后的代码如下:

client.on("messageCreate", (message) => {
  const command = message.content.trim();
  if (command === "$test") {
    const button = new ActionRowBuilder()
      .addComponents(
        new ButtonBuilder()
          .setCustomId("button1")
          .setLabel("Click me.")
          .setStyle(ButtonStyle.Secondary)
      );

    const embed = new EmbedBuilder()
      .setColor("#5A5A5A")
      .setTitle("Embed")
      .setDescription("Did you receive this message? Click button if you did.");

    const embed2 = new EmbedBuilder()
      .setColor("#5A5A5A")
      .setTitle("Confirmation Embed")
      .setDescription("Thanks for confirming!");

    // 保存发送的消息对象,基于它创建收集器
    message.channel.send({ embeds: [embed], components: [button] })
      .then(sentMessage => {
        // 仅监听当前发送的消息的组件,添加过滤条件和超时时间
        const collector = sentMessage.createMessageComponentCollector({
          filter: i => i.customId === "button1",
          time: 60000 // 可选:60秒后自动停止收集器,避免内存泄漏
        });

        collector.on("collect", (i) => {
          i.update({ embeds: [embed2], components: [] });
          collector.stop(); // 处理完成后手动停止收集器
        });
      });
  }
});
关键修改点说明
  1. 缩小收集器范围:使用sentMessage.createMessageComponentCollector()替代频道级收集器,确保收集器只监听刚发送的这条Embed的按钮。
  2. 添加过滤条件:通过filter参数只响应customId为button1的交互,避免误处理其他组件。
  3. 自动/手动停止收集器:设置time超时自动停止,或者在处理完交互后手动调用collector.stop(),防止无效监听占用资源。
关于.reply()的疑问

你不需要替换.update()——.update()的作用是修改触发交互的原消息(也就是你的Embed),完全符合你的需求。你看到的帖子提到的场景,是针对通过.reply()发送的消息需要后续编辑的情况,和你的问题场景不匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 19:06:31