From 93904dcb1b1525254f94b3fddaa14dfb7cc5773b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 25 Nov 2024 15:45:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=A0=E9=99=A4=E7=9A=84=E7=A7=BB?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/index.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/onebot/index.ts b/src/onebot/index.ts index 1702c8d71..0eb5bdfcb 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -181,15 +181,20 @@ export class NapCatOneBot11Adapter { const newLog = await this.creatOneBotLog(now); this.context.logger.log(`[Notice] [OneBot11] 配置变更前:\n${prevLog}`); this.context.logger.log(`[Notice] [OneBot11] 配置变更后:\n${newLog}`); - - await this.handleConfigChange(now.network.httpServers, OB11PassiveHttpAdapter); - await this.handleConfigChange(now.network.httpClients, OB11ActiveHttpAdapter); - await this.handleConfigChange(now.network.websocketServers, OB11PassiveWebSocketAdapter); - await this.handleConfigChange(now.network.websocketClients, OB11ActiveWebSocketAdapter); + + await this.handleConfigChange(prev.network.httpServers, now.network.httpServers, OB11PassiveHttpAdapter); + await this.handleConfigChange(prev.network.httpClients, now.network.httpClients, OB11ActiveHttpAdapter); + await this.handleConfigChange(prev.network.websocketServers, now.network.websocketServers, OB11PassiveWebSocketAdapter); + await this.handleConfigChange(prev.network.websocketClients, now.network.websocketClients, OB11ActiveWebSocketAdapter); } - - private async handleConfigChange(adapters: NetworkConfigAdapter[], adapterClass: new (...args: any[]) => IOB11NetworkAdapter): Promise { - for (const adapterConfig of adapters) { + + private async handleConfigChange( + prevConfig: NetworkConfigAdapter[], + nowConfig: NetworkConfigAdapter[], + adapterClass: new (...args: any[]) => IOB11NetworkAdapter + ): Promise { + // 通知新配置重载 删除关闭的 加入新开的 + for (const adapterConfig of nowConfig) { const existingAdapter = this.networkManager.findSomeAdapter(adapterConfig.name); if (existingAdapter) { const networkChange = await existingAdapter.reload(adapterConfig); @@ -201,6 +206,16 @@ export class NapCatOneBot11Adapter { await this.networkManager.registerAdapterAndOpen(newAdapter); } } + // 比较旧的找不到的回收 + for (const adapterConfig of prevConfig) { + const existingAdapter = nowConfig.find((e) => e.name === adapterConfig.name); + if (!existingAdapter) { + const existingAdapter = this.networkManager.findSomeAdapter(adapterConfig.name); + if (existingAdapter) { + await this.networkManager.closeSomeAdaterWhenOpen([existingAdapter]); + } + } + } } private initMsgListener() {