From 8ba47635d3da39af11118a39b082b3ebc89a3115 Mon Sep 17 00:00:00 2001 From: Alen Date: Thu, 25 Jul 2024 01:02:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.增加设精事件上报(目前上报的shortId经常出错,实际消息体却是正确的,待解决) 2.增加设精/取消设精api接口 3.poke事件增加raw信息上报 --- src/ntqqapi/api/group.ts | 25 ++++++++++++ src/ntqqapi/api/msg.ts | 12 +++--- src/ntqqapi/types/msg.ts | 6 +++ .../action/go-cqhttp/DelEssenceMsg.ts | 24 ++++++++++++ .../action/go-cqhttp/SetEssenceMsg.ts | 23 +++++++++++ src/onebot11/action/index.ts | 6 ++- src/onebot11/action/types.ts | 2 + src/onebot11/constructor.ts | 38 +++++++++++++++---- .../event/notice/OB11GroupEssenceEvent.ts | 14 +++++++ 9 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 src/onebot11/action/go-cqhttp/DelEssenceMsg.ts create mode 100644 src/onebot11/action/go-cqhttp/SetEssenceMsg.ts create mode 100644 src/onebot11/event/notice/OB11GroupEssenceEvent.ts diff --git a/src/ntqqapi/api/group.ts b/src/ntqqapi/api/group.ts index 06d00c5..6617dcc 100644 --- a/src/ntqqapi/api/group.ts +++ b/src/ntqqapi/api/group.ts @@ -5,6 +5,7 @@ import { deleteGroup, uidMaps } from '../../common/data' import { dbUtil } from '../../common/db' import { log } from '../../common/utils/log' import { NTQQWindowApi, NTQQWindows } from './window' +import { wrapperApi } from '../native/wrapper' export class NTQQGroupApi { @@ -282,4 +283,28 @@ export class NTQQGroupApi { }) } static publishGroupBulletin(groupQQ: string, title: string, content: string) {} + static async removeGroupEssence(GroupCode: string, msgId: string) { + // 代码没测过 + // 需要 ob11msgid->msgId + (peer) -> msgSeq + msgRandom + let MsgData = await wrapperApi.NodeIQQNTWrapperSession.getMsgService().getMsgsIncludeSelf({ chatType: 2, guildId: '', peerUid: GroupCode }, msgId, 1, false); + let param = { + groupCode: GroupCode, + msgRandom: parseInt(MsgData.msgList[0].msgRandom), + msgSeq: parseInt(MsgData.msgList[0].msgSeq) + }; + // GetMsgByShoretID(ShoretID); -> MsgService.getMsgs(Peer,MsgId,1,false); -> 组出参数 + return wrapperApi.NodeIQQNTWrapperSession.getGroupService().removeGroupEssence(param); + } + static async addGroupEssence(GroupCode: string, msgId: string) { + // 代码没测过 + // 需要 ob11msgid->msgId + (peer) -> msgSeq + msgRandom + let MsgData = await wrapperApi.NodeIQQNTWrapperSession.getMsgService().getMsgsIncludeSelf({ chatType: 2, guildId: '', peerUid: GroupCode }, msgId, 1, false); + let param = { + groupCode: GroupCode, + msgRandom: parseInt(MsgData.msgList[0].msgRandom), + msgSeq: parseInt(MsgData.msgList[0].msgSeq) + }; + // GetMsgByShoretID(ShoretID); -> MsgService.getMsgs(Peer,MsgId,1,false); -> 组出参数 + return wrapperApi.NodeIQQNTWrapperSession.getGroupService().addGroupEssence(param); + } } diff --git a/src/ntqqapi/api/msg.ts b/src/ntqqapi/api/msg.ts index 54ca551..1bd2da8 100644 --- a/src/ntqqapi/api/msg.ts +++ b/src/ntqqapi/api/msg.ts @@ -1,22 +1,17 @@ import { callNTQQApi, GeneralCallResult, NTQQApiMethod } from '../ntcall' -import { ChatType, RawMessage, SendMessageElement } from '../types' +import { ChatType, RawMessage, SendMessageElement, Peer } from '../types' import { dbUtil } from '../../common/db' import { selfInfo } from '../../common/data' import { ReceiveCmdS, registerReceiveHook } from '../hook' import { log } from '../../common/utils/log' import { sleep } from '../../common/utils/helper' import { isQQ998 } from '../../common/utils' +import { wrapperApi } from '@/ntqqapi/native/wrapper' export let sendMessagePool: Record void) | null> = {} // peerUid: callbackFunc export let sentMessages: Record = {} // msgId: RawMessage -export interface Peer { - chatType: ChatType - peerUid: string // 如果是群聊uid为群号,私聊uid就是加密的字符串 - guildId?: '' -} - async function sendWaiter(peer: Peer, waitComplete = true, timeout: number = 10000) { // 等待上一个相同的peer发送完 const peerUid = peer.peerUid @@ -293,4 +288,7 @@ export class NTQQMsgApi { }) }) } + static async getMsgsBySeqAndCount(peer: Peer, seq: string, count: number, desc: boolean, z: boolean) { + return await wrapperApi.NodeIQQNTWrapperSession.getMsgService().getMsgsBySeqAndCount(peer, seq, count, desc, z); + } } diff --git a/src/ntqqapi/types/msg.ts b/src/ntqqapi/types/msg.ts index 1e74520..e50295f 100644 --- a/src/ntqqapi/types/msg.ts +++ b/src/ntqqapi/types/msg.ts @@ -415,3 +415,9 @@ export interface RawMessage { multiForwardMsgElement: MultiForwardMsgElement }[] } + +export interface Peer { + chatType: ChatType; + peerUid: string; // 如果是群聊uid为群号,私聊uid就是加密的字符串 + guildId?: string; +} \ No newline at end of file diff --git a/src/onebot11/action/go-cqhttp/DelEssenceMsg.ts b/src/onebot11/action/go-cqhttp/DelEssenceMsg.ts new file mode 100644 index 0000000..2bc5c3b --- /dev/null +++ b/src/onebot11/action/go-cqhttp/DelEssenceMsg.ts @@ -0,0 +1,24 @@ + +import BaseAction from '../BaseAction'; +import { ActionName } from '../types'; +import { NTQQGroupApi } from '../../../ntqqapi/api/group' +import { dbUtil } from '@/common/db'; + +interface Payload { + message_id: number | string; +} + +export default class GoCQHTTPDelEssenceMsg extends BaseAction { + actionName = ActionName.GoCQHTTP_DelEssenceMsg; + + protected async _handle(payload: Payload): Promise { + const msg = await dbUtil.getMsgByShortId(parseInt(payload.message_id.toString())); + if (!msg) { + throw new Error('msg not found'); + } + return await NTQQGroupApi.removeGroupEssence( + msg.peerUid, + msg.msgId + ); + } +} diff --git a/src/onebot11/action/go-cqhttp/SetEssenceMsg.ts b/src/onebot11/action/go-cqhttp/SetEssenceMsg.ts new file mode 100644 index 0000000..682eddd --- /dev/null +++ b/src/onebot11/action/go-cqhttp/SetEssenceMsg.ts @@ -0,0 +1,23 @@ +import BaseAction from '../BaseAction'; +import { ActionName } from '../types'; +import { NTQQGroupApi } from '../../../ntqqapi/api/group' +import { dbUtil } from '@/common/db'; + +interface Payload { + message_id: number | string; +} + +export default class GoCQHTTPSetEssenceMsg extends BaseAction { + actionName = ActionName.GoCQHTTP_SetEssenceMsg; + + protected async _handle(payload: Payload): Promise { + const msg = await dbUtil.getMsgByShortId(parseInt(payload.message_id.toString())); + if (!msg) { + throw new Error('msg not found'); + } + return await NTQQGroupApi.addGroupEssence( + msg.peerUid, + msg.msgId + ); + } +} diff --git a/src/onebot11/action/index.ts b/src/onebot11/action/index.ts index 3785082..efe1523 100644 --- a/src/onebot11/action/index.ts +++ b/src/onebot11/action/index.ts @@ -50,6 +50,8 @@ import { ForwardFriendSingleMsg, ForwardGroupSingleMsg } from './msg/ForwardSing import { GetGroupEssence } from './group/GetGroupEssence' import { GetGroupHonorInfo } from './group/GetGroupHonorInfo' import { GoCQHTTHandleQuickOperation } from './go-cqhttp/QuickOperation' +import GoCQHTTPSetEssenceMsg from './go-cqhttp/SetEssenceMsg' +import GoCQHTTPDelEssenceMsg from './go-cqhttp/DelEssenceMsg' export const actionHandlers = [ new GetFile(), @@ -106,7 +108,9 @@ export const actionHandlers = [ new GoCQHTTPUploadPrivateFile(), new GoCQHTTPGetGroupMsgHistory(), new GoCQHTTGetForwardMsgAction(), - new GoCQHTTHandleQuickOperation() + new GoCQHTTHandleQuickOperation(), + new GoCQHTTPSetEssenceMsg(), + new GoCQHTTPDelEssenceMsg() ] function initActionMap() { diff --git a/src/onebot11/action/types.ts b/src/onebot11/action/types.ts index 1b61ecc..ec38c6e 100644 --- a/src/onebot11/action/types.ts +++ b/src/onebot11/action/types.ts @@ -70,4 +70,6 @@ export enum ActionName { GoCQHTTP_GetEssenceMsg = "get_essence_msg_list", GoCQHTTP_HandleQuickOperation = ".handle_quick_operation", GetGroupHonorInfo = "get_group_honor_info", + GoCQHTTP_SetEssenceMsg = 'set_essence_msg', + GoCQHTTP_DelEssenceMsg = 'delete_essence_msg', } diff --git a/src/onebot11/constructor.ts b/src/onebot11/constructor.ts index dc3e159..eaae427 100644 --- a/src/onebot11/constructor.ts +++ b/src/onebot11/constructor.ts @@ -15,6 +15,7 @@ import { FaceIndex, GrayTipElementSubType, Group, + Peer, GroupMember, PicType, RawMessage, @@ -34,6 +35,7 @@ import { OB11GroupUploadNoticeEvent } from './event/notice/OB11GroupUploadNotice import { OB11GroupNoticeEvent } from './event/notice/OB11GroupNoticeEvent' import { NTQQUserApi } from '../ntqqapi/api/user' import { NTQQFileApi } from '../ntqqapi/api/file' +import { NTQQMsgApi } from '../ntqqapi/api/msg' import { calcQQLevel } from '../common/utils/qqlevel' import { log } from '../common/utils/log' import { sleep } from '../common/utils/helper' @@ -49,6 +51,7 @@ import { OB11FriendRecallNoticeEvent } from './event/notice/OB11FriendRecallNoti import { OB11GroupRecallNoticeEvent } from './event/notice/OB11GroupRecallNoticeEvent' import { OB11FriendPokeEvent, OB11GroupPokeEvent } from './event/notice/OB11PokeEvent' import { OB11BaseNoticeEvent } from './event/notice/OB11BaseNoticeEvent'; +import { OB11GroupEssenceEvent } from './event/notice/OB11GroupEssenceEvent'; let lastRKeyUpdateTime = 0 @@ -533,13 +536,34 @@ export class OB11Constructor { return new OB11GroupPokeEvent(parseInt(msg.peerUid), parseInt((uidMaps[poke_uid[0].uid])!), parseInt((uidMaps[poke_uid[1].uid])), pokedetail); } } - const memberUin = json.items[1].param[0] - const title = json.items[3].txt - log('收到群成员新头衔消息', json) - getGroupMember(msg.peerUid, memberUin).then((member) => { - member.memberSpecialTitle = title - }) - return new OB11GroupTitleEvent(parseInt(msg.peerUid), parseInt(memberUin), title) + if (grayTipElement.jsonGrayTipElement.busiId == 2401) { + log('收到群精华消息', json) + const searchParams = new URL(json.items[0].jp).searchParams; + const msgSeq = searchParams.get('msgSeq')!; + const Group = searchParams.get('groupCode'); + const Businessid = searchParams.get('businessid'); + const Peer: Peer = { + guildId: '', + chatType: ChatType.group, + peerUid: Group! + }; + const msgList = (await NTQQMsgApi.getMsgsBySeqAndCount(Peer, msgSeq.toString(), 1, true, true)).msgList; + // (待解决) getMsgByLongId 拿到的 ShortId 经常跟实际 Msg 对不上(但msgId是一致的)。 + // 不过引用消息拿到的 ShortId 是对的。 + //console.log("原始消息: ", msgList); + //console.log("本地缓存: ", await dbUtil.getMsgByLongId(msgList[0].msgId)); + return new OB11GroupEssenceEvent(parseInt(msg.peerUid), Number(((await dbUtil.getMsgByLongId(msgList[0].msgId)).msgShortId)!), parseInt(msgList[0].senderUin)); + // 获取MsgSeq+Peer可获取具体消息 + } + if (grayTipElement.jsonGrayTipElement.busiId == 2407) { + const memberUin = json.items[1].param[0] + const title = json.items[3].txt + log('收到群成员新头衔消息', json) + getGroupMember(msg.peerUid, memberUin).then((member) => { + member.memberSpecialTitle = title + }) + return new OB11GroupTitleEvent(parseInt(msg.peerUid), parseInt(memberUin), title) + } } } } diff --git a/src/onebot11/event/notice/OB11GroupEssenceEvent.ts b/src/onebot11/event/notice/OB11GroupEssenceEvent.ts new file mode 100644 index 0000000..ae1053e --- /dev/null +++ b/src/onebot11/event/notice/OB11GroupEssenceEvent.ts @@ -0,0 +1,14 @@ +import { OB11GroupNoticeEvent } from './OB11GroupNoticeEvent'; +export class OB11GroupEssenceEvent extends OB11GroupNoticeEvent { + notice_type = 'essence'; + message_id: number; + sender_id: number; + sub_type: 'add' | 'delete' = 'add'; + + constructor(groupId: number, message_id: number, sender_id: number) { + super(); + this.group_id = groupId; + this.message_id = message_id; + this.sender_id = sender_id; + } +}