feat: 重构消息处理模块,引入统一的消息接口和类型安全的消息解析

This commit is contained in:
lixiangwuxian
2025-03-08 16:10:06 +08:00
parent e0637ab81f
commit 13ea5d7f98
16 changed files with 412 additions and 165 deletions

View File

@@ -2,12 +2,16 @@ package beatleader
import (
"log"
"os"
"strconv"
"sync"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/message"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/service/beatleader"
"git.lxtend.com/qqbot/util"
)
func init() {
@@ -135,9 +139,31 @@ func getMyRecentScore(msg model.Message) (reply model.Reply) {
FromMsg: msg,
}
}
for _, v := range records {
scoreMsg += v.ToString() + "\n\n"
userName = v.Name
// 单独线程下载封面图片
coverImageMap := make(map[string]string)
wg := sync.WaitGroup{}
for _, record := range records {
coverImageMap[record.SongHash] = record.CoverImage
wg.Add(1)
go func(songHash string) {
defer wg.Done()
//文件存在则跳过
if _, err := os.Stat(util.GetResizedIamgePathByOrgPath(util.GenTempFilePath(songHash + ".png"))); err == nil {
return
}
util.DownloadFile(coverImageMap[songHash], util.GenTempFilePath(songHash+".png"))
newPath, err := util.ResizeImageByMaxHeight(util.GenTempFilePath(songHash+".png"), 20)
os.Remove(util.GenTempFilePath(songHash + ".png"))
if err != nil {
log.Printf("缩放图片失败: %v", err)
}
coverImageMap[songHash] = newPath
}(record.SongHash)
}
wg.Wait()
for _, record := range records {
scoreMsg += record.ToString() + "\n\n"
userName = record.Name
recordCount++
}
if len(scoreMsg) > 0 {
@@ -157,8 +183,14 @@ func getMyRecentScore(msg model.Message) (reply model.Reply) {
}
func screenShotBL(msg model.Message) (reply model.Reply) {
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{
File: "file:///tmp/qqbot/" + beatleader.GetBLPicture(strconv.Itoa(int(msg.UserId))),
},
}
return model.Reply{
ReplyMsg: "[CQ:image,file=file:///tmp/qqbot/" + beatleader.GetBLPicture(strconv.Itoa(int(msg.UserId))) + "]",
ReplyMsg: imageMsg.ToCQString(),
ReferOriginMsg: true,
FromMsg: msg,
}