1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-05 02:30:23 +08:00

optimize: media print

This commit is contained in:
源文雨
2023-11-22 22:31:41 +09:00
parent 92a7eb4678
commit 0f21f89ad0
2 changed files with 48 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/hex"
"hash/crc64"
"net/url"
"regexp"
"runtime"
"strings"
"unsafe"
@@ -146,3 +147,19 @@ func DigestID(id string) uint64 {
}
return crc64.Checksum(b, crc64.MakeTable(crc64.ECMA))
}
const mediafilebed = `https://multimedia.nt.qq.com.cn`
var mediafileinfourlre = regexp.MustCompile(`/download\?appid=\d+&fileid=[0-9A-Za-z-_]+&rkey=[0-9A-Za-z-_]+`)
// mediaURL 从 fileinfo 得到 URL
func mediaURL(fileinfo string) (string, error) {
sb := strings.Builder{}
data, err := base64.StdEncoding.DecodeString(fileinfo)
if err != nil {
return "", err
}
sb.WriteString(mediafilebed)
sb.Write(mediafileinfourlre.Find(data))
return sb.String(), nil
}

View File

@@ -1,7 +1,6 @@
package nano
import (
"encoding/base64"
"encoding/json"
"reflect"
"strconv"
@@ -27,8 +26,8 @@ type Message struct {
FileUUID string `json:"file_uuid"`
FileInfo string `json:"file_info"`
Content string `json:"content"`
Timestamp time.Time `json:"timestamp"`
EditedTimestamp time.Time `json:"edited_timestamp"`
Timestamp *time.Time `json:"timestamp"`
EditedTimestamp *time.Time `json:"edited_timestamp"`
FileInfoTTL int `json:"ttl"`
MentionEveryone bool `json:"mention_everyone"`
Author *User `json:"author"`
@@ -47,6 +46,27 @@ type Message struct {
// "=> ギルド:", ctx.Message.GuildID+", 频道:", ctx.Message.ChannelID+", 用户:", ctx.Message.Author.Username+"("+ctx.Message.Author.ID+"), 内容:", ctx.Message.Content
func (m *Message) String() string {
sb := strings.Builder{}
if m.Timestamp != nil {
sb.WriteString(m.Timestamp.Format(time.DateTime))
}
if m.FileUUID != "" {
sb.WriteString("富媒体: ")
sb.WriteString(m.FileUUID)
sb.WriteString(", 有效期: ")
if m.FileInfoTTL == 0 {
sb.WriteString("长期")
} else {
sb.WriteString(strconv.Itoa(m.FileInfoTTL))
sb.WriteByte('s')
}
u, err := mediaURL(m.FileInfo)
if err == nil {
sb.WriteString(", URL: ")
sb.WriteString(u)
}
sb.WriteString(", URL: ")
return sb.String()
}
if m.SeqInChannel != "" {
sb.WriteByte('[')
sb.WriteString(m.SeqInChannel)
@@ -310,9 +330,9 @@ func (mp *MessagePost) String() string {
}
if mp.Media != nil {
sb.WriteString(", 富媒体: ")
data, err := base64.StdEncoding.DecodeString(mp.Media.FileInfo)
u, err := mediaURL(mp.Media.FileInfo)
if err == nil {
sb.Write(data)
sb.WriteString(u)
} else {
sb.WriteString(mp.Media.FileInfo)
}
@@ -354,7 +374,12 @@ func (bot *Bot) postMessageTo(ep string, content *MessagePost) (*Message, error)
if err != nil {
return nil, errors.Wrap(err, getThisFuncName())
}
return bot.postOpenAPIofMessage(ep, contenttype, body)
m, err := bot.postOpenAPIofMessage(ep, contenttype, body)
if err != nil {
return nil, errors.Wrap(err, getThisFuncName())
}
logrus.Infoln(getLogHeader(), "=> 消息结果:", m)
return m, nil
}
// PostMessageToChannel 向 channel_id 指定的子频道发送消息