From ad95327b1b85f42e172c9ccc4354f65bcd69259d Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Sat, 23 Sep 2023 20:13:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20bingtoken=E5=9B=BD=E4=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/chat.js | 61 ++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/apps/chat.js b/apps/chat.js index 2b92f922..ebeca335 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -1716,39 +1716,40 @@ export class chatgpt extends plugin { } } } else - if (message && typeof message === 'string' && message.indexOf('限流') > -1) { - throttledTokens.push(bingToken) - let bingTokens = JSON.parse(await redis.get('CHATGPT:BING_TOKENS')) - const badBingToken = bingTokens.findIndex(element => element.Token === bingToken) - const now = new Date() - const hours = now.getHours() - now.setHours(hours + 6) - bingTokens[badBingToken].State = '受限' - bingTokens[badBingToken].DisactivationTime = now - await redis.set('CHATGPT:BING_TOKENS', JSON.stringify(bingTokens)) + if (message && typeof message === 'string' && message.indexOf('限流') > -1) { + throttledTokens.push(bingToken) + let bingTokens = JSON.parse(await redis.get('CHATGPT:BING_TOKENS')) + const badBingToken = bingTokens.findIndex(element => element.Token === bingToken) + const now = new Date() + const hours = now.getHours() + now.setHours(hours + 6) + bingTokens[badBingToken].State = '受限' + bingTokens[badBingToken].DisactivationTime = now + await redis.set('CHATGPT:BING_TOKENS', JSON.stringify(bingTokens)) // 不减次数 - } else if (message && typeof message === 'string' && message.indexOf('UnauthorizedRequest') > -1) { + } else if (message && typeof message === 'string' && message.indexOf('UnauthorizedRequest') > -1) { // token过期了 - // let bingTokens = JSON.parse(await redis.get('CHATGPT:BING_TOKENS')) - // const badBingToken = bingTokens.findIndex(element => element.Token === bingToken) - // // 可能是微软抽风,给三次机会 - // if (bingTokens[badBingToken].exception) { - // if (bingTokens[badBingToken].exception <= 3) { - // bingTokens[badBingToken].exception += 1 - // } else { - // bingTokens[badBingToken].exception = 0 - // bingTokens[badBingToken].State = '过期' - // } - // } else { - // bingTokens[badBingToken].exception = 1 - // } - // await redis.set('CHATGPT:BING_TOKENS', JSON.stringify(bingTokens)) - logger.warn(`token${bingToken}疑似不存在或已过期,再试试`) - retry = retry - 1 + let bingTokens = JSON.parse(await redis.get('CHATGPT:BING_TOKENS')) + const badBingToken = bingTokens.findIndex(element => element.Token === bingToken) + // 可能是微软抽风,给三次机会 + if (bingTokens[badBingToken].exception) { + if (bingTokens[badBingToken].exception <= 3) { + bingTokens[badBingToken].exception += 1 + } else { + bingTokens[badBingToken].exception = 0 + bingTokens[badBingToken].State = '过期' + } } else { - retry-- - errorMessage = message === 'Timed out waiting for response. Try enabling debug mode to see more information.' ? (reply ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑瓜不好使了,不知道怎么回答!') : message + bingTokens[badBingToken].exception = 1 } + await redis.set('CHATGPT:BING_TOKENS', JSON.stringify(bingTokens)) + errorMessage = 'UnauthorizedRequest:必应token不正确或已过期' + // logger.warn(`token${bingToken}疑似不存在或已过期,再试试`) + // retry = retry - 1 + } else { + retry-- + errorMessage = message === 'Timed out waiting for response. Try enabling debug mode to see more information.' ? (reply ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑瓜不好使了,不知道怎么回答!') : message + } } } while (retry > 0) if (errorMessage) { @@ -1766,7 +1767,7 @@ export class chatgpt extends plugin { } } else { return { - text: response.response, + text: response?.response, quote: response.quote, suggestedResponses: response.suggestedResponses, conversationId: response.conversationId,