From 00f726b515011d3ca83976afde1a36f60a0e7558 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: Mon, 25 Nov 2024 21:20:32 +0800 Subject: [PATCH] fix: event parse --- src/onebot/api/group.ts | 59 +++++++---------------------------------- src/onebot/index.ts | 21 ++++++++++++--- 2 files changed, 28 insertions(+), 52 deletions(-) diff --git a/src/onebot/api/group.ts b/src/onebot/api/group.ts index 749bc8c8..5a189a78 100644 --- a/src/onebot/api/group.ts +++ b/src/onebot/api/group.ts @@ -174,6 +174,7 @@ export class OneBotGroupApi { }], ); } + async parseCardChangedEvent(msg: RawMessage) { if (msg.senderUin && msg.senderUin !== '0') { const member = await this.core.apis.GroupApi.getGroupMember(msg.peerUid, msg.senderUin); @@ -296,68 +297,28 @@ export class OneBotGroupApi { } async parseGrayTipElement(msg: RawMessage, grayTipElement: GrayTipElement) { - let events: Array = []; - - // 群名片修改事件解析 任何都该判断 - if (msg.senderUin && msg.senderUin !== '0') { - const cardChangedEvent = await this.parseCardChangedEvent(msg); - if (cardChangedEvent) { - events.push(cardChangedEvent); - } - } - - - if (msg.msgType === NTMsgType.KMSGTYPEFILE) { - //文件上传事件 文件为单一元素 - const elementWrapper = msg.elements.find(e => !!e.fileElement); - if (elementWrapper && elementWrapper.fileElement) { - const uploadGroupFileEvent = await this.parseGroupUploadFileEvene(msg, elementWrapper.fileElement, elementWrapper); - if (uploadGroupFileEvent) { - events.push(uploadGroupFileEvent); - return events;//带有file 说明必然不可能是灰条消息 - } - } - } - - if (grayTipElement.subElementType === NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_GROUP) { // 解析群组事件 - const groupEvent = await this.parseGroupElement(msg, grayTipElement.groupElement, grayTipElement); - if (groupEvent) { - events.push(groupEvent); - } + return await this.parseGroupElement(msg, grayTipElement.groupElement, grayTipElement); + } else if (grayTipElement.subElementType === NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_XMLMSG) { // 筛选出表情回应 事件 if (grayTipElement.xmlElement?.templId === '10382') { - const emojiLikeEvent = await this.obContext.apis.GroupApi.parseGroupEmojiLikeEventByGrayTip(msg.peerUid, grayTipElement); - if (emojiLikeEvent) { - events.push(emojiLikeEvent); - } + return await this.obContext.apis.GroupApi.parseGroupEmojiLikeEventByGrayTip(msg.peerUid, grayTipElement); + } else { - const GroupIncreaseEvent = await this.obContext.apis.GroupApi.parseGroupIncreaseEvent(msg.peerUid, grayTipElement); - if (GroupIncreaseEvent) { - events.push(GroupIncreaseEvent); - } + return await this.obContext.apis.GroupApi.parseGroupIncreaseEvent(msg.peerUid, grayTipElement); } } else if (grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_JSON) { // 解析json事件 if (grayTipElement.jsonGrayTipElement.busiId == 1061) { - const paiYiPaiEvent = await this.parsePaiYiPai(msg, grayTipElement.jsonGrayTipElement.jsonStr); - if (paiYiPaiEvent) { - events.push(paiYiPaiEvent); - } + return await this.parsePaiYiPai(msg, grayTipElement.jsonGrayTipElement.jsonStr); } else if (grayTipElement.jsonGrayTipElement.busiId == JsonGrayBusiId.AIO_GROUP_ESSENCE_MSG_TIP) { - const essenceMsgEvent = await this.parseEssenceMsg(msg, grayTipElement.jsonGrayTipElement.jsonStr); - if (essenceMsgEvent) { - events.push(essenceMsgEvent); - } + return await this.parseEssenceMsg(msg, grayTipElement.jsonGrayTipElement.jsonStr); } else { - const otherJsonEvent = await this.parseOtherJsonEvent(msg, grayTipElement.jsonGrayTipElement.jsonStr, this.core.context) - if (otherJsonEvent) { - events.push(otherJsonEvent); - } + return await this.parseOtherJsonEvent(msg, grayTipElement.jsonGrayTipElement.jsonStr, this.core.context) } } - return events; + return undefined; } } diff --git a/src/onebot/index.ts b/src/onebot/index.ts index 56a853af..bb02d11a 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -13,6 +13,7 @@ import { Peer, RawMessage, SendStatusType, + NTMsgType, } from '@/core'; import { OB11ConfigLoader } from '@/onebot/config'; import { @@ -600,10 +601,24 @@ export class NapCatOneBot11Adapter { private async handleGroupEvent(message: RawMessage) { try { + // 群名片修改事件解析 任何都该判断 + if (message.senderUin && message.senderUin !== '0') { + const cardChangedEvent = await this.apis.GroupApi.parseCardChangedEvent(message); + cardChangedEvent && await this.networkManager.emitEvent(cardChangedEvent); + } + if (message.msgType === NTMsgType.KMSGTYPEFILE) { + //文件上传事件 文件为单一元素 + const elementWrapper = message.elements.find(e => !!e.fileElement); + if (elementWrapper?.fileElement) { + const uploadGroupFileEvent = await this.apis.GroupApi.parseGroupUploadFileEvene(message, elementWrapper.fileElement, elementWrapper); + uploadGroupFileEvent && await this.networkManager.emitEvent(uploadGroupFileEvent); + } + } const grayTipElement = message.elements.find((element) => element.grayTipElement)?.grayTipElement; - if (!grayTipElement) return; - const events = await this.apis.GroupApi.parseGrayTipElement(message, grayTipElement); - await this.networkManager.emitEvents(events); + if (grayTipElement) { + const event = await this.apis.GroupApi.parseGrayTipElement(message, grayTipElement); + event && await this.networkManager.emitEvent(event); + } } catch (e) { this.context.logger.logError('constructGroupEvent error: ', e); }