From 9f9db54192c2f8dc57e387349e115e1821ef5329 Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Fri, 12 Nov 2021 03:04:11 +0800 Subject: [PATCH] feat: guild channel updated event --- coolq/api.go | 72 +---------------------------------------- coolq/bot.go | 3 +- coolq/converter.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++ coolq/event.go | 19 ++++++++++- 4 files changed, 102 insertions(+), 73 deletions(-) create mode 100644 coolq/converter.go diff --git a/coolq/api.go b/coolq/api.go index b57d9d3..2a1b387 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -123,28 +123,7 @@ func (bot *CQBot) CQGetGuildChannelList(guildID uint64, noCache bool) global.MSG } channels := make([]global.MSG, 0, len(guild.Channels)) for _, c := range guild.Channels { - slowModes := make([]global.MSG, 0, len(c.Meta.SlowModes)) - for _, mode := range c.Meta.SlowModes { - slowModes = append(slowModes, global.MSG{ - "slow_mode_key": mode.SlowModeKey, - "slow_mode_text": mode.SlowModeText, - "speak_frequency": mode.SpeakFrequency, - "slow_mode_circle": mode.SlowModeCircle, - }) - } - channels = append(channels, global.MSG{ - "channel_id": c.ChannelId, - "channel_type": c.ChannelType, - "channel_name": c.ChannelName, - "owner_guild_id": c.Meta.GuildId, - "creator_id": c.Meta.CreatorUin, - "creator_tiny_id": c.Meta.CreatorTinyId, - "create_time": c.Meta.CreateTime, - "current_slow_mode": c.Meta.CurrentSlowMode, - "talk_permission": c.Meta.TalkPermission, - "visible_type": c.Meta.VisibleType, - "slow_modes": slowModes, - }) + channels = append(channels, convertChannelInfo(c)) } return OK(channels) } @@ -1712,55 +1691,6 @@ func Failed(code int, msg ...string) global.MSG { return global.MSG{"data": nil, "retcode": code, "msg": m, "wording": w, "status": "failed"} } -func convertGroupMemberInfo(groupID int64, m *client.GroupMemberInfo) global.MSG { - return global.MSG{ - "group_id": groupID, - "user_id": m.Uin, - "nickname": m.Nickname, - "card": m.CardName, - "sex": func() string { - if m.Gender == 1 { - return "female" - } else if m.Gender == 0 { - return "male" - } - // unknown = 0xff - return "unknown" - }(), - "age": 0, - "area": "", - "join_time": m.JoinTime, - "last_sent_time": m.LastSpeakTime, - "shut_up_timestamp": m.ShutUpTimestamp, - "level": strconv.FormatInt(int64(m.Level), 10), - "role": func() string { - switch m.Permission { - case client.Owner: - return "owner" - case client.Administrator: - return "admin" - case client.Member: - return "member" - default: - return "member" - } - }(), - "unfriendly": false, - "title": m.SpecialTitle, - "title_expire_time": m.SpecialTitleExpireTime, - "card_changeable": false, - } -} - -func convertGuildMemberInfo(m *client.GuildMemberInfo) global.MSG { - return global.MSG{ - "tiny_id": m.TinyId, - "title": m.Title, - "nickname": m.Nickname, - "role": m.Role, - } -} - func limitedString(str string) string { limited := [14]rune{10: ' ', 11: '.', 12: '.', 13: '.'} i := 0 diff --git a/coolq/bot.go b/coolq/bot.go index 8e96ba6..2168c3d 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -77,7 +77,8 @@ func NewQQBot(cli *client.QQClient) *CQBot { } bot.Client.OnTempMessage(bot.tempMessageEvent) bot.Client.GuildService.OnGuildChannelMessage(bot.guildChannelMessageEvent) - bot.Client.GuildService.OnGuildMessageReactionsUpdated(bot.guildMessageReactionsUpdated) + bot.Client.GuildService.OnGuildMessageReactionsUpdated(bot.guildMessageReactionsUpdatedEvent) + bot.Client.GuildService.OnGuildChannelUpdated(bot.guildChannelUpdatedEvent) bot.Client.OnGroupMuted(bot.groupMutedEvent) bot.Client.OnGroupMessageRecalled(bot.groupRecallEvent) bot.Client.OnGroupNotify(bot.groupNotifyEvent) diff --git a/coolq/converter.go b/coolq/converter.go new file mode 100644 index 0000000..44b1e14 --- /dev/null +++ b/coolq/converter.go @@ -0,0 +1,81 @@ +package coolq + +import ( + "github.com/Mrs4s/MiraiGo/client" + "github.com/Mrs4s/go-cqhttp/global" + "strconv" +) + +func convertGroupMemberInfo(groupID int64, m *client.GroupMemberInfo) global.MSG { + return global.MSG{ + "group_id": groupID, + "user_id": m.Uin, + "nickname": m.Nickname, + "card": m.CardName, + "sex": func() string { + if m.Gender == 1 { + return "female" + } else if m.Gender == 0 { + return "male" + } + // unknown = 0xff + return "unknown" + }(), + "age": 0, + "area": "", + "join_time": m.JoinTime, + "last_sent_time": m.LastSpeakTime, + "shut_up_timestamp": m.ShutUpTimestamp, + "level": strconv.FormatInt(int64(m.Level), 10), + "role": func() string { + switch m.Permission { + case client.Owner: + return "owner" + case client.Administrator: + return "admin" + case client.Member: + return "member" + default: + return "member" + } + }(), + "unfriendly": false, + "title": m.SpecialTitle, + "title_expire_time": m.SpecialTitleExpireTime, + "card_changeable": false, + } +} + +func convertGuildMemberInfo(m *client.GuildMemberInfo) global.MSG { + return global.MSG{ + "tiny_id": m.TinyId, + "title": m.Title, + "nickname": m.Nickname, + "role": m.Role, + } +} + +func convertChannelInfo(c *client.ChannelInfo) global.MSG { + slowModes := make([]global.MSG, 0, len(c.Meta.SlowModes)) + for _, mode := range c.Meta.SlowModes { + slowModes = append(slowModes, global.MSG{ + "slow_mode_key": mode.SlowModeKey, + "slow_mode_text": mode.SlowModeText, + "speak_frequency": mode.SpeakFrequency, + "slow_mode_circle": mode.SlowModeCircle, + }) + } + return global.MSG{ + "channel_id": c.ChannelId, + "channel_type": c.ChannelType, + "channel_name": c.ChannelName, + "owner_guild_id": c.Meta.GuildId, + "creator_id": c.Meta.CreatorUin, + "creator_tiny_id": c.Meta.CreatorTinyId, + "create_time": c.Meta.CreateTime, + "current_slow_mode": c.Meta.CurrentSlowMode, + "talk_permission": c.Meta.TalkPermission, + "visible_type": c.Meta.VisibleType, + "slow_modes": slowModes, + } +} diff --git a/coolq/event.go b/coolq/event.go index 990cbda..5212520 100644 --- a/coolq/event.go +++ b/coolq/event.go @@ -164,7 +164,7 @@ func (bot *CQBot) guildChannelMessageEvent(c *client.QQClient, m *message.GuildC }) } -func (bot *CQBot) guildMessageReactionsUpdated(c *client.QQClient, e *client.GuildMessageReactionsUpdatedEvent) { +func (bot *CQBot) guildMessageReactionsUpdatedEvent(c *client.QQClient, e *client.GuildMessageReactionsUpdatedEvent) { guild := c.GuildService.FindGuild(e.GuildId) if guild == nil { return @@ -198,6 +198,23 @@ func (bot *CQBot) guildMessageReactionsUpdated(c *client.QQClient, e *client.Gui }) } +func (bot *CQBot) guildChannelUpdatedEvent(c *client.QQClient, e *client.GuildChannelUpdatedEvent) { + guild := c.GuildService.FindGuild(e.GuildId) + if guild == nil { + return + } + log.Infof("频道 %v(%v) 子频道 %v(%v) 信息已更新", guild.GuildName, guild.GuildId, e.NewChannelInfo.ChannelName, e.NewChannelInfo.ChannelId) + bot.dispatchEventMessage(global.MSG{ + "post_type": "notice", + "notice_type": "channel_updated", + "guild_id": e.GuildId, + "channel_id": e.ChannelId, + "operator_id": e.OperatorId, + "old_info": convertChannelInfo(e.OldChannelInfo), + "new_info": convertChannelInfo(e.NewChannelInfo), + }) +} + func (bot *CQBot) groupMutedEvent(c *client.QQClient, e *client.GroupMuteEvent) { g := c.FindGroup(e.GroupCode) if e.TargetUin == 0 {