1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

feat: GuildChannelDestroyedEvent

This commit is contained in:
Mrs4s 2021-11-14 04:46:57 +08:00
parent c149f28fb8
commit 849be61d5a
No known key found for this signature in database
GPG Key ID: 3186E98FA19CE3A7
4 changed files with 52 additions and 11 deletions

View File

@ -238,7 +238,7 @@ type (
NewChannelInfo *ChannelInfo
}
GuildChannelCreatedEvent struct {
GuildChannelOperationEvent struct {
OperatorId uint64
GuildId uint64
ChannelInfo *ChannelInfo

View File

@ -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

View File

@ -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{}

View File

@ -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