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

Discord.js V14按钮交互报错DiscordAPIError[40060]求助

解决Discord.js 14.6.0中按钮交互出现DiscordAPIError[40060]的问题

问题场景

使用Discord.js 14.6.0和Node.js 18.11.0开发Discord机器人时,所有按钮交互均触发DiscordAPIError[40060]: Interaction has already been acknowledged.错误。即使使用极简的按钮回复代码(如下),机器人完成回复后仍会出现相同错误,功能有时正常、有时错误提前触发。

极简按钮回复代码:

client.on(Events.InteractionCreate, async(interaction) => {
    if(!interaction.isButton()) return;
    if(interaction.customId === "test")
    await interaction.reply({
      content: "Test worked",
      ephemeral: true
    });
  });

报错详情:

DiscordAPIError[40060]: Interaction has already been acknowledged.
    at SequentialHandler.runRequest (C:\Users\ryuuo\Desktop\ryuu\node_modules\@discordjs\rest\dist\index.js:659:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async SequentialHandler.queueRequest (C:\Users\ryuuo\Desktop\ryuu\node_modules\@discordjs\rest\dist\index.js:458:14)
    at async REST.request (C:\Users\ryuuo\Desktop\ryuu\node_modules\@discordjs\rest\dist\index.js:902:22)
    at async ButtonInteraction.deferReply (C:\Users\ryuuo\Desktop\ryuu\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:69:5)        
    at async Object.execute (C:\Users\ryuuo\Desktop\ryuu\Buttons\languague.js:11:5) {
  requestBody: { files: undefined, json: { type: 5, data: { flags: 64 } } },
  rawError: {
    message: 'Interaction has already been acknowledged.',
    code: 40060
  },
  code: 40060,
  status: 400,
  method: 'POST',
  url: 'https://discord.com/api/v10/interactions/1037980437043236884/aW50ZXJhY3Rpb246MTAzNzk4MDQzNzA0MzIzNjg4NDpteWUxWkVidG8xY1VsUkpOU3Jxb3JJSnp2b2JwSlFtUUVqVnFEb0FNeEg4UUZqc1JNMWdyMkYxU3JNMDdPR2RDMEV2U3Q0TzByMkMzY1lQTk1kS09SVHh5SjEwR1daOHF6OVhWbEh0bDJYd1B6UFpIYXdMYktwczF3RXRxNVFFTA/callback'
}

解决方案

1. 排查重复的InteractionCreate事件监听

  • 问题核心:如果代码中多次注册client.on(Events.InteractionCreate)回调(比如主入口文件和按钮处理器文件各自监听),会导致同一个交互被多次处理。第一次回复已完成交互ack,第二次处理时就会触发40060错误。
  • 修复:全局只保留一处InteractionCreate事件监听,在该回调中统一分发不同customId的按钮交互到对应处理器,避免重复监听。

2. 检查按钮处理器中的重复响应

  • 从报错栈可见,错误来自ButtonInteraction.deferReply调用,说明languague.js第11行的deferReply操作,之前已有代码对该交互执行了ack(比如reply、deferReply等)。
  • 修复:遍历所有按钮处理逻辑,确保每个交互仅调用一次reply/deferReply/update/deferUpdate中的一个响应方法,禁止重复触发。

3. 规范异步代码的错误与流程控制

  • 问题:异步代码未正确使用await或未捕获异常,可能导致后续代码继续执行,重复响应交互。
  • 修复:在InteractionCreate回调中添加try/catch块,确保异步操作的错误被捕获,同时避免未控制的代码分支重复响应:
client.on(Events.InteractionCreate, async (interaction) => {
  if (!interaction.isButton()) return;
  try {
    if (interaction.customId === "test") {
      await interaction.reply({ content: "Test worked", ephemeral: true });
    } else if (interaction.customId === "language") {
      // 确保只调用一次响应方法
      await interaction.deferReply({ ephemeral: true });
      // 后续业务逻辑...
      await interaction.editReply("语言设置已更新");
    }
  } catch (error) {
    console.error("交互处理失败:", error);
    // 若交互未被ack,补充错误提示
    if (!interaction.replied && !interaction.deferred) {
      await interaction.reply({ content: "操作失败,请重试", ephemeral: true });
    }
  }
});

4. 验证Discord.js版本兼容性

  • 部分版本的Discord.js可能存在交互响应的bug,尝试升级到最新14.x稳定版,或降级到已知稳定版本(如14.5.0):
# 升级到最新稳定版
npm install discord.js@latest

5. 遵守交互响应时间限制

  • Discord要求交互必须在3秒内完成ack(回复或延迟回复),若业务逻辑耗时超过3秒,必须先调用deferReply,后续再用editReply更新内容,避免超时后重复触发响应逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 14:20:27