From cdc4275f81606960db8a54bb520872a579f91d20 Mon Sep 17 00:00:00 2001 From: Alen Date: Sun, 25 Aug 2024 22:23:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=9A=E4=B8=AA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复group_increase事件上报 修复启动时加载群员信息失败 修复文件发送失败 --- src/common/utils/file.ts | 23 ++++++++++++++--------- src/common/utils/helper.ts | 10 +++++----- src/core/apis/group.ts | 23 ++++------------------- src/onebot/api/group.ts | 2 +- src/onebot/helper/event.ts | 2 +- 5 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index 31d494ae..f90e037f 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -187,22 +187,27 @@ export enum FileUriType { export async function checkUriType(Uri: string) { - const LocalFileRet = await solveProblem((Uri) => { if (fs.existsSync(Uri)) return { Uri: Uri, Type: FileUriType.Local }; }); + const LocalFileRet = await solveProblem((uri: string) => { + if (fs.existsSync(uri)) { + return { Uri: uri, Type: FileUriType.Local }; + } + return undefined; + }, Uri); if (LocalFileRet) return LocalFileRet; - const OtherFileRet = await solveProblem((Uri) => { + const OtherFileRet = await solveProblem((uri: string) => { //再判断是否是Http - if (Uri.startsWith('http://') || Uri.startsWith('https://')) { - return { Uri: Uri, Type: FileUriType.Remote }; + if (uri.startsWith('http://') || uri.startsWith('https://')) { + return { Uri: uri, Type: FileUriType.Remote }; } //再判断是否是Base64 - if (Uri.startsWith('base64://')) { - return { Uri: Uri, Type: FileUriType.Base64 }; + if (uri.startsWith('base64://')) { + return { Uri: uri, Type: FileUriType.Base64 }; } - if (Uri.startsWith('file://')) { + if (uri.startsWith('file://')) { let filePath: string; // await fs.copyFile(url.pathname, filePath); - const pathname = decodeURIComponent(new URL(Uri).pathname); + const pathname = decodeURIComponent(new URL(uri).pathname); if (process.platform === 'win32') { filePath = pathname.slice(1); } else { @@ -210,7 +215,7 @@ export async function checkUriType(Uri: string) { } return { Uri: filePath, Type: FileUriType.Local }; } - }); + }, Uri); if (OtherFileRet) return OtherFileRet; return { Uri: Uri, Type: FileUriType.Unknown }; diff --git a/src/common/utils/helper.ts b/src/common/utils/helper.ts index dd3720f9..fd718908 100644 --- a/src/common/utils/helper.ts +++ b/src/common/utils/helper.ts @@ -3,10 +3,10 @@ import fs from 'fs'; import os from 'node:os'; import { QQLevel } from '@/core'; -export async function solveProblem any>(func: T): Promise | undefined> { - return new Promise | undefined>(async (resolve) => { +export async function solveProblem any>(func: T, ...args: Parameters): Promise | undefined> { + return new Promise | undefined>((resolve) => { try { - const result = func(); + const result = func(...args); resolve(result); } catch (e) { resolve(undefined); @@ -14,10 +14,10 @@ export async function solveProblem any>(func: T): P }); } -export async function solveAsyncProblem Promise>(func: T): Promise> | undefined> { +export async function solveAsyncProblem Promise>(func: T, ...args: Parameters): Promise> | undefined> { return new Promise> | undefined>(async (resolve) => { try { - const result = await func(); + const result = await func(...args); resolve(result); } catch (e) { resolve(undefined); diff --git a/src/core/apis/group.ts b/src/core/apis/group.ts index da3c1f1d..378373fc 100644 --- a/src/core/apis/group.ts +++ b/src/core/apis/group.ts @@ -14,7 +14,7 @@ import { NodeIKernelGroupListener, NodeIKernelGroupService, } from '@/core'; -import { isNumeric, runAllWithTimeout } from '@/common/utils/helper'; +import { isNumeric, runAllWithTimeout, sleep } from '@/common/utils/helper'; export class NTQQGroupApi { context: InstanceContext; @@ -25,7 +25,9 @@ export class NTQQGroupApi { constructor(context: InstanceContext, core: NapCatCore) { this.context = context; this.core = core; - this.initCache().then().catch(context.logger.logError); + sleep(1000).then(() => { + this.initCache().then().catch(context.logger.logError); + }); } async initCache() { this.groups = await this.getGroups(); @@ -269,10 +271,7 @@ export class NTQQGroupApi { } async getGroupMemberV2(GroupCode: string, uid: string, forced = false) { - //type ListenerType = NodeIKernelGroupListener['onMemberInfoChange']; type EventType = NodeIKernelGroupService['getMemberInfo']; - // NTEventDispatch.CreatListenerFunction('NodeIKernelGroupListener/onGroupMemberInfoUpdate', - //return napCatCore.session.getGroupService().getMemberInfo(GroupCode, [uid], forced); const Listener = this.core.eventWrapper.RegisterListen<(params: any) => void> ( 'NodeIKernelGroupListener/onMemberInfoChange', @@ -294,20 +293,6 @@ export class NTQQGroupApi { member = members.get(uid); } return member; - - // 原本的方法: (no_cache 下效率很高, cache 下效率一致) - // const [, , , _members] = await this.core.eventWrapper.CallNormalEvent - // ( - // 'NodeIKernelGroupService/getMemberInfo', - // 'NodeIKernelGroupListener/onMemberInfoChange', - // 1, - // 5000, - // (groupCode: string, changeType: number, members: Map) => { - // return groupCode == GroupCode && members.has(uid); - // }, - // GroupCode, [uid], forced, - // ); - // return _members.get(uid); } async getGroupMembers(groupQQ: string, num = 3000): Promise> { diff --git a/src/onebot/api/group.ts b/src/onebot/api/group.ts index e3a02571..b16883ce 100644 --- a/src/onebot/api/group.ts +++ b/src/onebot/api/group.ts @@ -75,7 +75,7 @@ export class OneBotGroupApi { const NTQQGroupApi = this.coreContext.apis.GroupApi; const groupElement = grayTipElement?.groupElement; if (!groupElement) return undefined; - const member = await NTQQGroupApi.getGroupMember(GroupCode, groupElement.memberUid); + const member = await NTQQGroupApi.getGroupMemberV2(GroupCode, groupElement.memberUid); const memberUin = member?.uin; const adminMember = await NTQQGroupApi.getGroupMember(GroupCode, groupElement.adminUid); if (memberUin) { diff --git a/src/onebot/helper/event.ts b/src/onebot/helper/event.ts index 730b21b3..8589e8e4 100644 --- a/src/onebot/helper/event.ts +++ b/src/onebot/helper/event.ts @@ -94,7 +94,7 @@ export async function NT2GroupEvent(core: NapCatCore, obContext: NapCatOneBot11A if (emojiLikeEvent) return emojiLikeEvent; } if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_XMLMSG) { - const GroupIncreaseEvent = await obContext.apiContext.GroupApi.parseGroupMemberIncreaseEvent(msg.peerUid, element.grayTipElement); + const GroupIncreaseEvent = await obContext.apiContext.GroupApi.parseGroupIncreaseEvent(msg.peerUid, element.grayTipElement); if (GroupIncreaseEvent) return GroupIncreaseEvent; }