1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-06-07 16:19:42 +08:00

update MiraiGo & private ptt support.

This commit is contained in:
Mrs4s 2020-10-17 14:58:35 +08:00
commit 7df17f7ff2
2 changed files with 45 additions and 42 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/gob" "encoding/gob"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/syndtr/goleveldb/leveldb"
"hash/crc32" "hash/crc32"
"path" "path"
"sync" "sync"
@ -17,14 +18,13 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/xujiajun/nutsdb"
) )
type CQBot struct { type CQBot struct {
Client *client.QQClient Client *client.QQClient
events []func(MSG) events []func(MSG)
db *nutsdb.DB db *leveldb.DB
friendReqCache sync.Map friendReqCache sync.Map
invitedReqCache sync.Map invitedReqCache sync.Map
joinReqCache sync.Map joinReqCache sync.Map
@ -41,14 +41,22 @@ func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot {
Client: cli, Client: cli,
} }
if conf.EnableDB { if conf.EnableDB {
opt := nutsdb.DefaultOptions p := path.Join("data", "leveldb")
opt.Dir = path.Join("data", "db") db, err := leveldb.OpenFile(p, nil)
opt.EntryIdxMode = nutsdb.HintBPTSparseIdxMode
db, err := nutsdb.Open(opt)
if err != nil { if err != nil {
log.Fatalf("打开数据库失败, 如果频繁遇到此问题请清理 data/db 文件夹或关闭数据库功能。") log.Fatalf("打开数据库失败, 如果频繁遇到此问题请清理 data/db 文件夹或关闭数据库功能。")
} }
bot.db = 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{}) gob.Register(message.Sender{})
log.Info("信息数据库初始化完成.") log.Info("信息数据库初始化完成.")
} else { } else {
@ -103,17 +111,14 @@ func (bot *CQBot) OnEventPush(f func(m MSG)) {
func (bot *CQBot) GetGroupMessage(mid int32) MSG { func (bot *CQBot) GetGroupMessage(mid int32) MSG {
if bot.db != nil { if bot.db != nil {
m := MSG{} m := MSG{}
err := bot.db.View(func(tx *nutsdb.Tx) error { data, err := bot.db.Get(binary.ToBytes(mid), nil)
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)
})
if err == 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) 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) id := ToGlobalId(m.GroupCode, m.Id)
if bot.db != nil { if bot.db != nil {
err := bot.db.Update(func(tx *nutsdb.Tx) error { buf := new(bytes.Buffer)
buf := new(bytes.Buffer) if err := gob.NewEncoder(buf).Encode(val); err != nil {
if err := gob.NewEncoder(buf).Encode(val); err != nil { log.Warnf("记录聊天数据时出现错误: %v", err)
return err return -1
} }
return tx.Put("group-messages", binary.ToBytes(id), binary.GZipCompress(buf.Bytes()), 0) if err := bot.db.Put(binary.ToBytes(id), binary.GZipCompress(buf.Bytes()), nil); err != nil {
})
if err != nil {
log.Warnf("记录聊天数据时出现错误: %v", err) log.Warnf("记录聊天数据时出现错误: %v", err)
return -1 return -1
} }

View File

@ -567,27 +567,27 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (m messag
case "cardimage": case "cardimage":
source := d["source"] source := d["source"]
icon := d["icon"] icon := d["icon"]
minwidth, _ := strconv.ParseInt(d["minwidth"], 10, 64) minWidth, _ := strconv.ParseInt(d["minwidth"], 10, 64)
if minwidth == 0 { if minWidth == 0 {
minwidth = 200 minWidth = 200
} }
minheight, _ := strconv.ParseInt(d["minheight"], 10, 64) minHeight, _ := strconv.ParseInt(d["minheight"], 10, 64)
if minheight == 0 { if minHeight == 0 {
minheight = 200 minHeight = 200
} }
maxwidth, _ := strconv.ParseInt(d["maxwidth"], 10, 64) maxWidth, _ := strconv.ParseInt(d["maxwidth"], 10, 64)
if maxwidth == 0 { if maxWidth == 0 {
maxwidth = 500 maxWidth = 500
} }
maxheight, _ := strconv.ParseInt(d["maxheight"], 10, 64) maxHeight, _ := strconv.ParseInt(d["maxheight"], 10, 64)
if maxheight == 0 { if maxHeight == 0 {
maxheight = 1000 maxHeight = 1000
} }
img, err := bot.makeImageElem(d, group) img, err := bot.makeImageElem(d, group)
if err != nil { if err != nil {
return nil, errors.New("send cardimage faild") 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: default:
return nil, errors.New("unsupported cq code: " + t) 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 方式发送的群消息图片 //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 var xml string
xml = "" xml = ""
if i, ok := elem.(*message.ImageElement); ok { 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) log.Warnf("警告: 好友消息 %v 消息图片上传失败: %v", 1, err)
return nil, err return nil, err
} }
xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, "", gm.Md5, gm.Md5, len(i.Data), "", minwidth, minheigt, maxwidth, maxheight, source, icon) xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, "", gm.Md5, gm.Md5, len(i.Data), "", minWidth, minHeight, maxWidth, maxHeight, source, icon)
} else { } else {
gm, err := bot.Client.UploadGroupImage(1, i.Data) 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) log.Warnf("警告: 群 %v 消息图片上传失败: %v", 1, err)
return nil, err return nil, err
} }
xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, "", gm.Md5, gm.Md5, len(i.Data), "", minwidth, minheigt, maxwidth, maxheight, source, icon) xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, "", gm.Md5, gm.Md5, len(i.Data), "", minWidth, minHeight, maxWidth, maxHeight, source, icon)
} }
} }
if i, ok := elem.(*message.GroupImageElement); ok { if i, ok := elem.(*message.GroupImageElement); ok {
xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, "", i.Md5, i.Md5, 0, "", minwidth, minheigt, maxwidth, maxheight, source, icon) xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, "", i.Md5, i.Md5, 0, "", minWidth, minHeight, maxWidth, maxHeight, source, icon)
} }
if i, ok := elem.(*message.FriendImageElement); ok { if i, ok := elem.(*message.FriendImageElement); ok {
xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, "", i.Md5, i.Md5, 0, "", minwidth, minheigt, maxwidth, maxheight, source, icon) xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, "", i.Md5, i.Md5, 0, "", minWidth, minHeight, maxWidth, maxHeight, source, icon)
} }
if xml != "" { if xml != "" {
log.Warn(xml) log.Warn(xml)