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

如何解决Nextcord的ApplicationInvokeError及Unknown interaction错误?

排查Nextcord Discord机器人的Unknown Interaction错误

我在用VSCode搭配Nextcord开发Python Discord机器人,运行一段时间后抛出如下错误:

Ignoring exception in command <nextcord.application_command.SlashApplicationCommand object at 0x000002379C4C7730>:
Traceback (most recent call last):
  File "C:\Users\jonat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\nextcord\application_command.py", line 863, in invoke_callback_with_hooks
    await self(interaction, *args, **kwargs)
  File "C:\Users\jonat\OneDrive\Documents\DiscordBot\main.py", line 53, in arem
    await interaction.response.send_message("||**Cal Jordan:** Instant message from... AREM!!||", files=[nextcord.File('AREM.mp3')])
  File "C:\Users\jonat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\nextcord\interactions.py", line 895, in send_message
    await adapter.create_interaction_response(
  File "C:\Users\jonat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\nextcord\webhook\async_.py", line 204, in request
    raise NotFound(response, data)
nextcord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction

The above exception was the direct cause of the following exception:

nextcord.errors.ApplicationInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction

错误原因及解决办法

  • 核心原因:Discord要求交互响应必须在3秒内完成,发送带文件的消息时,文件读取、网络传输等操作可能超时,导致Discord判定交互已失效,后续响应就会返回Unknown Interaction错误。
  • 具体修复步骤:
    1. 先发送延迟响应:在执行任何耗时操作前,调用await interaction.response.defer(),告知Discord将延迟回复,这会把响应时限延长至15分钟。
    2. 用followup发送内容:延迟响应后,使用await interaction.followup.send()替代interaction.response.send_message()来发送带文件的消息。
    3. 提前加载文件:如果AREM.mp3文件较大,建议在机器人启动时就将文件加载到内存,避免每次命令执行时重复读取磁盘,减少耗时:
      import io
      from nextcord import File
      
      # 机器人启动时全局加载文件
      arem_file = None
      with open('AREM.mp3', 'rb') as f:
          arem_file = io.BytesIO(f.read())
      arem_file.seek(0)  # 重置文件指针到开头
      
      # 命令函数中使用
      async def arem(interaction):
          await interaction.response.defer()
          await interaction.followup.send("||**Cal Jordan:** Instant message from... AREM!!||", files=[File(arem_file, filename='AREM.mp3')])
      
    4. 检查网络稳定性:确保机器人所在服务器网络稳定,避免因网络波动导致响应超时。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 14:50:38