Discord.py同步命令时触发404 Unknown Interaction错误求助
Discord斜杠命令同步时出现404 Unknown Interaction错误排查
问题描述
运行自定义Discord斜杠命令进行命令同步时,触发了404 Not Found (error code: 10062): Unknown interaction错误,命令代码及错误输出如下:
命令代码
@vot.event @bot.tree.command(name='synx', description='expiremental sync') @app_commands.checks.has_any_role(1024396774955298909, 1249016717032820770, 1309514819245834360) async def sync(interaction: discord.Interaction): await bot.tree.sync() print('Command tree synced expirementaly') await interaction.response.send_message(f'Command tree synced expirementaly, {interaction.user.mention}', ephemeral=True)
错误输出
> 2 commands has been synced succsesfully Command tree synced expirementaly 2024-11-22 22:22:52 ERROR discord.app_commands.tree Ignoring exception in command 'synx' Traceback (most recent call last): File "C:\Users\lazorzneq\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\app_commands\commands.py", line 858, in _do_call return await self._callback(interaction, **params) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\lazorzneq\Documents\Project.Discord\Swift Blink\Welcome.py", line 38, in sync await interaction.response.send_message(f'Command tree synced expirementaly, {interaction.user.mention}', ephemeral=True) File "C:\Users\lazorzneq\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\interactions.py", line 855, in send_message await adapter.create_interaction_response( File "C:\Users\lazorzneq\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\webhook\async_.py", line 221, in request raise NotFound(response, data) discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction The above exception was the direct cause of the following exception: The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\lazorzneq\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\app_commands\tree.py", line 1310, in _call await command._invoke_with_namespace(interaction, namespace) File "C:\Users\lazorzneq\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\app_commands\commands.py", line 883, in _invoke_with_namespace return await self._do_call(interaction, transformed_values) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\lazorzneq\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\app_commands\commands.py", line 876, in _do_call raise CommandInvokeError(self, e) from e discord.app_commands.errors.CommandInvokeError: Command 'synx' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction
错误原因
核心问题是Discord交互响应超时限制:Discord要求所有交互命令必须在3秒内返回响应,而await bot.tree.sync()操作(尤其是全局同步)可能因命令数量多、网络延迟等原因,执行时间超过3秒。当同步完成后再调用interaction.response.send_message()时,Discord已判定该交互会话失效,因此返回404错误。
解决方法
方法一:先发送延迟响应,再编辑结果
先立即发送一个临时响应抢占3秒窗口,等同步完成后再编辑消息告知结果:
@vot.event @bot.tree.command(name='synx', description='expiremental sync') @app_commands.checks.has_any_role(1024396774955298909, 1249016717032820770, 1309514819245834360) async def sync(interaction: discord.Interaction): # 发送延迟响应,告知用户操作进行中 await interaction.response.defer(ephemeral=True) # 执行同步操作 await bot.tree.sync() print('Command tree synced expirementaly') # 编辑响应消息发送最终结果 await interaction.followup.send(f'Command tree synced expirementaly, {interaction.user.mention}', ephemeral=True)
方法二:缩小同步范围
默认bot.tree.sync()是全局同步,耗时较长。可以指定同步到特定服务器,大幅缩短执行时间:
# 将GUILD_ID替换为目标服务器的ID await bot.tree.sync(guild=discord.Object(id=GUILD_ID))
方法三:优化网络环境
确保机器人所在服务器网络稳定,减少与Discord API的通信延迟,避免同步操作耗时过长。
内容的提问来源于stack exchange,提问作者Classical human
相关产品推荐
相关产品推荐

