refactor: apiInit Refactor

This commit is contained in:
手瓜一十雪 2024-11-14 10:52:03 +08:00
parent e8bf6fa0a6
commit c893ec6030
4 changed files with 21 additions and 13 deletions

View File

@ -25,9 +25,10 @@ export class NTQQGroupApi {
constructor(context: InstanceContext, core: NapCatCore) { constructor(context: InstanceContext, core: NapCatCore) {
this.context = context; this.context = context;
this.core = core; 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() { async initCache() {
this.groups = await this.getGroups(); this.groups = await this.getGroups();
for (const group of this.groups) { for (const group of this.groups) {

View File

@ -26,14 +26,15 @@ export class NTQQPacketApi {
this.context = context; this.context = context;
this.core = core; this.core = core;
this.logger = core.context.logger; this.logger = core.context.logger;
this.InitSendPacket(this.context.basicInfoWrapper.getFullQQVesion()) }
async initApi() {
await this.InitSendPacket(this.context.basicInfoWrapper.getFullQQVesion())
.then() .then()
.catch((err) => { .catch((err) => {
this.logger.logError.bind(this.core.context.logger); this.logger.logError.bind(this.core.context.logger);
this.errStack.push(err); this.errStack.push(err);
}); });
} }
get available(): boolean { get available(): boolean {
return this.pkt?.available ?? false; return this.pkt?.available ?? false;
} }

View File

@ -120,7 +120,13 @@ export class NapCatCore {
if (!fs.existsSync(this.NapCatTempPath)) { if (!fs.existsSync(this.NapCatTempPath)) {
fs.mkdirSync(this.NapCatTempPath, { recursive: true }); 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.initNapCatCoreListeners().then().catch(this.context.logger.logError.bind(this.context.logger));
this.context.logger.setFileLogEnabled( this.context.logger.setFileLogEnabled(