1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00

feat: GuildChannelUpdatedEvent

This commit is contained in:
Mrs4s 2021-11-11 19:25:59 +08:00
parent e02a9ece6e
commit e3411adb3d
No known key found for this signature in database
GPG Key ID: 3186E98FA19CE3A7
4 changed files with 61 additions and 0 deletions

View File

@ -230,6 +230,14 @@ type (
CurrentReactions []*message.GuildMessageEmojiReaction CurrentReactions []*message.GuildMessageEmojiReaction
} }
GuildChannelUpdatedEvent struct {
OperatorId uint64
GuildId uint64
ChannelId uint64
OldChannelInfo *ChannelInfo
NewChannelInfo *ChannelInfo
}
OcrResponse struct { OcrResponse struct {
Texts []*TextDetection `json:"texts"` Texts []*TextDetection `json:"texts"`
Language string `json:"language"` Language string `json:"language"`

View File

@ -16,6 +16,7 @@ type eventHandlers struct {
selfGroupMessageHandlers []func(*QQClient, *message.GroupMessage) selfGroupMessageHandlers []func(*QQClient, *message.GroupMessage)
guildChannelMessageHandlers []func(*QQClient, *message.GuildChannelMessage) guildChannelMessageHandlers []func(*QQClient, *message.GuildChannelMessage)
guildMessageReactionsUpdatedHandlers []func(*QQClient, *GuildMessageReactionsUpdatedEvent) guildMessageReactionsUpdatedHandlers []func(*QQClient, *GuildMessageReactionsUpdatedEvent)
guildChannelUpdatedHandlers []func(*QQClient, *GuildChannelUpdatedEvent)
groupMuteEventHandlers []func(*QQClient, *GroupMuteEvent) groupMuteEventHandlers []func(*QQClient, *GroupMuteEvent)
groupRecalledHandlers []func(*QQClient, *GroupMessageRecalledEvent) groupRecalledHandlers []func(*QQClient, *GroupMessageRecalledEvent)
friendRecalledHandlers []func(*QQClient, *FriendMessageRecalledEvent) friendRecalledHandlers []func(*QQClient, *FriendMessageRecalledEvent)
@ -78,6 +79,10 @@ func (s *GuildService) OnGuildMessageReactionsUpdated(f func(*QQClient, *GuildMe
s.c.eventHandlers.guildMessageReactionsUpdatedHandlers = append(s.c.eventHandlers.guildMessageReactionsUpdatedHandlers, f) s.c.eventHandlers.guildMessageReactionsUpdatedHandlers = append(s.c.eventHandlers.guildMessageReactionsUpdatedHandlers, f)
} }
func (s *GuildService) OnGuildChannelUpdated(f func(*QQClient, *GuildChannelUpdatedEvent)) {
s.c.eventHandlers.guildChannelUpdatedHandlers = append(s.c.eventHandlers.guildChannelUpdatedHandlers, f)
}
func (c *QQClient) OnGroupMuted(f func(*QQClient, *GroupMuteEvent)) { func (c *QQClient) OnGroupMuted(f func(*QQClient, *GroupMuteEvent)) {
c.eventHandlers.groupMuteEventHandlers = append(c.eventHandlers.groupMuteEventHandlers, f) c.eventHandlers.groupMuteEventHandlers = append(c.eventHandlers.groupMuteEventHandlers, f)
} }
@ -262,6 +267,17 @@ func (c *QQClient) dispatchGuildMessageReactionsUpdatedEvent(e *GuildMessageReac
} }
} }
func (c *QQClient) dispatchGuildChannelUpdatedEvent(e *GuildChannelUpdatedEvent) {
if e == nil {
return
}
for _, f := range c.eventHandlers.guildChannelUpdatedHandlers {
cover(func() {
f(c, e)
})
}
}
func (c *QQClient) dispatchGroupMuteEvent(e *GroupMuteEvent) { func (c *QQClient) dispatchGroupMuteEvent(e *GroupMuteEvent) {
if e == nil { if e == nil {
return return

View File

@ -2,6 +2,7 @@ package client
import ( import (
"fmt" "fmt"
"time"
"github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client/pb/channel" "github.com/Mrs4s/MiraiGo/client/pb/channel"
@ -76,6 +77,8 @@ type (
ChannelType ChannelType ChannelType ChannelType
AtAllSeq uint64 AtAllSeq uint64
Meta *ChannelMeta Meta *ChannelMeta
fetchTime int64
} }
ChannelMeta struct { ChannelMeta struct {
@ -381,6 +384,7 @@ func convertChannelInfo(info *channel.GuildChannelInfo) *ChannelInfo {
NotifyType: uint32(info.GetFinalNotifyType()), NotifyType: uint32(info.GetFinalNotifyType()),
ChannelType: ChannelType(info.GetChannelType()), ChannelType: ChannelType(info.GetChannelType()),
Meta: meta, Meta: meta,
fetchTime: time.Now().Unix(),
} }
} }

View File

@ -1,6 +1,7 @@
package client package client
import ( import (
"sync"
"time" "time"
"github.com/Mrs4s/MiraiGo/client/pb/channel" "github.com/Mrs4s/MiraiGo/client/pb/channel"
@ -14,6 +15,10 @@ func init() {
decoders["MsgPush.PushGroupProMsg"] = decodeGuildEventFlowPacket decoders["MsgPush.PushGroupProMsg"] = decodeGuildEventFlowPacket
} }
var (
updateChanLock sync.Mutex
)
func decodeGuildEventFlowPacket(c *QQClient, _ *incomingPacketInfo, payload []byte) (interface{}, error) { func decodeGuildEventFlowPacket(c *QQClient, _ *incomingPacketInfo, payload []byte) (interface{}, error) {
push := new(channel.MsgOnlinePush) push := new(channel.MsgOnlinePush)
if err := proto.Unmarshal(payload, push); err != nil { if err := proto.Unmarshal(payload, push); err != nil {
@ -59,6 +64,34 @@ func decodeGuildEventFlowPacket(c *QQClient, _ *incomingPacketInfo, payload []by
c.Error("failed to unmarshal guild channel event body: %v", err) c.Error("failed to unmarshal guild channel event body: %v", err)
continue continue
} }
if eventBody.ChangeChanInfo != nil {
updateChanLock.Lock()
defer updateChanLock.Unlock()
guild := c.GuildService.FindGuild(m.Head.RoutingHead.GetGuildId())
oldInfo := guild.FindChannel(eventBody.ChangeChanInfo.GetChanId())
if time.Now().Unix()-oldInfo.fetchTime <= 2 {
continue
}
newInfo, err := c.GuildService.FetchChannelInfo(m.Head.RoutingHead.GetGuildId(), eventBody.ChangeChanInfo.GetChanId())
if err != nil {
c.Error("error to decode channel info updated event: fetch channel info failed: %v", err)
continue
}
for i := range guild.Channels {
if guild.Channels[i].ChannelId == newInfo.ChannelId {
guild.Channels[i] = newInfo
break
}
}
c.dispatchGuildChannelUpdatedEvent(&GuildChannelUpdatedEvent{
OperatorId: m.Head.RoutingHead.GetFromTinyid(),
GuildId: m.Head.RoutingHead.GetGuildId(),
ChannelId: eventBody.ChangeChanInfo.GetChanId(),
OldChannelInfo: oldInfo,
NewChannelInfo: newInfo,
})
continue
}
if eventBody.UpdateMsg != nil { if eventBody.UpdateMsg != nil {
if eventBody.UpdateMsg.GetEventType() == 1 || eventBody.UpdateMsg.GetEventType() == 2 { // todo: 撤回消息 if eventBody.UpdateMsg.GetEventType() == 1 || eventBody.UpdateMsg.GetEventType() == 2 { // todo: 撤回消息
continue continue