From 66914115508d4feb4faa5231c4b0e338fb10ab2e Mon Sep 17 00:00:00 2001 From: yuanqiuye Date: Sun, 12 Apr 2026 14:12:07 +0800 Subject: [PATCH] fix(discord): prevent 10062 Unknown interaction error by deferring slash commands immediately (#7474) * fix(discord): prevent 10062 Unknown interaction error by deferring slash commands immediately * fix(discord): early return if deferral fails Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * chore: ruff format --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Soulter <905617992@qq.com> --- .../discord/discord_platform_adapter.py | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/astrbot/core/platform/sources/discord/discord_platform_adapter.py b/astrbot/core/platform/sources/discord/discord_platform_adapter.py index c436117c3..4c0b8eaf9 100644 --- a/astrbot/core/platform/sources/discord/discord_platform_adapter.py +++ b/astrbot/core/platform/sources/discord/discord_platform_adapter.py @@ -436,6 +436,21 @@ class DiscordPlatformAdapter(Platform): async def dynamic_callback( ctx: discord.ApplicationContext, params: str | None = None ) -> None: + # 1. 嘗試立即响应,防止超时 (移到最前面) + followup_webhook = None + try: + # 設定 2.5 秒超時,避免卡死整個 event loop + await asyncio.wait_for(ctx.defer(), timeout=2.5) + followup_webhook = ctx.followup + except asyncio.TimeoutError: + logger.warning( + f"[Discord] Defer command '{cmd_name}' timeout. Network might be too slow." + ) + return + except Exception as e: + logger.warning(f"[Discord] Failed to defer command '{cmd_name}': {e}") + return + # 将平台特定的前缀'/'剥离,以适配通用的CommandFilter logger.debug(f"[Discord] Callback triggered: {cmd_name}") logger.debug(f"[Discord] Callback context: {ctx}") @@ -450,14 +465,6 @@ class DiscordPlatformAdapter(Platform): f"Built command string: '{message_str_for_filter}'", ) - # 尝试立即响应,防止超时 - followup_webhook = None - try: - await ctx.defer() - followup_webhook = ctx.followup - except Exception as e: - logger.warning(f"[Discord] Failed to defer command '{cmd_name}': {e}") - # 2. 构建 AstrBotMessage channel = ctx.channel abm = AstrBotMessage()