From 726a0d0394c50b830bcc9fc1d258cabcefdd7268 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: Sun, 29 Dec 2024 22:30:33 +0800 Subject: [PATCH 1/4] fix --- src/onebot/api/msg.ts | 44 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index f125eb7f..48d52aef 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -990,7 +990,27 @@ export class OneBotMsgApi { if (SysMessage.contentHead.type == 33 && SysMessage.body?.msgContent) { const groupChange = new NapProtoMsg(GroupChange).decode(SysMessage.body.msgContent); this.core.apis.GroupApi.refreshGroupMemberCache(groupChange.groupUin.toString()).then().catch(); - const operatorUid = groupChange.operatorInfo ? Buffer.from(groupChange.operatorInfo).toString() : ''; + let operatorUid = groupChange.operatorInfo ? Buffer.from(groupChange.operatorInfo).toString() : ''; + + let groupRole = this.core.apis.GroupApi.groupMemberCache.get(groupChange.groupUin.toString())?.get(this.core.selfInfo.uid.toString())?.role; + let isAdminOrOwner = groupRole === 3 || groupRole === 4; + if (isAdminOrOwner && !operatorUid) { + let dataNotify: GroupNotify | undefined; + await this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onGroupNotifiesUpdated', + (doubt, notifies) => { + for (const notify of notifies) { + if (notify.group.groupCode === groupChange.groupUin.toString() && notify.user1.uid === groupChange.memberUid) { + dataNotify = notify; + return true; + } + } + return false; + }, 1, 1000).catch(undefined); + if (dataNotify) { + operatorUid = dataNotify.actionUser.uid ?? dataNotify.user2.uid; + } + } + return new OB11GroupIncreaseEvent( this.core, groupChange.groupUin, @@ -1001,9 +1021,29 @@ export class OneBotMsgApi { } else if (SysMessage.contentHead.type == 34 && SysMessage.body?.msgContent) { const groupChange = new NapProtoMsg(GroupChange).decode(SysMessage.body.msgContent); // 自身被踢出时operatorInfo会是一个protobuf 否则大多数情况为一个string - const operatorUid = groupChange.decreaseType === 3 && groupChange.operatorInfo ? + let operatorUid = groupChange.decreaseType === 3 && groupChange.operatorInfo ? new NapProtoMsg(GroupChangeInfo).decode(groupChange.operatorInfo).operator?.operatorUid : groupChange.operatorInfo?.toString(); + let groupRole = this.core.apis.GroupApi.groupMemberCache.get(groupChange.groupUin.toString())?.get(this.core.selfInfo.uid.toString())?.role; + let isAdminOrOwner = groupRole === 3 || groupRole === 4; + + if (isAdminOrOwner && !operatorUid) { + let dataNotify: GroupNotify | undefined; + await this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onGroupNotifiesUpdated', + (doubt, notifies) => { + for (const notify of notifies) { + if (notify.group.groupCode === groupChange.groupUin.toString() && notify.user1.uid === groupChange.memberUid) { + dataNotify = notify; + return true; + } + } + return false; + }, 1, 1000).catch(undefined); + if (dataNotify) { + operatorUid = dataNotify.actionUser.uid ?? dataNotify.user2.uid; + } + } + if (groupChange.memberUid === this.core.selfInfo.uid) { setTimeout(() => { this.core.apis.GroupApi.groupMemberCache.delete(groupChange.groupUin.toString()); From 5fc5a6f1a6a05dd3292277092088c437301cd660 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: Sun, 29 Dec 2024 22:44:25 +0800 Subject: [PATCH 2/4] fix: #670 --- src/onebot/api/msg.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index 48d52aef..55c97403 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -994,6 +994,9 @@ export class OneBotMsgApi { let groupRole = this.core.apis.GroupApi.groupMemberCache.get(groupChange.groupUin.toString())?.get(this.core.selfInfo.uid.toString())?.role; let isAdminOrOwner = groupRole === 3 || groupRole === 4; + + console.log(Buffer.from(msg).toString('hex')); + if (isAdminOrOwner && !operatorUid) { let dataNotify: GroupNotify | undefined; await this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onGroupNotifiesUpdated', @@ -1007,8 +1010,9 @@ export class OneBotMsgApi { return false; }, 1, 1000).catch(undefined); if (dataNotify) { - operatorUid = dataNotify.actionUser.uid ?? dataNotify.user2.uid; + operatorUid = !!dataNotify.actionUser.uid ? dataNotify.actionUser.uid :dataNotify.user2.uid; } + console.log(dataNotify); } return new OB11GroupIncreaseEvent( @@ -1027,6 +1031,7 @@ export class OneBotMsgApi { let groupRole = this.core.apis.GroupApi.groupMemberCache.get(groupChange.groupUin.toString())?.get(this.core.selfInfo.uid.toString())?.role; let isAdminOrOwner = groupRole === 3 || groupRole === 4; + console.log(Buffer.from(msg).toString('hex')); if (isAdminOrOwner && !operatorUid) { let dataNotify: GroupNotify | undefined; await this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onGroupNotifiesUpdated', @@ -1039,8 +1044,9 @@ export class OneBotMsgApi { } return false; }, 1, 1000).catch(undefined); + console.log(dataNotify); if (dataNotify) { - operatorUid = dataNotify.actionUser.uid ?? dataNotify.user2.uid; + operatorUid = !!dataNotify.actionUser.uid ? dataNotify.actionUser.uid :dataNotify.user2.uid; } } From ac52d9bae2846c83a4028056dc1fd41ebc76bf45 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, 30 Dec 2024 11:59:16 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E7=AE=97=E4=BA=86=E8=83=BD=E7=94=A8?= =?UTF-8?q?=E5=B0=B1=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/api/msg.ts | 102 ++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index 55c97403..5fe5c2ea 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -972,6 +972,7 @@ export class OneBotMsgApi { return { path, fileName: inputdata.name ?? fileName }; } + groupChangDecreseType2String(type: number): GroupDecreaseSubType { switch (type) { case 130: @@ -985,36 +986,49 @@ export class OneBotMsgApi { } } + async waitGroupNotify(groupUin: string, memberUid?: string, operatorUid?: string) { + let groupRole = this.core.apis.GroupApi.groupMemberCache.get(groupUin)?.get(this.core.selfInfo.uid.toString())?.role; + let isAdminOrOwner = groupRole === 3 || groupRole === 4; + + if (isAdminOrOwner && !operatorUid) { + let dataNotify: GroupNotify | undefined; + await this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onGroupNotifiesUpdated', + (doubt, notifies) => { + for (const notify of notifies) { + if (notify.group.groupCode === groupUin && notify.user1.uid === memberUid) { + dataNotify = notify; + return true; + } + } + return false; + }, 1, 1000).catch(undefined); + if (dataNotify) { + return !dataNotify.actionUser.uid ? dataNotify.user2.uid : dataNotify.actionUser.uid; + } + } + + return operatorUid; + } + async parseSysMessage(msg: number[]) { const SysMessage = new NapProtoMsg(PushMsgBody).decode(Uint8Array.from(msg)); - if (SysMessage.contentHead.type == 33 && SysMessage.body?.msgContent) { + if (SysMessage.contentHead.type == 85 && SysMessage.body?.msgContent) { + const groupChange = new NapProtoMsg(GroupChange).decode(SysMessage.body.msgContent); + return new OB11GroupIncreaseEvent( + this.core, + groupChange.groupUin, + +this.core.selfInfo.uin, + groupChange.memberUid ? +await this.core.apis.UserApi.getUinByUidV2(groupChange.memberUid) : 0, + 'approve' + ); + } else if (SysMessage.contentHead.type == 33 && SysMessage.body?.msgContent) { const groupChange = new NapProtoMsg(GroupChange).decode(SysMessage.body.msgContent); this.core.apis.GroupApi.refreshGroupMemberCache(groupChange.groupUin.toString()).then().catch(); - let operatorUid = groupChange.operatorInfo ? Buffer.from(groupChange.operatorInfo).toString() : ''; - - let groupRole = this.core.apis.GroupApi.groupMemberCache.get(groupChange.groupUin.toString())?.get(this.core.selfInfo.uid.toString())?.role; - let isAdminOrOwner = groupRole === 3 || groupRole === 4; - - console.log(Buffer.from(msg).toString('hex')); - - if (isAdminOrOwner && !operatorUid) { - let dataNotify: GroupNotify | undefined; - await this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onGroupNotifiesUpdated', - (doubt, notifies) => { - for (const notify of notifies) { - if (notify.group.groupCode === groupChange.groupUin.toString() && notify.user1.uid === groupChange.memberUid) { - dataNotify = notify; - return true; - } - } - return false; - }, 1, 1000).catch(undefined); - if (dataNotify) { - operatorUid = !!dataNotify.actionUser.uid ? dataNotify.actionUser.uid :dataNotify.user2.uid; - } - console.log(dataNotify); - } - + let operatorUid = await this.waitGroupNotify( + groupChange.groupUin.toString(), + groupChange.memberUid, + groupChange.operatorInfo ? Buffer.from(groupChange.operatorInfo).toString() : '' + ); return new OB11GroupIncreaseEvent( this.core, groupChange.groupUin, @@ -1022,34 +1036,17 @@ export class OneBotMsgApi { operatorUid ? +await this.core.apis.UserApi.getUinByUidV2(operatorUid) : 0, groupChange.decreaseType == 131 ? 'invite' : 'approve', ); + } else if (SysMessage.contentHead.type == 34 && SysMessage.body?.msgContent) { const groupChange = new NapProtoMsg(GroupChange).decode(SysMessage.body.msgContent); // 自身被踢出时operatorInfo会是一个protobuf 否则大多数情况为一个string - let operatorUid = groupChange.decreaseType === 3 && groupChange.operatorInfo ? - new NapProtoMsg(GroupChangeInfo).decode(groupChange.operatorInfo).operator?.operatorUid : - groupChange.operatorInfo?.toString(); - let groupRole = this.core.apis.GroupApi.groupMemberCache.get(groupChange.groupUin.toString())?.get(this.core.selfInfo.uid.toString())?.role; - let isAdminOrOwner = groupRole === 3 || groupRole === 4; - - console.log(Buffer.from(msg).toString('hex')); - if (isAdminOrOwner && !operatorUid) { - let dataNotify: GroupNotify | undefined; - await this.core.eventWrapper.registerListen('NodeIKernelGroupListener/onGroupNotifiesUpdated', - (doubt, notifies) => { - for (const notify of notifies) { - if (notify.group.groupCode === groupChange.groupUin.toString() && notify.user1.uid === groupChange.memberUid) { - dataNotify = notify; - return true; - } - } - return false; - }, 1, 1000).catch(undefined); - console.log(dataNotify); - if (dataNotify) { - operatorUid = !!dataNotify.actionUser.uid ? dataNotify.actionUser.uid :dataNotify.user2.uid; - } - } - + let operatorUid = await this.waitGroupNotify( + groupChange.groupUin.toString(), + groupChange.memberUid, + groupChange.decreaseType === 3 && groupChange.operatorInfo ? + new NapProtoMsg(GroupChangeInfo).decode(groupChange.operatorInfo).operator?.operatorUid : + groupChange.operatorInfo?.toString() + ); if (groupChange.memberUid === this.core.selfInfo.uid) { setTimeout(() => { this.core.apis.GroupApi.groupMemberCache.delete(groupChange.groupUin.toString()); @@ -1149,8 +1146,7 @@ export class OneBotMsgApi { '', request_seq ); - } - else if (SysMessage.contentHead.type == 528 && SysMessage.contentHead.subType == 39 && SysMessage.body?.msgContent) { + } else if (SysMessage.contentHead.type == 528 && SysMessage.contentHead.subType == 39 && SysMessage.body?.msgContent) { return await this.obContext.apis.UserApi.parseLikeEvent(SysMessage.body?.msgContent); } } From de7996d7895a25ed40278ac3d166015cacb4e95d 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, 30 Dec 2024 12:27:42 +0800 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20=E8=BF=9B=E7=BE=A4=E5=85=A5?= =?UTF-8?q?=E7=BE=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/types/element.ts | 1 + src/onebot/api/group.ts | 40 +++++++++++++++++++++++++++++++++++++-- src/onebot/api/msg.ts | 12 ++---------- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/core/types/element.ts b/src/core/types/element.ts index 8a848876..be9eec2d 100644 --- a/src/core/types/element.ts +++ b/src/core/types/element.ts @@ -56,6 +56,7 @@ export interface GrayTipElement { aioOpGrayTipElement: TipAioOpGrayTipElement; groupElement: TipGroupElement; xmlElement: { + busiId: string; content: string; templId: string; }; diff --git a/src/onebot/api/group.ts b/src/onebot/api/group.ts index fd58456d..870b0268 100644 --- a/src/onebot/api/group.ts +++ b/src/onebot/api/group.ts @@ -24,6 +24,7 @@ import { OB11GroupUploadNoticeEvent } from '../event/notice/OB11GroupUploadNotic import { OB11GroupNameEvent } from '../event/notice/OB11GroupNameEvent'; import { pathToFileURL } from 'node:url'; import { FileNapCatOneBotUUID } from '@/common/helper'; +import { OB11GroupIncreaseEvent } from '../event/notice/OB11GroupIncreaseEvent'; export class OneBotGroupApi { obContext: NapCatOneBot11Adapter; @@ -219,16 +220,51 @@ export class OneBotGroupApi { } else if (element.type === TipGroupElementType.KSHUTUP) { let event = await this.parseGroupBanEvent(msg.peerUid, elementWrapper); return event; + } else if (element.type === TipGroupElementType.KMEMBERADD) { + // 自己的通知 协议推送为type->85 在这里实现为了避免邀请出现问题 + if (element.memberUid == this.core.selfInfo.uid) { + this.core.apis.GroupApi.refreshGroupMemberCache(msg.peerUid).then().catch(); + return new OB11GroupIncreaseEvent( + this.core, + parseInt(msg.peerUid), + +this.core.selfInfo.uin, + element.adminUid ? +await this.core.apis.UserApi.getUinByUidV2(element.adminUid) : 0, + 'approve' + ); + } } } + + async parseSelfInviteEvent(msg: RawMessage, inviterUin: string, inviteeUin: string) { + return new OB11GroupIncreaseEvent( + this.core, + parseInt(msg.peerUid), + +inviteeUin, + +inviterUin, + 'invite' + ); + } async parseGrayTipElement(msg: RawMessage, grayTipElement: GrayTipElement) { if (grayTipElement.subElementType === NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_GROUP) { // 解析群组事件 由sysmsg解析 return await this.parseGroupElement(msg, grayTipElement.groupElement, grayTipElement); } else if (grayTipElement.subElementType === NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_XMLMSG) { - // 筛选出表情回应 事件 - if (grayTipElement.xmlElement?.templId === '10382') { + // 筛选自身入群情况 + // if (grayTipElement.xmlElement.busiId === '10145') { + // const inviteData = new fastXmlParser.XMLParser({ + // ignoreAttributes: false, + // attributeNamePrefix: '', + // }).parse(grayTipElement.xmlElement.content); + + // const inviterUin: string = inviteData.gtip.qq[0].jp; + // const inviteeUin: string = inviteData.gtip.qq[1].jp; + // //刷新群缓存 + // if (inviteeUin === this.core.selfInfo.uin) { + // this.core.apis.GroupApi.refreshGroupMemberCache(msg.peerUid).then().catch(); + // return this.parseSelfInviteEvent(msg, inviterUin, inviteeUin); + // } + } else if (grayTipElement.xmlElement?.templId === '10382') { return await this.obContext.apis.GroupApi.parseGroupEmojiLikeEventByGrayTip(msg.peerUid, grayTipElement); } else { //return await this.obContext.apis.GroupApi.parseGroupIncreaseEvent(msg.peerUid, grayTipElement); diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index 5fe5c2ea..5cca3c86 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -1012,16 +1012,8 @@ export class OneBotMsgApi { async parseSysMessage(msg: number[]) { const SysMessage = new NapProtoMsg(PushMsgBody).decode(Uint8Array.from(msg)); - if (SysMessage.contentHead.type == 85 && SysMessage.body?.msgContent) { - const groupChange = new NapProtoMsg(GroupChange).decode(SysMessage.body.msgContent); - return new OB11GroupIncreaseEvent( - this.core, - groupChange.groupUin, - +this.core.selfInfo.uin, - groupChange.memberUid ? +await this.core.apis.UserApi.getUinByUidV2(groupChange.memberUid) : 0, - 'approve' - ); - } else if (SysMessage.contentHead.type == 33 && SysMessage.body?.msgContent) { + // 邀请需要解grayTipElement + if (SysMessage.contentHead.type == 33 && SysMessage.body?.msgContent) { const groupChange = new NapProtoMsg(GroupChange).decode(SysMessage.body.msgContent); this.core.apis.GroupApi.refreshGroupMemberCache(groupChange.groupUin.toString()).then().catch(); let operatorUid = await this.waitGroupNotify(