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

discord.py开发Discord机器人:slash命令defer/send_message报404错误

Discord.py Slash命令中defer()/send_message()触发404 Unknown Interaction错误

使用Python和discord.py开发Discord机器人时,在slash命令中调用inter.response.send_message()或inter.response.defer()频繁出现“The application did not respond”提示,偶尔能正常运行,报错为discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction。

最初的实现代码

@bot.tree.command(name="check_ban", description="Check ban status of accounts")
async def check_ban(inter: discord.Interaction):
    log(f"[check_ban] Command invoked by user={inter.user.id}")
    if await check_restriction(inter):
        log(f"[check_ban] Aborted: user={inter.user.id} is restricted")
        return

    log(f"[check_ban] Sending inter response")
    await inter.response.send_message("Checking ban status...", ephemeral=True)
    log(f"[check_ban]  inter response sent")
    ...

触发的错误栈

[ERROR] discord.app_commands.tree: Ignoring exception in command 'check_ban'
Traceback (most recent call last):
  File "/home/discordbot/bot/venv/lib/python3.12/site-packages/discord/app_commands/commands.py", line 858, in _do_call
    return await self._callback(interaction, **params)
  File "/home/discordbot/bot/remainm.py", line 511, in check_ban
    await inter.response.send_message("Checking ban status...", ephemeral=True)
  File "/home/discordbot/bot/venv/lib/python3.12/site-packages/discord/interactions.py", line 1004, in send_message
    response = await adapter.create_interaction_response(
  File "/home/discordbot/bot/venv/lib/python3.12/site-packages/discord/webhook/async_.py", line 224, in request
    raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction

对应日志

[1746556901.163286][check_ban] Command invoked by user=7...
[1746556901.1636057][check_ban] Sending inter response
# 无后续日志

改用defer()后的代码

@bot.tree.command(name="check_ban", description="Check ban status of accounts")
async def check_ban(inter: discord.Interaction):
    log(f"[check_ban] Command invoked by user={inter.user.id}")

    log(f"[check_ban] defering")
    await inter.response.defer(ephemeral=True)
    log(f"[check_ban] deferred")
    ...

改用defer()后的日志与异常

日志:

[1746557536.6729193][check_ban] Command invoked by user=7...
[1746557536.673151][check_ban] defering
# 提示应用未响应

捕获的异常:

e=NotFound('404 Not Found (error code: 10062): Unknown interaction')

问题原因与解决方法

  • 核心原因:Discord要求所有交互必须在3秒内给出响应(send_message/defer),如果代码在调用响应方法前存在阻塞操作、耗时逻辑,或者事件循环被占用,就会导致交互超时失效,触发404错误。

  • 具体解决步骤:

    1. 优先发送响应:把inter.response.defer()或send_message()放在命令函数的最开头(除了必要的权限校验,但权限校验必须是异步且耗时极短的),先完成Discord的交互响应要求,再处理后续的耗时逻辑。
    2. 检查阻塞操作:排查check_restriction()或其他前置逻辑是否包含同步IO(比如用requests而不是aiohttp)、大量计算等阻塞代码,全部改为异步实现,或者放到响应发送后执行。
    3. 避免阻塞事件循环:如果后续有耗时任务,用asyncio.create_task()将其丢到后台异步执行,不要阻塞主命令函数的执行流程。
    4. 验证网络状态:测试服务器到Discord API的网络延迟,若延迟过高,考虑更换服务器节点或优化网络配置。
    5. 正确使用followup:如果已经调用了defer(),后续必须用await inter.followup.send()发送结果,禁止重复调用inter.response下的方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 05:19:51