diff --git a/coolq/bot.go b/coolq/bot.go index 3152fe4..40cff0a 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -5,6 +5,7 @@ import ( "encoding/gob" "encoding/json" "fmt" + "github.com/syndtr/goleveldb/leveldb" "hash/crc32" "path" "sync" @@ -17,14 +18,13 @@ import ( log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" - "github.com/xujiajun/nutsdb" ) type CQBot struct { Client *client.QQClient events []func(MSG) - db *nutsdb.DB + db *leveldb.DB friendReqCache sync.Map invitedReqCache sync.Map joinReqCache sync.Map @@ -41,14 +41,22 @@ func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot { Client: cli, } if conf.EnableDB { - opt := nutsdb.DefaultOptions - opt.Dir = path.Join("data", "db") - opt.EntryIdxMode = nutsdb.HintBPTSparseIdxMode - db, err := nutsdb.Open(opt) + p := path.Join("data", "leveldb") + db, err := leveldb.OpenFile(p, nil) if err != nil { log.Fatalf("打开数据库失败, 如果频繁遇到此问题请清理 data/db 文件夹或关闭数据库功能。") } bot.db = db + /* + opt := nutsdb.DefaultOptions + opt.Dir = path.Join("data", "db") + opt.EntryIdxMode = nutsdb.HintBPTSparseIdxMode + db, err := nutsdb.Open(opt) + if err != nil { + log.Fatalf("打开数据库失败, 如果频繁遇到此问题请清理 data/db 文件夹或关闭数据库功能。") + } + bot.db = db + */ gob.Register(message.Sender{}) log.Info("信息数据库初始化完成.") } else { @@ -103,17 +111,14 @@ func (bot *CQBot) OnEventPush(f func(m MSG)) { func (bot *CQBot) GetGroupMessage(mid int32) MSG { if bot.db != nil { m := MSG{} - err := bot.db.View(func(tx *nutsdb.Tx) error { - e, err := tx.Get("group-messages", binary.ToBytes(mid)) - if err != nil { - return err - } - buff := new(bytes.Buffer) - buff.Write(binary.GZipUncompress(e.Value)) - return gob.NewDecoder(buff).Decode(&m) - }) + data, err := bot.db.Get(binary.ToBytes(mid), nil) if err == nil { - return m + buff := new(bytes.Buffer) + buff.Write(binary.GZipUncompress(data)) + err = gob.NewDecoder(buff).Decode(&m) + if err == nil { + return m + } } log.Warnf("获取信息时出现错误: %v id: %v", err, mid) } @@ -299,14 +304,12 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 { } id := ToGlobalId(m.GroupCode, m.Id) if bot.db != nil { - err := bot.db.Update(func(tx *nutsdb.Tx) error { - buf := new(bytes.Buffer) - if err := gob.NewEncoder(buf).Encode(val); err != nil { - return err - } - return tx.Put("group-messages", binary.ToBytes(id), binary.GZipCompress(buf.Bytes()), 0) - }) - if err != nil { + buf := new(bytes.Buffer) + if err := gob.NewEncoder(buf).Encode(val); err != nil { + log.Warnf("记录聊天数据时出现错误: %v", err) + return -1 + } + if err := bot.db.Put(binary.ToBytes(id), binary.GZipCompress(buf.Bytes()), nil); err != nil { log.Warnf("记录聊天数据时出现错误: %v", err) return -1 } diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 13e77aa..c3d0748 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -567,27 +567,27 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (m messag case "cardimage": source := d["source"] icon := d["icon"] - minwidth, _ := strconv.ParseInt(d["minwidth"], 10, 64) - if minwidth == 0 { - minwidth = 200 + minWidth, _ := strconv.ParseInt(d["minwidth"], 10, 64) + if minWidth == 0 { + minWidth = 200 } - minheight, _ := strconv.ParseInt(d["minheight"], 10, 64) - if minheight == 0 { - minheight = 200 + minHeight, _ := strconv.ParseInt(d["minheight"], 10, 64) + if minHeight == 0 { + minHeight = 200 } - maxwidth, _ := strconv.ParseInt(d["maxwidth"], 10, 64) - if maxwidth == 0 { - maxwidth = 500 + maxWidth, _ := strconv.ParseInt(d["maxwidth"], 10, 64) + if maxWidth == 0 { + maxWidth = 500 } - maxheight, _ := strconv.ParseInt(d["maxheight"], 10, 64) - if maxheight == 0 { - maxheight = 1000 + maxHeight, _ := strconv.ParseInt(d["maxheight"], 10, 64) + if maxHeight == 0 { + maxHeight = 1000 } img, err := bot.makeImageElem(d, group) if err != nil { return nil, errors.New("send cardimage faild") } - return bot.SendNewPic(img, source, icon, minwidth, minheight, maxwidth, maxheight, group) + return bot.SendNewPic(img, source, icon, minWidth, minHeight, maxWidth, maxHeight, group) default: return nil, errors.New("unsupported cq code: " + t) } @@ -737,7 +737,7 @@ func (bot *CQBot) makeImageElem(d map[string]string, group bool) (message.IMessa } //SendNewPic 一种xml 方式发送的群消息图片 -func (bot *CQBot) SendNewPic(elem message.IMessageElement, source string, icon string, minwidth int64, minheigt int64, maxwidth int64, maxheight int64, group bool) (*message.ServiceElement, error) { +func (bot *CQBot) SendNewPic(elem message.IMessageElement, source string, icon string, minWidth int64, minHeight int64, maxWidth int64, maxHeight int64, group bool) (*message.ServiceElement, error) { var xml string xml = "" if i, ok := elem.(*message.ImageElement); ok { @@ -747,7 +747,7 @@ func (bot *CQBot) SendNewPic(elem message.IMessageElement, source string, icon s log.Warnf("警告: 好友消息 %v 消息图片上传失败: %v", 1, err) return nil, err } - xml = fmt.Sprintf(``, "", gm.Md5, gm.Md5, len(i.Data), "", minwidth, minheigt, maxwidth, maxheight, source, icon) + xml = fmt.Sprintf(``, "", gm.Md5, gm.Md5, len(i.Data), "", minWidth, minHeight, maxWidth, maxHeight, source, icon) } else { gm, err := bot.Client.UploadGroupImage(1, i.Data) @@ -755,14 +755,14 @@ func (bot *CQBot) SendNewPic(elem message.IMessageElement, source string, icon s log.Warnf("警告: 群 %v 消息图片上传失败: %v", 1, err) return nil, err } - xml = fmt.Sprintf(``, "", gm.Md5, gm.Md5, len(i.Data), "", minwidth, minheigt, maxwidth, maxheight, source, icon) + xml = fmt.Sprintf(``, "", gm.Md5, gm.Md5, len(i.Data), "", minWidth, minHeight, maxWidth, maxHeight, source, icon) } } if i, ok := elem.(*message.GroupImageElement); ok { - xml = fmt.Sprintf(``, "", i.Md5, i.Md5, 0, "", minwidth, minheigt, maxwidth, maxheight, source, icon) + xml = fmt.Sprintf(``, "", i.Md5, i.Md5, 0, "", minWidth, minHeight, maxWidth, maxHeight, source, icon) } if i, ok := elem.(*message.FriendImageElement); ok { - xml = fmt.Sprintf(``, "", i.Md5, i.Md5, 0, "", minwidth, minheigt, maxwidth, maxheight, source, icon) + xml = fmt.Sprintf(``, "", i.Md5, i.Md5, 0, "", minWidth, minHeight, maxWidth, maxHeight, source, icon) } if xml != "" { log.Warn(xml)