mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-15 17:30:13 +08:00
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>
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user