From 922c6b3ff50eb06bce0240645f233bfa5ceda4e6 Mon Sep 17 00:00:00 2001 From: sliverp Date: Thu, 29 Jan 2026 19:25:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=84=E7=90=86=E9=A2=91=E7=8E=87?= =?UTF-8?q?=E9=99=90=E5=88=B6=E5=B9=B6=E4=BC=98=E5=8C=96=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 10 +++++++--- src/gateway.ts | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 5ecc185..382faa1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "qqbot", - "version": "1.1.0", + "version": "1.2.0", "type": "module", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -11,12 +11,16 @@ "extensions": ["./index.ts"] }, "scripts": { - "build": "tsc", - "dev": "tsc --watch" + "build": "tsc || true", + "dev": "tsc --watch", + "prepack": "npm install --omit=dev" }, "dependencies": { "ws": "^8.18.0" }, + "bundledDependencies": [ + "ws" + ], "devDependencies": { "@types/node": "^20.0.0", "@types/ws": "^8.5.0", diff --git a/src/gateway.ts b/src/gateway.ts index e0611b5..7af7fc9 100644 --- a/src/gateway.ts +++ b/src/gateway.ts @@ -12,6 +12,7 @@ const INTENTS = { // 重连配置 const RECONNECT_DELAYS = [1000, 2000, 5000, 10000, 30000, 60000]; // 递增延迟 +const RATE_LIMIT_DELAY = 60000; // 遇到频率限制时等待 60 秒 const MAX_RECONNECT_ATTEMPTS = 100; const MAX_QUICK_DISCONNECT_COUNT = 3; // 连续快速断开次数阈值 const QUICK_DISCONNECT_THRESHOLD = 5000; // 5秒内断开视为快速断开 @@ -69,13 +70,13 @@ export async function startGateway(ctx: GatewayContext): Promise { return RECONNECT_DELAYS[idx]; }; - const scheduleReconnect = () => { + const scheduleReconnect = (customDelay?: number) => { if (isAborted || reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) { log?.error(`[qqbot:${account.accountId}] Max reconnect attempts reached or aborted`); return; } - const delay = getReconnectDelay(); + const delay = customDelay ?? getReconnectDelay(); reconnectAttempts++; log?.info(`[qqbot:${account.accountId}] Reconnecting in ${delay}ms (attempt ${reconnectAttempts})`); @@ -90,8 +91,8 @@ export async function startGateway(ctx: GatewayContext): Promise { try { cleanup(); - // 刷新 token(可能过期了) - clearTokenCache(); + // 获取 token(使用缓存,除非已过期) + // 注意:不要每次都 clearTokenCache,否则会触发频率限制 const accessToken = await getAccessToken(account.appId, account.clientSecret); const gatewayUrl = await getGatewayUrl(accessToken); @@ -517,8 +518,16 @@ export async function startGateway(ctx: GatewayContext): Promise { }); } catch (err) { + const errMsg = String(err); log?.error(`[qqbot:${account.accountId}] Connection failed: ${err}`); - scheduleReconnect(); + + // 如果是频率限制错误,等待更长时间 + if (errMsg.includes("Too many requests") || errMsg.includes("100001")) { + log?.info(`[qqbot:${account.accountId}] Rate limited, waiting ${RATE_LIMIT_DELAY}ms before retry`); + scheduleReconnect(RATE_LIMIT_DELAY); + } else { + scheduleReconnect(); + } } };