diff --git a/coolq/api.go b/coolq/api.go index d20f376..1350ee5 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -439,7 +439,7 @@ func (bot *CQBot) CQGetForwardMessage(resId string) MSG { } var r []MSG for _, n := range m.Nodes { - checkImage(n.Message) + checkMedia(n.Message) r = append(r, MSG{ "sender": MSG{ "user_id": n.SenderId, diff --git a/coolq/cqcode.go b/coolq/cqcode.go index bdbfbbf..e4d5b11 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -45,7 +45,11 @@ func ToStringMessage(e []message.IMessageElement, code int64, raw ...bool) (r st case *message.FaceElement: r += fmt.Sprintf(`[CQ:face,id=%d]`, o.Index) case *message.VoiceElement: - r += fmt.Sprintf(`[CQ:record,file=%s]`, o.Name) + if ur { + r += fmt.Sprintf(`[CQ:record,file=%s]`, o.Name) + } else { + r += fmt.Sprintf(`[CQ:record,file=%s,url=%s]`, o.Name, o.Url) + } case *message.ImageElement: if ur { r += fmt.Sprintf(`[CQ:image,file=%s]`, o.Filename) @@ -286,6 +290,13 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message. } data = b } + if global.PathExists(path.Join(global.VOICE_PATH, f)) { + b, err := ioutil.ReadFile(path.Join(global.VOICE_PATH, f)) + if err != nil { + return nil, err + } + data = b + } if !global.IsAMR(data) { return nil, errors.New("unsupported voice file format (please use AMR file for now)") } diff --git a/coolq/event.go b/coolq/event.go index 1a21374..7838ea3 100644 --- a/coolq/event.go +++ b/coolq/event.go @@ -10,11 +10,12 @@ import ( "io/ioutil" "path" "strconv" + "strings" "time" ) func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMessage) { - checkImage(m.Elements) + checkMedia(m.Elements) cqm := ToStringMessage(m.Elements, 0, true) log.Infof("收到好友 %v(%v) 的消息: %v", m.Sender.DisplayName(), m.Sender.Uin, cqm) fm := MSG{ @@ -39,7 +40,7 @@ func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMess } func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage) { - checkImage(m.Elements) + checkMedia(m.Elements) for _, elem := range m.Elements { if file, ok := elem.(*message.GroupFileElement); ok { log.Infof("群 %v(%v) 内 %v(%v) 上传了文件: %v", m.GroupName, m.GroupCode, m.Sender.DisplayName(), m.Sender.Uin, file.Name) @@ -117,7 +118,7 @@ func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage) } func (bot *CQBot) tempMessageEvent(c *client.QQClient, m *message.TempMessage) { - checkImage(m.Elements) + checkMedia(m.Elements) cqm := ToStringMessage(m.Elements, 0, true) bot.tempMsgCache.Store(m.Sender.Uin, m.GroupCode) log.Infof("收到来自群 %v(%v) 内 %v(%v) 的临时会话消息: %v", m.GroupName, m.GroupCode, m.Sender.DisplayName(), m.Sender.Uin, cqm) @@ -346,9 +347,10 @@ func (bot *CQBot) groupDecrease(groupCode, userUin int64, operator *client.Group } } -func checkImage(e []message.IMessageElement) { +func checkMedia(e []message.IMessageElement) { for _, elem := range e { - if i, ok := elem.(*message.ImageElement); ok { + switch i := elem.(type) { + case *message.ImageElement: filename := hex.EncodeToString(i.Md5) + ".image" if !global.PathExists(path.Join(global.IMAGE_PATH, filename)) { _ = ioutil.WriteFile(path.Join(global.IMAGE_PATH, filename), binary.NewWriterF(func(w *binary.Writer) { @@ -359,6 +361,17 @@ func checkImage(e []message.IMessageElement) { }), 0777) } i.Filename = filename + case *message.VoiceElement: + i.Name = strings.ReplaceAll(i.Name, "{", "") + i.Name = strings.ReplaceAll(i.Name, "}", "") + if !global.PathExists(path.Join(global.VOICE_PATH, i.Name)) { + b, err := global.GetBytes(i.Url) + if err != nil { + log.Warnf("语音文件 %v 下载失败: %v", i.Name, err) + continue + } + _ = ioutil.WriteFile(path.Join(global.VOICE_PATH, i.Name), b, 0777) + } } } } diff --git a/global/fs.go b/global/fs.go index a7570cc..4104511 100644 --- a/global/fs.go +++ b/global/fs.go @@ -9,6 +9,8 @@ import ( var IMAGE_PATH = path.Join("data", "images") +var VOICE_PATH = path.Join("data", "voices") + func PathExists(path string) bool { _, err := os.Stat(path) return err == nil || os.IsExist(err) diff --git a/go.mod b/go.mod index 2de2751..fa9f430 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Mrs4s/go-cqhttp go 1.14 require ( - github.com/Mrs4s/MiraiGo v0.0.0-20200809221224-7a84cfae6795 + github.com/Mrs4s/MiraiGo v0.0.0-20200810032556-a425f9d1b98e github.com/gin-gonic/gin v1.6.3 github.com/gorilla/websocket v1.4.2 github.com/guonaihong/gout v0.1.1 diff --git a/go.sum b/go.sum index accc4cb..8db1332 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Mrs4s/MiraiGo v0.0.0-20200809221224-7a84cfae6795 h1:Bu4k9ZS/IIy9Shwd9lS/C2P/2I8fYUwg1OpRF91hr1w= github.com/Mrs4s/MiraiGo v0.0.0-20200809221224-7a84cfae6795/go.mod h1:0je03wji/tSw4bUH4QCF2Z4/EjyNWjSJTyy5tliX6EM= +github.com/Mrs4s/MiraiGo v0.0.0-20200810032556-a425f9d1b98e h1:5LYDouOL9ZgTL5PwZuuSlFYSfboRQjnXqRIlhviRcGE= +github.com/Mrs4s/MiraiGo v0.0.0-20200810032556-a425f9d1b98e/go.mod h1:0je03wji/tSw4bUH4QCF2Z4/EjyNWjSJTyy5tliX6EM= github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/main.go b/main.go index 5ad0937..8b2cbeb 100644 --- a/main.go +++ b/main.go @@ -36,14 +36,16 @@ func init() { if err == nil { log.SetOutput(io.MultiWriter(os.Stderr, w)) } - if !global.PathExists("data") { - if err := os.Mkdir("data", 0777); err != nil { - log.Fatalf("创建数据文件夹失败: %v", err) - } - if err := os.Mkdir(path.Join("data", "images"), 0777); err != nil { + if !global.PathExists(global.IMAGE_PATH) { + if err := os.MkdirAll(global.IMAGE_PATH, 0677); err != nil { log.Fatalf("创建图片缓存文件夹失败: %v", err) } } + if !global.PathExists(global.VOICE_PATH) { + if err := os.MkdirAll(global.VOICE_PATH, 06777); err != nil { + log.Fatalf("创建语音缓存文件夹失败: %v", err) + } + } if global.PathExists("cqhttp.json") { log.Info("发现 cqhttp.json 将在五秒后尝试导入配置,按 Ctrl+C 取消.") log.Warn("警告: 该操作会删除 cqhttp.json 并覆盖 config.json 文件.")