mirror of
https://github.com/fumiama/NanoBot.git
synced 2026-06-08 20:20:23 +08:00
optimize: media print
This commit is contained in:
17
helper.go
17
helper.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"hash/crc64"
|
"hash/crc64"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -146,3 +147,19 @@ func DigestID(id string) uint64 {
|
|||||||
}
|
}
|
||||||
return crc64.Checksum(b, crc64.MakeTable(crc64.ECMA))
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package nano
|
package nano
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -27,8 +26,8 @@ type Message struct {
|
|||||||
FileUUID string `json:"file_uuid"`
|
FileUUID string `json:"file_uuid"`
|
||||||
FileInfo string `json:"file_info"`
|
FileInfo string `json:"file_info"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp *time.Time `json:"timestamp"`
|
||||||
EditedTimestamp time.Time `json:"edited_timestamp"`
|
EditedTimestamp *time.Time `json:"edited_timestamp"`
|
||||||
FileInfoTTL int `json:"ttl"`
|
FileInfoTTL int `json:"ttl"`
|
||||||
MentionEveryone bool `json:"mention_everyone"`
|
MentionEveryone bool `json:"mention_everyone"`
|
||||||
Author *User `json:"author"`
|
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
|
// "=> ギルド:", ctx.Message.GuildID+", 频道:", ctx.Message.ChannelID+", 用户:", ctx.Message.Author.Username+"("+ctx.Message.Author.ID+"), 内容:", ctx.Message.Content
|
||||||
func (m *Message) String() string {
|
func (m *Message) String() string {
|
||||||
sb := strings.Builder{}
|
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 != "" {
|
if m.SeqInChannel != "" {
|
||||||
sb.WriteByte('[')
|
sb.WriteByte('[')
|
||||||
sb.WriteString(m.SeqInChannel)
|
sb.WriteString(m.SeqInChannel)
|
||||||
@@ -310,9 +330,9 @@ func (mp *MessagePost) String() string {
|
|||||||
}
|
}
|
||||||
if mp.Media != nil {
|
if mp.Media != nil {
|
||||||
sb.WriteString(", 富媒体: ")
|
sb.WriteString(", 富媒体: ")
|
||||||
data, err := base64.StdEncoding.DecodeString(mp.Media.FileInfo)
|
u, err := mediaURL(mp.Media.FileInfo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
sb.Write(data)
|
sb.WriteString(u)
|
||||||
} else {
|
} else {
|
||||||
sb.WriteString(mp.Media.FileInfo)
|
sb.WriteString(mp.Media.FileInfo)
|
||||||
}
|
}
|
||||||
@@ -354,7 +374,12 @@ func (bot *Bot) postMessageTo(ep string, content *MessagePost) (*Message, error)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, getThisFuncName())
|
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 指定的子频道发送消息
|
// PostMessageToChannel 向 channel_id 指定的子频道发送消息
|
||||||
|
|||||||
Reference in New Issue
Block a user