From 849be61d5a74a3122a505dc977864b43ff1bfcda Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Sun, 14 Nov 2021 04:46:57 +0800 Subject: [PATCH] feat: GuildChannelDestroyedEvent --- client/entities.go | 2 +- client/events.go | 22 +++++++++++++++++++--- client/guild.go | 11 +++++++++++ client/guild_eventflow.go | 28 +++++++++++++++++++++------- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/client/entities.go b/client/entities.go index 3a6f64d6..c14a6c65 100644 --- a/client/entities.go +++ b/client/entities.go @@ -238,7 +238,7 @@ type ( NewChannelInfo *ChannelInfo } - GuildChannelCreatedEvent struct { + GuildChannelOperationEvent struct { OperatorId uint64 GuildId uint64 ChannelInfo *ChannelInfo diff --git a/client/events.go b/client/events.go index 9b9dc80d..1eaa75fb 100644 --- a/client/events.go +++ b/client/events.go @@ -17,7 +17,8 @@ type eventHandlers struct { guildChannelMessageHandlers []func(*QQClient, *message.GuildChannelMessage) guildMessageReactionsUpdatedHandlers []func(*QQClient, *GuildMessageReactionsUpdatedEvent) guildChannelUpdatedHandlers []func(*QQClient, *GuildChannelUpdatedEvent) - guildChannelCreatedHandlers []func(*QQClient, *GuildChannelCreatedEvent) + guildChannelCreatedHandlers []func(*QQClient, *GuildChannelOperationEvent) + guildChannelDestroyedHandlers []func(*QQClient, *GuildChannelOperationEvent) groupMuteEventHandlers []func(*QQClient, *GroupMuteEvent) groupRecalledHandlers []func(*QQClient, *GroupMessageRecalledEvent) friendRecalledHandlers []func(*QQClient, *FriendMessageRecalledEvent) @@ -84,10 +85,14 @@ func (s *GuildService) OnGuildChannelUpdated(f func(*QQClient, *GuildChannelUpda s.c.eventHandlers.guildChannelUpdatedHandlers = append(s.c.eventHandlers.guildChannelUpdatedHandlers, f) } -func (s *GuildService) OnGuildChannelCreated(f func(*QQClient, *GuildChannelCreatedEvent)) { +func (s *GuildService) OnGuildChannelCreated(f func(*QQClient, *GuildChannelOperationEvent)) { s.c.eventHandlers.guildChannelCreatedHandlers = append(s.c.eventHandlers.guildChannelCreatedHandlers, f) } +func (s *GuildService) OnGuildChannelDestroyed(f func(*QQClient, *GuildChannelOperationEvent)) { + s.c.eventHandlers.guildChannelDestroyedHandlers = append(s.c.eventHandlers.guildChannelDestroyedHandlers, f) +} + func (c *QQClient) OnGroupMuted(f func(*QQClient, *GroupMuteEvent)) { c.eventHandlers.groupMuteEventHandlers = append(c.eventHandlers.groupMuteEventHandlers, f) } @@ -283,7 +288,7 @@ func (c *QQClient) dispatchGuildChannelUpdatedEvent(e *GuildChannelUpdatedEvent) } } -func (c *QQClient) dispatchGuildChannelCreatedEvent(e *GuildChannelCreatedEvent) { +func (c *QQClient) dispatchGuildChannelCreatedEvent(e *GuildChannelOperationEvent) { if e == nil { return } @@ -294,6 +299,17 @@ func (c *QQClient) dispatchGuildChannelCreatedEvent(e *GuildChannelCreatedEvent) } } +func (c *QQClient) dispatchGuildChannelDestroyedEvent(e *GuildChannelOperationEvent) { + if e == nil { + return + } + for _, f := range c.eventHandlers.guildChannelDestroyedHandlers { + cover(func() { + f(c, e) + }) + } +} + func (c *QQClient) dispatchGroupMuteEvent(e *GroupMuteEvent) { if e == nil { return diff --git a/client/guild.go b/client/guild.go index a8ff5392..b06bd72e 100644 --- a/client/guild.go +++ b/client/guild.go @@ -2,6 +2,7 @@ package client import ( "fmt" + "sort" "time" "github.com/pkg/errors" @@ -153,6 +154,16 @@ func (g *GuildInfo) FindChannel(channelId uint64) *ChannelInfo { return nil } +func (g *GuildInfo) removeChannel(id uint64) { + i := sort.Search(len(g.Channels), func(i int) bool { + return g.Channels[i].ChannelId >= id + }) + if i >= len(g.Channels) || g.Channels[i].ChannelId != id { + return + } + g.Channels = append(g.Channels[:i], g.Channels[i+1:]...) +} + func (s *GuildService) GetUserProfile(tinyId uint64) (*GuildUserProfile, error) { seq := s.c.nextSeq() flags := binary.DynamicProtoMessage{} diff --git a/client/guild_eventflow.go b/client/guild_eventflow.go index 0ccbc3fd..e1f97154 100644 --- a/client/guild_eventflow.go +++ b/client/guild_eventflow.go @@ -77,13 +77,15 @@ func decodeGuildEventFlowPacket(c *QQClient, _ *incomingPacketInfo, payload []by } func (c *QQClient) processGuildEventBody(m *channel.ChannelMsgContent, eventBody *channel.EventBody) { - switch { - case eventBody.CreateChan != nil: - guild := c.GuildService.FindGuild(m.Head.RoutingHead.GetGuildId()) - if guild == nil { - c.Warning("process create channel event error: guild not found.") + var guild *GuildInfo + if m.Head.RoutingHead.GetGuildId() != 0 { + if guild = c.GuildService.FindGuild(m.Head.RoutingHead.GetGuildId()); guild == nil { + c.Warning("process channel event error: guild not found.") return } + } + switch { + case eventBody.CreateChan != nil: for _, chanId := range eventBody.CreateChan.CreateId { if guild.FindChannel(chanId.GetChanId()) != nil { continue @@ -94,16 +96,28 @@ func (c *QQClient) processGuildEventBody(m *channel.ChannelMsgContent, eventBody continue } guild.Channels = append(guild.Channels, channelInfo) - c.dispatchGuildChannelCreatedEvent(&GuildChannelCreatedEvent{ + c.dispatchGuildChannelCreatedEvent(&GuildChannelOperationEvent{ OperatorId: m.Head.RoutingHead.GetFromTinyid(), GuildId: m.Head.RoutingHead.GetGuildId(), ChannelInfo: channelInfo, }) } + case eventBody.DestroyChan != nil: + for _, chanId := range eventBody.DestroyChan.DeleteId { + channelInfo := guild.FindChannel(chanId.GetChanId()) + if channelInfo == nil { + continue + } + guild.removeChannel(chanId.GetChanId()) + c.dispatchGuildChannelDestroyedEvent(&GuildChannelOperationEvent{ + OperatorId: m.Head.RoutingHead.GetFromTinyid(), + GuildId: guild.GuildId, + ChannelInfo: channelInfo, + }) + } case 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 { return