From c893ec6030f6ac372533223cbe9e827d85803313 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: Thu, 14 Nov 2024 10:52:03 +0800 Subject: [PATCH] refactor: apiInit Refactor --- src/core/apis/file.ts | 16 ++++++++-------- src/core/apis/group.ts | 5 +++-- src/core/apis/packet.ts | 5 +++-- src/core/index.ts | 8 +++++++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index a3a34c77..be6a1da3 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -300,18 +300,18 @@ export class NTQQFileApi { element.elementType === ElementType.FILE ) { switch (element.elementType) { - case ElementType.PIC: + case ElementType.PIC: element.picElement!.sourcePath = elementResults[elementIndex]; - break; - case ElementType.VIDEO: + break; + case ElementType.VIDEO: element.videoElement!.filePath = elementResults[elementIndex]; - break; - case ElementType.PTT: + break; + case ElementType.PTT: element.pttElement!.filePath = elementResults[elementIndex]; - break; - case ElementType.FILE: + break; + case ElementType.FILE: element.fileElement!.filePath = elementResults[elementIndex]; - break; + break; } elementIndex++; } diff --git a/src/core/apis/group.ts b/src/core/apis/group.ts index 711b864e..93fb792d 100644 --- a/src/core/apis/group.ts +++ b/src/core/apis/group.ts @@ -25,9 +25,10 @@ export class NTQQGroupApi { constructor(context: InstanceContext, core: NapCatCore) { this.context = context; this.core = core; - this.initCache().then().catch(context.logger.logError.bind(context.logger)); } - + async initApi() { + this.initCache().then().catch(this.context.logger.logError.bind(this.context.logger)); + } async initCache() { this.groups = await this.getGroups(); for (const group of this.groups) { diff --git a/src/core/apis/packet.ts b/src/core/apis/packet.ts index c8c65021..e29dcee9 100644 --- a/src/core/apis/packet.ts +++ b/src/core/apis/packet.ts @@ -26,14 +26,15 @@ export class NTQQPacketApi { this.context = context; this.core = core; this.logger = core.context.logger; - this.InitSendPacket(this.context.basicInfoWrapper.getFullQQVesion()) + } + async initApi() { + await this.InitSendPacket(this.context.basicInfoWrapper.getFullQQVesion()) .then() .catch((err) => { this.logger.logError.bind(this.core.context.logger); this.errStack.push(err); }); } - get available(): boolean { return this.pkt?.available ?? false; } diff --git a/src/core/index.ts b/src/core/index.ts index 1135546a..81ef2947 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -120,7 +120,13 @@ export class NapCatCore { if (!fs.existsSync(this.NapCatTempPath)) { fs.mkdirSync(this.NapCatTempPath, { recursive: true }); } - + //遍历this.apis[i].initApi 如果存在该函数进行async 调用 + for (const apiKey in this.apis) { + const api = this.apis[apiKey as keyof StableNTApiWrapper]; + if ('initApi' in api && typeof api.initApi === 'function') { + await api.initApi(); + } + } this.initNapCatCoreListeners().then().catch(this.context.logger.logError.bind(this.context.logger)); this.context.logger.setFileLogEnabled(