From 10a98ca4f199c67908e7f710b0800db031ca2a4a Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Mon, 8 Nov 2021 18:07:32 +0800 Subject: [PATCH] feat: supported parsing emoji reactions updated event operator id and message sender uin --- client/entities.go | 3 ++- client/guild_msg.go | 35 ++++++++++++++++++++++++++++++++--- client/network.go | 19 +++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/client/entities.go b/client/entities.go index 271f3d0b..3583ef0f 100644 --- a/client/entities.go +++ b/client/entities.go @@ -221,11 +221,12 @@ type ( } GuildMessageReactionsUpdatedEvent struct { - OperatorId uint64 // OperatorId 操作者TinyId, 只有自身消息被贴表情才会有值 + OperatorId uint64 // OperatorId 操作者TinyId, 删除贴表情的事件下不会有值 EmojiId int32 // EmojiId 被贴的表情, 只有自身消息被贴表情才会有值 GuildId uint64 ChannelId uint64 MessageId uint64 + MessageSenderUin int64 // MessageSenderUin 被贴表情的消息发送者QQ号 CurrentReactions []*message.GuildMessageEmojiReaction } diff --git a/client/guild_msg.go b/client/guild_msg.go index 46c09608..aa8ad16b 100644 --- a/client/guild_msg.go +++ b/client/guild_msg.go @@ -8,6 +8,7 @@ import ( "github.com/Mrs4s/MiraiGo/client/pb/msg" "github.com/Mrs4s/MiraiGo/message" "google.golang.org/protobuf/proto" + "time" ) func init() { @@ -41,6 +42,12 @@ func decodeGuildMessagePushPacket(c *QQClient, _ *incomingPacketInfo, payload [] } for _, m := range push.Msgs { if m.Head.ContentHead.GetType() == 3841 { + type tipsPushInfo struct { + TinyId uint64 + TargetMessageSenderUin int64 + GuildId uint64 + ChannelId uint64 + } // todo: 回头 event flow 的处理移出去重构下逻辑, 先暂时这样方便改 var common *msg.CommonElem if m.Body != nil { @@ -52,7 +59,18 @@ func decodeGuildMessagePushPacket(c *QQClient, _ *incomingPacketInfo, payload [] } } if m.Head.ContentHead.GetSubType() == 2 { // todo: tips? - continue + if common == nil { // empty tips + + } + tipsInfo := &tipsPushInfo{ + TinyId: m.Head.RoutingHead.GetFromTinyid(), + GuildId: m.Head.RoutingHead.GetGuildId(), + ChannelId: m.Head.RoutingHead.GetChannelId(), + } + if len(m.CtrlHead.IncludeUin) > 0 { + tipsInfo.TargetMessageSenderUin = int64(m.CtrlHead.IncludeUin[0]) + } + return tipsInfo, nil } if common == nil || common.GetServiceType() != 500 { continue @@ -76,13 +94,24 @@ func decodeGuildMessagePushPacket(c *QQClient, _ *incomingPacketInfo, payload [] if t[0].Head.RoutingHead.GetFromTinyid() == c.GuildService.TinyId { continue } - // todo: 如果是别人消息被贴表情, 会在在后续继续推送一个 empty tips, 可以从那个消息获取到 OperatorId - c.dispatchGuildMessageReactionsUpdatedEvent(&GuildMessageReactionsUpdatedEvent{ + updatedEvent := &GuildMessageReactionsUpdatedEvent{ GuildId: m.Head.RoutingHead.GetGuildId(), ChannelId: m.Head.RoutingHead.GetChannelId(), MessageId: t[0].Head.ContentHead.GetSeq(), CurrentReactions: decodeGuildMessageEmojiReactions(t[0]), + } + tipsInfo, err := c.waitPacketTimeoutSyncF("MsgPush.PushGroupProMsg", time.Second, func(i interface{}) bool { + if i == nil { + return false + } + _, ok := i.(*tipsPushInfo) + return ok }) + if err == nil { + updatedEvent.OperatorId = tipsInfo.(*tipsPushInfo).TinyId + updatedEvent.MessageSenderUin = tipsInfo.(*tipsPushInfo).TargetMessageSenderUin + } + c.dispatchGuildMessageReactionsUpdatedEvent(updatedEvent) } } continue diff --git a/client/network.go b/client/network.go index 994756b3..2e82737c 100644 --- a/client/network.go +++ b/client/network.go @@ -213,6 +213,25 @@ func (c *QQClient) waitPacket(cmd string, f func(interface{}, error)) func() { } } +// waitPacketTimeoutSyncF +// 等待一个数据包解析, 优先级低于 sendAndWait +func (c *QQClient) waitPacketTimeoutSyncF(cmd string, timeout time.Duration, filter func(interface{}) bool) (r interface{}, e error) { + notifyChan := make(chan bool) + defer c.waitPacket(cmd, func(i interface{}, err error) { + if filter(i) { + r = i + e = err + notifyChan <- true + } + })() + select { + case <-notifyChan: + return + case <-time.After(timeout): + return nil, errors.New("timeout") + } +} + // sendAndWaitDynamic // 发送数据包并返回需要解析的 response func (c *QQClient) sendAndWaitDynamic(seq uint16, pkt []byte) ([]byte, error) {