From 8f85c897c8204be8a8afc7676ce17df2f732b455 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, 12 Sep 2024 09:20:10 +0800 Subject: [PATCH] refactor: SysMsg --- src/onebot/api/msg.ts | 35 +++++++++++++++++++++++++++++++++++ src/onebot/index.ts | 37 ++++--------------------------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index a2033539..a29c1466 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -34,6 +34,7 @@ import { RequestUtil } from '@/common/request'; import fs from 'node:fs'; import fsPromise from 'node:fs/promises'; import { OB11FriendAddNoticeEvent } from '@/onebot/event/notice/OB11FriendAddNoticeEvent'; +import { SysMessage, SysMessageType } from '@/core/proto/ProfileLike'; type RawToOb11Converters = { [Key in keyof MessageElement as Key extends `${string}Element` ? Key : never]: ( @@ -818,4 +819,38 @@ export class OneBotMsgApi { return { path, fileName: inputdata.name ?? fileName }; } + async parseSysMessage(msg: number[]) { + const sysMsg = SysMessage.decode(Uint8Array.from(msg)) as unknown as SysMessageType; + if (sysMsg.msgSpec.length === 0) { + return; + } + const { msgType, subType, subSubType } = sysMsg.msgSpec[0]; + if (msgType === 528 && subType === 39 && subSubType === 39) { + if (!sysMsg.bodyWrapper) return; + let event = await this.obContext.apis.UserApi.parseLikeEvent(sysMsg.bodyWrapper.wrappedBody); + return event; + }; + /* + if (msgType === 732 && subType === 16 && subSubType === 16) { + const greyTip = GreyTipWrapper.fromBinary(Uint8Array.from(sysMsg.bodyWrapper!.wrappedBody.slice(7))); + if (greyTip.subTypeId === 36) { + const emojiLikeToOthers = EmojiLikeToOthersWrapper1 + .fromBinary(greyTip.rest) + .wrapper! + .body!; + if (emojiLikeToOthers.attributes?.operation !== 1) { // Un-like + return; + } + const eventOrEmpty = await this.apis.GroupApi.createGroupEmojiLikeEvent( + greyTip.groupCode.toString(), + await this.core.apis.UserApi.getUinByUidV2(emojiLikeToOthers.attributes!.senderUid), + emojiLikeToOthers.msgSpec!.msgSeq.toString(), + emojiLikeToOthers.attributes!.emojiId, + ); + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + eventOrEmpty && await this.networkManager.emitEvent(eventOrEmpty); + } + } + */ + } } diff --git a/src/onebot/index.ts b/src/onebot/index.ts index ff4c2cbb..b21f0060 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -238,39 +238,10 @@ export class NapCatOneBot11Adapter { private initMsgListener() { const msgListener = new NodeIKernelMsgListener(); - msgListener.onRecvSysMsg = async (msg) => { - const sysMsg = SysMessage.decode(Uint8Array.from(msg)) as unknown as SysMessageType; - if (sysMsg.msgSpec.length === 0) { - return; - } - const { msgType, subType, subSubType } = sysMsg.msgSpec[0]; - if (msgType === 528 && subType === 39 && subSubType === 39) { - if (!sysMsg.bodyWrapper) return; - let event = await this.apis.UserApi.parseLikeEvent(sysMsg.bodyWrapper.wrappedBody); - if (event) await this.networkManager.emitEvent(event); - }; - /* - if (msgType === 732 && subType === 16 && subSubType === 16) { - const greyTip = GreyTipWrapper.fromBinary(Uint8Array.from(sysMsg.bodyWrapper!.wrappedBody.slice(7))); - if (greyTip.subTypeId === 36) { - const emojiLikeToOthers = EmojiLikeToOthersWrapper1 - .fromBinary(greyTip.rest) - .wrapper! - .body!; - if (emojiLikeToOthers.attributes?.operation !== 1) { // Un-like - return; - } - const eventOrEmpty = await this.apis.GroupApi.createGroupEmojiLikeEvent( - greyTip.groupCode.toString(), - await this.core.apis.UserApi.getUinByUidV2(emojiLikeToOthers.attributes!.senderUid), - emojiLikeToOthers.msgSpec!.msgSeq.toString(), - emojiLikeToOthers.attributes!.emojiId, - ); - // eslint-disable-next-line @typescript-eslint/no-unused-expressions - eventOrEmpty && await this.networkManager.emitEvent(eventOrEmpty); - } - } - */ + msgListener.onRecvSysMsg = (msg) => { + this.apis.MsgApi.parseSysMessage(msg).then((event) => { + if (event) this.networkManager.emitEvent(event); + }).catch(e => this.context.logger.logError('constructSysMessage error: ', e)); }; msgListener.onInputStatusPush = async data => {