From cd74687b7bccb729a78ba43b6aedf1abe90c1c88 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: Sat, 28 Sep 2024 21:36:54 +0800 Subject: [PATCH] fix: getMsg --- src/native/index.ts | 8 ++++++++ src/onebot/action/msg/GetMsg.ts | 15 ++++++++++----- src/onebot/index.ts | 28 ++++++++++++++-------------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/native/index.ts b/src/native/index.ts index 5c387d84..873b0eae 100644 --- a/src/native/index.ts +++ b/src/native/index.ts @@ -5,6 +5,7 @@ export class Native { platform: string; supportedPlatforms = ['win32']; MoeHooExport: any = { exports: {} }; + recallHookEnabled: boolean = false; constructor(nodePath: string, platform: string = process.platform) { this.platform = platform; if (!this.supportedPlatforms.includes(this.platform)) { @@ -12,4 +13,11 @@ export class Native { } dlopen(this.MoeHooExport, path.join(nodePath, './native/MoeHoo.win32.node'), constants.dlopen.RTLD_LAZY); } + isSetReCallEnabled(): boolean { + return this.recallHookEnabled; + } + registerRecallCallback(callback: (hex: string) => any): void { + this.recallHookEnabled = true; + return this.MoeHooExport.exports.registMsgPush(callback); + } } \ No newline at end of file diff --git a/src/onebot/action/msg/GetMsg.ts b/src/onebot/action/msg/GetMsg.ts index 40f0251e..9b183db8 100644 --- a/src/onebot/action/msg/GetMsg.ts +++ b/src/onebot/action/msg/GetMsg.ts @@ -3,6 +3,7 @@ import BaseAction from '../BaseAction'; import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { MessageUnique } from '@/common/message-unique'; +import { RawMessage } from '@/core'; export type ReturnDataType = OB11Message @@ -32,13 +33,17 @@ class GetMsg extends BaseAction { throw new Error('消息不存在'); } const peer = { guildId: '', peerUid: msgIdWithPeer?.Peer.peerUid, chatType: msgIdWithPeer.Peer.chatType }; - const msg = await this.core.apis.MsgApi.getMsgsByMsgId( - peer, - [msgIdWithPeer?.MsgId || payload.message_id.toString()]); - const retMsg = await this.obContext.apis.MsgApi.parseMessage(msg.msgList[0]); + let orimsg = this.obContext.recallMsgCache.get(msgIdWithPeer.MsgId); + let msg: RawMessage; + if (orimsg) { + msg = orimsg; + } else { + msg = (await this.core.apis.MsgApi.getMsgsByMsgId(peer, [msgIdWithPeer?.MsgId || payload.message_id.toString()])).msgList[0]; + } + const retMsg = await this.obContext.apis.MsgApi.parseMessage(msg); if (!retMsg) throw Error('消息为空'); try { - retMsg.message_id = MessageUnique.createUniqueMsgId(peer, msg.msgList[0].msgId)!; + retMsg.message_id = MessageUnique.createUniqueMsgId(peer, msg.msgId)!; retMsg.message_seq = retMsg.message_id; retMsg.real_id = retMsg.message_id; } catch (e) { diff --git a/src/onebot/index.ts b/src/onebot/index.ts index 1b26fddf..b8cb81e9 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -46,7 +46,6 @@ import { LRUCache } from '@/common/lru-cache'; import { NodeIKernelRecentContactListener } from '@/core/listeners/NodeIKernelRecentContactListener'; import { Native } from '@/native'; import { Message, RecallGroup } from '@/core/proto/Message'; -import { OB11MessageData } from './types'; //OneBot实现类 export class NapCatOneBot11Adapter { @@ -57,8 +56,9 @@ export class NapCatOneBot11Adapter { apis: StableOneBotApiWrapper; networkManager: OB11NetworkManager; actions: ActionMap; - + nativeCore: Native | undefined; private bootTime = Date.now() / 1000; + recallMsgCache = new LRUCache(100); constructor(core: NapCatCore, context: InstanceContext, pathWrapper: NapCatPathWrapper) { this.core = core; @@ -73,14 +73,14 @@ export class NapCatOneBot11Adapter { }; this.actions = createActionMap(this, core); this.networkManager = new OB11NetworkManager(); - this.registerNative(core,context).then().catch(); + this.registerNative(core, context).then().catch(); this.InitOneBot() .catch(e => this.context.logger.logError.bind(this.context.logger)('初始化OneBot失败', e)); } async registerNative(core: NapCatCore, context: InstanceContext) { - let appNative = new Native(context.pathWrapper.binaryPath); - appNative.MoeHooExport.exports.registMsgPush(async (hex: string) => { + this.nativeCore = new Native(context.pathWrapper.binaryPath); + this.nativeCore.registerRecallCallback(async (hex: string) => { try { let data = Message.decode(Buffer.from(hex, 'hex')) as any; //data.MsgHead.BodyInner.MsgType SubType @@ -95,12 +95,13 @@ export class NapCatOneBot11Adapter { let peer: Peer = { chatType: ChatType.KCHATTYPEGROUP, peerUid: uid.toString() }; context.logger.log("[Native] 群消息撤回 Peer: " + uid.toString() + " / MsgSeq:" + seq); let msgs = await core.apis.MsgApi.queryMsgsWithFilterExWithSeq(peer, seq.toString()); - - let ob11 = await this.apis.MsgApi.parseMessage(msgs.msgList[0], 'array') - if (ob11) { - const { sendElements, deleteAfterSentFiles } = await this.apis.MsgApi.createSendElements(ob11.message as OB11MessageData[], peer); - this.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles); - } + this.recallMsgCache.put(msgs.msgList[0].msgId, msgs.msgList[0]); + // let ob11 = await this.apis.MsgApi.parseMessage(msgs.msgList[0], 'array') + // .catch(e => this.context.logger.logError.bind(this.context.logger)('处理消息失败', e)); + // if (ob11) { + // const { sendElements, deleteAfterSentFiles } = await this.apis.MsgApi.createSendElements(ob11.message as OB11MessageData[], peer); + // this.apis.MsgApi.sendMsgWithOb11UniqueId(peer, sendElements, deleteAfterSentFiles); + // } // this.apis.MsgApi.sendMsg(peer, [{ @@ -116,7 +117,7 @@ export class NapCatOneBot11Adapter { // }]); } } catch (error: any) { - context.logger.logWarn("[Native] Error:", (error as Error).message,' HEX:',Buffer.from(hex, 'hex')); + context.logger.logWarn("[Native] Error:", (error as Error).message, ' HEX:', hex); } }); } @@ -569,7 +570,6 @@ export class NapCatOneBot11Adapter { } }).catch(e => this.context.logger.logError.bind(this.context.logger)('constructPrivateEvent error: ', e)); } - private async emitRecallMsg(msgList: RawMessage[], cache: LRUCache) { for (const message of msgList) { // log("message update", message.sendStatus, message.msgId, message.msgSeq) @@ -601,7 +601,7 @@ export class NapCatOneBot11Adapter { parseInt(message.peerUin), parseInt(message.senderUin), parseInt(operatorId), - oriMessageId, + oriMessageId ); this.networkManager.emitEvent(groupRecallEvent) .catch(e => this.context.logger.logError.bind(this.context.logger)('处理群消息撤回失败', e));