diff --git a/coolq/api.go b/coolq/api.go index 827b90e..2c6c786 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -131,7 +131,7 @@ func (bot *CQBot) CQGetGuildChannelList(guildID uint64, noCache bool) global.MSG if noCache { channels, err := bot.Client.GuildService.FetchChannelList(guildID) if err != nil { - log.Errorf("获取频道 %v 子频道列表时出现错误: %v", guildID, err) + log.Warnf("获取频道 %v 子频道列表时出现错误: %v", guildID, err) return Failed(100, "API_ERROR", err.Error()) } guild.Channels = channels @@ -195,7 +195,7 @@ func (bot *CQBot) CQGetGuildMemberProfile(guildID, userID uint64) global.MSG { } profile, err := bot.Client.GuildService.FetchGuildMemberProfileInfo(guildID, userID) if err != nil { - log.Errorf("获取频道 %v 成员 %v 资料时出现错误: %v", guildID, userID, err) + log.Warnf("获取频道 %v 成员 %v 资料时出现错误: %v", guildID, userID, err) return Failed(100, "API_ERROR", err.Error()) } roles := make([]global.MSG, 0, len(profile.Roles)) @@ -219,7 +219,7 @@ func (bot *CQBot) CQGetGuildMemberProfile(guildID, userID uint64) global.MSG { func (bot *CQBot) CQGetGuildRoles(guildID uint64) global.MSG { r, err := bot.Client.GuildService.GetGuildRoles(guildID) if err != nil { - log.Errorf("获取频道 %v 角色列表时出现错误: %v", guildID, err) + log.Warnf("获取频道 %v 角色列表时出现错误: %v", guildID, err) return Failed(100, "API_ERROR", err.Error()) } roles := make([]global.MSG, len(r)) @@ -249,7 +249,7 @@ func (bot *CQBot) CQCreateGuildRole(guildID uint64, name string, color uint32, i } role, err := bot.Client.GuildService.CreateGuildRole(guildID, name, color, independent, userSlice) if err != nil { - log.Errorf("创建频道 %v 角色时出现错误: %v", guildID, err) + log.Warnf("创建频道 %v 角色时出现错误: %v", guildID, err) return Failed(100, "API_ERROR", err.Error()) } return OK(global.MSG{ @@ -262,7 +262,7 @@ func (bot *CQBot) CQCreateGuildRole(guildID uint64, name string, color uint32, i func (bot *CQBot) CQDeleteGuildRole(guildID uint64, roleID uint64) global.MSG { err := bot.Client.GuildService.DeleteGuildRole(guildID, roleID) if err != nil { - log.Errorf("删除频道 %v 角色时出现错误: %v", guildID, err) + log.Warnf("删除频道 %v 角色时出现错误: %v", guildID, err) return Failed(100, "API_ERROR", err.Error()) } return OK(nil) @@ -279,7 +279,7 @@ func (bot *CQBot) CQSetGuildMemberRole(guildID uint64, set bool, roleID uint64, } err := bot.Client.GuildService.SetUserRoleInGuild(guildID, set, roleID, userSlice) if err != nil { - log.Errorf("设置用户在频道 %v 中的角色时出现错误: %v", guildID, err) + log.Warnf("设置用户在频道 %v 中的角色时出现错误: %v", guildID, err) return Failed(100, "API_ERROR", err.Error()) } return OK(nil) @@ -290,7 +290,7 @@ func (bot *CQBot) CQSetGuildMemberRole(guildID uint64, set bool, roleID uint64, func (bot *CQBot) CQModifyRoleInGuild(guildID uint64, roleID uint64, name string, color uint32, indepedent bool) global.MSG { err := bot.Client.GuildService.ModifyRoleInGuild(guildID, roleID, name, color, indepedent) if err != nil { - log.Errorf("修改频道 %v 角色时出现错误: %v", guildID, err) + log.Warnf("修改频道 %v 角色时出现错误: %v", guildID, err) return Failed(100, "API_ERROR", err.Error()) } return OK(nil) @@ -312,7 +312,7 @@ func (bot *CQBot) CQGetTopicChannelFeeds(guildID, channelID uint64) global.MSG { } feeds, err := bot.Client.GuildService.GetTopicChannelFeeds(guildID, channelID) if err != nil { - log.Errorf("获取频道 %v 帖子时出现错误: %v", channelID, err) + log.Warnf("获取频道 %v 帖子时出现错误: %v", channelID, err) return Failed(100, "API_ERROR", err.Error()) } c := make([]global.MSG, 0, len(feeds)) @@ -344,7 +344,7 @@ func (bot *CQBot) CQGetFriendList() global.MSG { func (bot *CQBot) CQGetUnidirectionalFriendList() global.MSG { list, err := bot.Client.GetUnidirectionalFriendList() if err != nil { - log.Errorf("获取单向好友列表时出现错误: %v", err) + log.Warnf("获取单向好友列表时出现错误: %v", err) return Failed(100, "API_ERROR", err.Error()) } fs := make([]global.MSG, 0, len(list)) @@ -365,13 +365,13 @@ func (bot *CQBot) CQGetUnidirectionalFriendList() global.MSG { func (bot *CQBot) CQDeleteUnidirectionalFriend(uin int64) global.MSG { list, err := bot.Client.GetUnidirectionalFriendList() if err != nil { - log.Errorf("获取单向好友列表时出现错误: %v", err) + log.Warnf("获取单向好友列表时出现错误: %v", err) return Failed(100, "API_ERROR", err.Error()) } for _, f := range list { if f.Uin == uin { if err = bot.Client.DeleteUnidirectionalFriend(uin); err != nil { - log.Errorf("删除单向好友时出现错误: %v", err) + log.Warnf("删除单向好友时出现错误: %v", err) return Failed(100, "API_ERROR", err.Error()) } return OK(nil) @@ -388,7 +388,7 @@ func (bot *CQBot) CQDeleteFriend(uin int64) global.MSG { return Failed(100, "FRIEND_NOT_FOUND", "好友不存在") } if err := bot.Client.DeleteFriend(uin); err != nil { - log.Errorf("删除好友时出现错误: %v", err) + log.Warnf("删除好友时出现错误: %v", err) return Failed(100, "DELETE_API_ERROR", err.Error()) } return OK(nil) @@ -516,7 +516,7 @@ func (bot *CQBot) CQGetGroupMemberInfo(groupID, userID int64, noCache bool) glob func (bot *CQBot) CQGetGroupFileSystemInfo(groupID int64) global.MSG { fs, err := bot.Client.GetGroupFileSystem(groupID) if err != nil { - log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err) + log.Warnf("获取群 %v 文件系统信息失败: %v", groupID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } return OK(fs) @@ -529,12 +529,12 @@ func (bot *CQBot) CQGetGroupFileSystemInfo(groupID int64) global.MSG { func (bot *CQBot) CQGetGroupRootFiles(groupID int64) global.MSG { fs, err := bot.Client.GetGroupFileSystem(groupID) if err != nil { - log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err) + log.Warnf("获取群 %v 文件系统信息失败: %v", groupID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } files, folders, err := fs.Root() if err != nil { - log.Errorf("获取群 %v 根目录文件失败: %v", groupID, err) + log.Warnf("获取群 %v 根目录文件失败: %v", groupID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } return OK(global.MSG{ @@ -550,12 +550,12 @@ func (bot *CQBot) CQGetGroupRootFiles(groupID int64) global.MSG { func (bot *CQBot) CQGetGroupFilesByFolderID(groupID int64, folderID string) global.MSG { fs, err := bot.Client.GetGroupFileSystem(groupID) if err != nil { - log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err) + log.Warnf("获取群 %v 文件系统信息失败: %v", groupID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } files, folders, err := fs.GetFilesByFolder(folderID) if err != nil { - log.Errorf("获取群 %v 根目录 %v 子文件失败: %v", groupID, folderID, err) + log.Warnf("获取群 %v 根目录 %v 子文件失败: %v", groupID, folderID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } return OK(global.MSG{ @@ -584,19 +584,19 @@ func (bot *CQBot) CQGetGroupFileURL(groupID int64, fileID string, busID int32) g // @route(upload_group_file) func (bot *CQBot) CQUploadGroupFile(groupID int64, file, name, folder string) global.MSG { if !global.PathExists(file) { - log.Errorf("上传群文件 %v 失败: 文件不存在", file) + log.Warnf("上传群文件 %v 失败: 文件不存在", file) return Failed(100, "FILE_NOT_FOUND", "文件不存在") } fs, err := bot.Client.GetGroupFileSystem(groupID) if err != nil { - log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err) + log.Warnf("获取群 %v 文件系统信息失败: %v", groupID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } if folder == "" { folder = "/" } if err = fs.UploadFile(file, name, folder); err != nil { - log.Errorf("上传群 %v 文件 %v 失败: %v", groupID, file, err) + log.Warnf("上传群 %v 文件 %v 失败: %v", groupID, file, err) return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error()) } return OK(nil) @@ -608,11 +608,11 @@ func (bot *CQBot) CQUploadGroupFile(groupID int64, file, name, folder string) gl func (bot *CQBot) CQGroupFileCreateFolder(groupID int64, parentID, name string) global.MSG { fs, err := bot.Client.GetGroupFileSystem(groupID) if err != nil { - log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err) + log.Warnf("获取群 %v 文件系统信息失败: %v", groupID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } if err = fs.CreateFolder(parentID, name); err != nil { - log.Errorf("创建群 %v 文件夹失败: %v", groupID, err) + log.Warnf("创建群 %v 文件夹失败: %v", groupID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } return OK(nil) @@ -625,11 +625,11 @@ func (bot *CQBot) CQGroupFileCreateFolder(groupID int64, parentID, name string) func (bot *CQBot) CQGroupFileDeleteFolder(groupID int64, id string) global.MSG { fs, err := bot.Client.GetGroupFileSystem(groupID) if err != nil { - log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err) + log.Warnf("获取群 %v 文件系统信息失败: %v", groupID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } if err = fs.DeleteFolder(id); err != nil { - log.Errorf("删除群 %v 文件夹 %v 时出现文件: %v", groupID, id, err) + log.Warnf("删除群 %v 文件夹 %v 时出现文件: %v", groupID, id, err) return Failed(200, "FILE_SYSTEM_API_ERROR", err.Error()) } return OK(nil) @@ -642,11 +642,11 @@ func (bot *CQBot) CQGroupFileDeleteFolder(groupID int64, id string) global.MSG { func (bot *CQBot) CQGroupFileDeleteFile(groupID int64, id string, busID int32) global.MSG { fs, err := bot.Client.GetGroupFileSystem(groupID) if err != nil { - log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err) + log.Warnf("获取群 %v 文件系统信息失败: %v", groupID, err) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) } if res := fs.DeleteFile("", id, busID); res != "" { - log.Errorf("删除群 %v 文件 %v 时出现文件: %v", groupID, id, res) + log.Warnf("删除群 %v 文件 %v 时出现文件: %v", groupID, id, res) return Failed(200, "FILE_SYSTEM_API_ERROR", res) } return OK(nil) @@ -1125,14 +1125,14 @@ func (bot *CQBot) CQProcessFriendRequest(flag string, approve bool) global.MSG { func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bool) global.MSG { msgs, err := bot.Client.GetGroupSystemMessages() if err != nil { - log.Errorf("获取群系统消息失败: %v", err) + log.Warnf("获取群系统消息失败: %v", err) return Failed(100, "SYSTEM_MSG_API_ERROR", err.Error()) } if subType == "add" { for _, req := range msgs.JoinRequests { if strconv.FormatInt(req.RequestId, 10) == flag { if req.Checked { - log.Errorf("处理群系统消息失败: 无法操作已处理的消息.") + log.Warnf("处理群系统消息失败: 无法操作已处理的消息.") return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理") } if approve { @@ -1147,7 +1147,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo for _, req := range msgs.InvitedRequests { if strconv.FormatInt(req.RequestId, 10) == flag { if req.Checked { - log.Errorf("处理群系统消息失败: 无法操作已处理的消息.") + log.Warnf("处理群系统消息失败: 无法操作已处理的消息.") return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理") } if approve { @@ -1159,7 +1159,7 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo } } } - log.Errorf("处理群系统消息失败: 消息 %v 不存在.", flag) + log.Warnf("处理群系统消息失败: 消息 %v 不存在.", flag) return Failed(100, "FLAG_NOT_FOUND", "FLAG不存在") } @@ -1566,24 +1566,64 @@ func (bot *CQBot) CQGetMessage(messageID int32) global.MSG { // CQGetGuildMessage 获取频道消息 // @route(get_guild_msg) -func (bot *CQBot) CQGetGuildMessage(messageID string) global.MSG { - msg, err := db.GetGuildChannelMessageByID(messageID) - if err != nil { - log.Warnf("获取消息时出现错误: %v", err) - return Failed(100, "MSG_NOT_FOUND", "消息不存在") +func (bot *CQBot) CQGetGuildMessage(messageID string, noCache bool) global.MSG { + source, seq := decodeGuildMessageID(messageID) + if source == nil { + log.Warnf("获取消息时出现错误: 无效消息ID") + return Failed(100, "INVALID_MESSAGE_ID", "无效消息ID") } m := global.MSG{ - "message_id": msg.ID, - "message_source": "channel", - "guild_id": fU64(msg.GuildID), - "channel_id": fU64(msg.ChannelID), - "message_seq": msg.Attribute.MessageSeq, - "sender": global.MSG{ - "user_id": fU64(msg.Attribute.SenderTinyID), - "nickname": msg.Attribute.SenderName, - }, - "time": msg.Attribute.Timestamp, - "message": ToFormattedMessage(bot.ConvertContentMessage(msg.Content, MessageSourceGuildChannel), MessageSource{SourceType: MessageSourceGuildChannel, PrimaryID: msg.GuildID, SubID: msg.ChannelID}), + "message_id": messageID, + "message_source": func() string { + if source.SourceType == MessageSourceGuildDirect { + return "direct" + } + return "channel" + }(), + "message_seq": seq, + "guild_id": fU64(source.PrimaryID), + "reactions": []int{}, + } + switch source.SourceType { + case MessageSourceGuildChannel: + m["channel_id"] = fU64(source.SubID) + if noCache { + pull, err := bot.Client.GuildService.PullGuildChannelMessage(source.PrimaryID, source.SubID, seq, seq) + if err != nil { + log.Warnf("获取消息时出现错误: %v", err) + return Failed(100, "API_ERROR", err.Error()) + } + if len(m) == 0 { + log.Warnf("获取消息时出现错误: 消息不存在") + return Failed(100, "MSG_NOT_FOUND", "消息不存在") + } + m["time"] = pull[0].Time + m["sender"] = global.MSG{ + "user_id": pull[0].Sender.TinyId, + "tiny_id": fU64(pull[0].Sender.TinyId), + "nickname": pull[0].Sender.Nickname, + } + m["message"] = ToFormattedMessage(pull[0].Elements, *source, false) + m["reactions"] = convertReactions(pull[0].Reactions) + bot.InsertGuildChannelMessage(pull[0]) + return OK(m) + } else { + channelMsgByDB, err := db.GetGuildChannelMessageByID(messageID) + if err != nil { + log.Warnf("获取消息时出现错误: %v", err) + return Failed(100, "MSG_NOT_FOUND", "消息不存在") + } + m["time"] = channelMsgByDB.Attribute.Timestamp + m["sender"] = global.MSG{ + "user_id": channelMsgByDB.Attribute.SenderTinyID, + "tiny_id": fU64(channelMsgByDB.Attribute.SenderTinyID), + "nickname": channelMsgByDB.Attribute.SenderName, + } + m["message"] = ToFormattedMessage(bot.ConvertContentMessage(channelMsgByDB.Content, MessageSourceGuildChannel), *source) + } + case MessageSourceGuildDirect: + // todo(mrs4s): 支持 direct 消息 + m["tiny_id"] = fU64(source.SubID) } return OK(m) } diff --git a/coolq/bot.go b/coolq/bot.go index ce9d809..b190049 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -519,7 +519,7 @@ func (bot *CQBot) InsertTempMessage(target int64, m *message.TempMessage) int32 // InsertGuildChannelMessage 频道消息入数据库 func (bot *CQBot) InsertGuildChannelMessage(m *message.GuildChannelMessage) string { - id := encodeGuildMessageID(m.GuildId, m.ChannelId, m.Id) + id := encodeGuildMessageID(m.GuildId, m.ChannelId, m.Id, MessageSourceGuildChannel) msg := &db.StoredGuildChannelMessage{ ID: id, Attribute: &db.StoredGuildMessageAttribute{ @@ -610,10 +610,29 @@ func encodeMessageID(target int64, seq int32) string { })) } -func encodeGuildMessageID(guildID, channelID, seq uint64) string { +// encodeGuildMessageID 将频道信息编码为字符串 +// 当信息来源为 Channel 时 primaryID 为 guildID , subID 为 channelID +// 当信息来源为 Direct 时 primaryID 为 guildID , subID 为 tinyID +func encodeGuildMessageID(primaryID, subID, seq uint64, source MessageSourceType) string { return base64.StdEncoding.EncodeToString(binary.NewWriterF(func(w *binary.Writer) { - w.WriteUInt64(guildID) - w.WriteUInt64(channelID) + w.WriteByte(byte(source)) + w.WriteUInt64(primaryID) + w.WriteUInt64(subID) w.WriteUInt64(seq) })) } + +func decodeGuildMessageID(id string) (source *MessageSource, seq uint64) { + b, _ := base64.StdEncoding.DecodeString(id) + if len(b) < 25 { + return + } + r := binary.NewReader(b) + source = &MessageSource{ + SourceType: MessageSourceType(r.ReadByte()), + PrimaryID: uint64(r.ReadInt64()), + SubID: uint64(r.ReadInt64()), + } + seq = uint64(r.ReadInt64()) + return +} diff --git a/coolq/converter.go b/coolq/converter.go index a62bce8..44bc17c 100644 --- a/coolq/converter.go +++ b/coolq/converter.go @@ -205,6 +205,21 @@ func convertChannelFeedInfo(f *topic.Feed) global.MSG { return m } +func convertReactions(reactions []*message.GuildMessageEmojiReaction) (r []global.MSG) { + r = make([]global.MSG, len(reactions)) + for i, re := range reactions { + r[i] = global.MSG{ + "emoji_id": re.EmojiId, + "emoji_index": re.Face.Index, + "emoji_type": re.EmojiType, + "emoji_name": re.Face.Name, + "count": re.Count, + "clicked": re.Clicked, + } + } + return +} + func fU64(v uint64) string { return strconv.FormatUint(v, 10) } diff --git a/coolq/cqcode.go b/coolq/cqcode.go index c142036..84b2a13 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -72,13 +72,14 @@ type MessageSource struct { } // MessageSourceType 消息来源类型 -type MessageSourceType int32 +type MessageSourceType byte // MessageSourceType 常量 const ( MessageSourcePrivate MessageSourceType = 0 MessageSourceGroup MessageSourceType = 1 MessageSourceGuildChannel MessageSourceType = 2 + MessageSourceGuildDirect MessageSourceType = 3 ) const ( diff --git a/coolq/event.go b/coolq/event.go index 450a5c4..0199bf6 100644 --- a/coolq/event.go +++ b/coolq/event.go @@ -169,7 +169,8 @@ func (bot *CQBot) guildChannelMessageEvent(c *client.QQClient, m *message.GuildC "self_tiny_id": fU64(bot.Client.GuildService.TinyId), "time": m.Time, "sender": global.MSG{ - "user_id": fU64(m.Sender.TinyId), + "user_id": m.Sender.TinyId, + "tiny_id": fU64(m.Sender.TinyId), "nickname": m.Sender.Nickname, }, }) @@ -180,7 +181,7 @@ func (bot *CQBot) guildMessageReactionsUpdatedEvent(c *client.QQClient, e *clien if guild == nil { return } - msgID := encodeGuildMessageID(e.GuildId, e.ChannelId, e.MessageId) + msgID := encodeGuildMessageID(e.GuildId, e.ChannelId, e.MessageId, MessageSourceGuildChannel) str := fmt.Sprintf("频道 %v(%v) 消息 %v 表情贴片已更新: ", guild.GuildName, guild.GuildId, msgID) currentReactions := make([]global.MSG, len(e.CurrentReactions)) for i, r := range e.CurrentReactions { @@ -199,18 +200,17 @@ func (bot *CQBot) guildMessageReactionsUpdatedEvent(c *client.QQClient, e *clien } log.Infof(str) bot.dispatchEventMessage(global.MSG{ - "post_type": "notice", - "notice_type": "message_reactions_updated", - "message_sender_uin": e.MessageSenderUin, - "guild_id": fU64(e.GuildId), - "channel_id": fU64(e.ChannelId), - "message_id": msgID, - "operator_id": fU64(e.OperatorId), - "current_reactions": currentReactions, - "time": time.Now().Unix(), - "self_id": bot.Client.Uin, - "self_tiny_id": fU64(bot.Client.GuildService.TinyId), - "user_id": e.OperatorId, + "post_type": "notice", + "notice_type": "message_reactions_updated", + "guild_id": fU64(e.GuildId), + "channel_id": fU64(e.ChannelId), + "message_id": msgID, + "operator_id": fU64(e.OperatorId), + "current_reactions": currentReactions, + "time": time.Now().Unix(), + "self_id": bot.Client.Uin, + "self_tiny_id": fU64(bot.Client.GuildService.TinyId), + "user_id": e.OperatorId, }) } @@ -228,7 +228,7 @@ func (bot *CQBot) guildChannelMessageRecalledEvent(c *client.QQClient, e *client log.Errorf("处理频道撤回事件时出现错误: 获取操作者资料时出现错误 %v", err) return } - msgID := encodeGuildMessageID(e.GuildId, e.ChannelId, e.MessageId) + msgID := encodeGuildMessageID(e.GuildId, e.ChannelId, e.MessageId, MessageSourceGuildChannel) log.Infof("用户 %v(%v) 撤回了频道 %v(%v) 子频道 %v(%v) 的消息 %v", operator.Nickname, operator.TinyId, guild.GuildName, guild.GuildId, channel.ChannelName, channel.ChannelId, msgID) bot.dispatchEventMessage(global.MSG{ "post_type": "notice", diff --git a/db/mongodb/mongodb.go b/db/mongodb/mongodb.go index 9528743..9c8449d 100644 --- a/db/mongodb/mongodb.go +++ b/db/mongodb/mongodb.go @@ -84,18 +84,18 @@ func (m *MongoDBImpl) GetGuildChannelMessageByID(id string) (*db.StoredGuildChan func (m *MongoDBImpl) InsertGroupMessage(msg *db.StoredGroupMessage) error { coll := m.mongo.Collection(MongoGroupMessageCollection) - _, err := coll.InsertOne(context.Background(), msg) + _, err := coll.UpdateOne(context.Background(), bson.D{{"_id", msg.ID}}, bson.D{{"$set", msg}}, options.Update().SetUpsert(true)) return errors.Wrap(err, "insert error") } func (m *MongoDBImpl) InsertPrivateMessage(msg *db.StoredPrivateMessage) error { coll := m.mongo.Collection(MongoPrivateMessageCollection) - _, err := coll.InsertOne(context.Background(), msg) + _, err := coll.UpdateOne(context.Background(), bson.D{{"_id", msg.ID}}, bson.D{{"$set", msg}}, options.Update().SetUpsert(true)) return errors.Wrap(err, "insert error") } func (m *MongoDBImpl) InsertGuildChannelMessage(msg *db.StoredGuildChannelMessage) error { coll := m.mongo.Collection(MongoGuildChannelMessageCollection) - _, err := coll.InsertOne(context.Background(), msg) + _, err := coll.UpdateOne(context.Background(), bson.D{{"_id", msg.ID}}, bson.D{{"$set", msg}}, options.Update().SetUpsert(true)) return errors.Wrap(err, "insert error") } diff --git a/docs/guild.md b/docs/guild.md index b3d2d1a..e6a2ed2 100644 --- a/docs/guild.md +++ b/docs/guild.md @@ -238,6 +238,8 @@ RoleInfo: | `sender` | Sender | | 发送者 | | `message` | Message | | 消息内容 | +> 注: 此处的 `Sender` 对象为保证一致性, `user_id` 为 `uint64` 类型, 并添加了 `string` 类型的 `tiny_id` 字段 + ### 频道消息表情贴更新 **上报数据** diff --git a/go.mod b/go.mod index 9b6cba8..c159c7a 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f github.com/Microsoft/go-winio v0.5.1 - github.com/Mrs4s/MiraiGo v0.0.0-20211212174229-5d7cb85b1ed4 + github.com/Mrs4s/MiraiGo v0.0.0-20211214084325-ed979508cf8a github.com/dustin/go-humanize v1.0.0 github.com/fumiama/go-hide-param v0.1.4 github.com/gabriel-vasile/mimetype v1.4.0 diff --git a/go.sum b/go.sum index c31c289..06f5a9e 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,8 @@ github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f/g github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY= github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84= -github.com/Mrs4s/MiraiGo v0.0.0-20211212174229-5d7cb85b1ed4 h1:rtUcMuZ8aMspm7Ahgw6wVXHAT+mmHU8Fe0hfmKMxks0= -github.com/Mrs4s/MiraiGo v0.0.0-20211212174229-5d7cb85b1ed4/go.mod h1:YD9gBKkxC9lPPtx3doYXRG26VBkK6YXjrS76cv01C5w= +github.com/Mrs4s/MiraiGo v0.0.0-20211214084325-ed979508cf8a h1:hADFsj9XtB+A2AZAf99vMW870YpzjPDA30+mqCXJDCo= +github.com/Mrs4s/MiraiGo v0.0.0-20211214084325-ed979508cf8a/go.mod h1:YD9gBKkxC9lPPtx3doYXRG26VBkK6YXjrS76cv01C5w= github.com/RomiChan/protobuf v0.0.0-20211204042931-ff4f35848737 h1:p4o7/eSoP39jwnGZz08N1IpH/mNzg9SdCn7kPM9A9BE= github.com/RomiChan/protobuf v0.0.0-20211204042931-ff4f35848737/go.mod h1:CKKOWC7mBxd36zxsCB1V8DTrwlTNRQvkSVbYqyUiGEE= github.com/bits-and-blooms/bitset v1.2.1 h1:M+/hrU9xlMp7t4TyTDQW97d3tRPVuKFC6zBEK16QnXY= diff --git a/modules/api/api.go b/modules/api/api.go index 8ba0cec..1eaee62 100644 --- a/modules/api/api.go +++ b/modules/api/api.go @@ -153,7 +153,8 @@ func (c *Caller) call(action string, p Getter) global.MSG { return c.bot.CQGetGuildMetaByGuest(p0) case "get_guild_msg": p0 := p.Get("message_id").String() - return c.bot.CQGetGuildMessage(p0) + p1 := p.Get("no_cache").Bool() + return c.bot.CQGetGuildMessage(p0, p1) case "get_guild_roles": p0 := p.Get("guild_id").Uint() return c.bot.CQGetGuildRoles(p0)