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

@@ -8,6 +8,7 @@ import (
"git.lxtend.com/qqbot/auth"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/message"
"git.lxtend.com/qqbot/model"
)
@@ -36,8 +37,9 @@ func setUserLevel(msg model.Message) (reply model.Reply) {
}
}
userText := tokens[1]
if userId, err := model.ParseAtMessage(userText); err == nil {
userText = userId.Data.QQ
atMsg := message.AtMessage{}
if err := atMsg.ParseMessage(userText); err == nil {
userText = atMsg.Data.QQ
}
log.Println(userText)
user, err := strconv.Atoi(userText)

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,
}

View File

@@ -1,11 +1,10 @@
package drawback
import (
"log"
"git.lxtend.com/qqbot/action"
"git.lxtend.com/qqbot/constants"
"git.lxtend.com/qqbot/handler"
"git.lxtend.com/qqbot/message"
"git.lxtend.com/qqbot/model"
)
@@ -15,9 +14,8 @@ func init() {
}
func drawback(msg model.Message) model.Reply {
msgIdToDrawback, err := model.ParseReplyData(msg.RawMsg)
log.Printf("Drawback message %d", msgIdToDrawback.Data.ID)
if err != nil {
msgIdToDrawback := message.ReplyMessage{}
if err := msgIdToDrawback.ParseMessage(msg.RawMsg); err != nil {
return model.Reply{
ReplyMsg: "",
ReferOriginMsg: false,

View File

@@ -1,6 +1,7 @@
package getweb
import (
"git.lxtend.com/qqbot/message"
"git.lxtend.com/qqbot/model"
"git.lxtend.com/qqbot/util"
)
@@ -24,8 +25,14 @@ func getweb(msg model.Message) (reply model.Reply) {
FromMsg: msg,
}
}
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{
File: "file:///tmp/qqbot/getweb/url.png",
},
}
return model.Reply{
ReplyMsg: "[CQ:image,file=file:///tmp/qqbot/getweb/url.png]",
ReplyMsg: imageMsg.ToCQString(),
ReferOriginMsg: true,
FromMsg: msg,
}

View File

@@ -6,6 +6,7 @@ import (
"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/xibao"
"git.lxtend.com/qqbot/util"
@@ -33,8 +34,14 @@ func xiBao(msg model.Message) (reply model.Reply) {
}
}
xibao.GenerateCongratulationImage(tokens[1], "./resource/xibao_background.png", filePath, true)
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{
File: "file:///tmp/qqbot/" + fileName + ".png",
},
}
return model.Reply{
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///tmp/qqbot/%s]", fileName+".png"),
ReplyMsg: imageMsg.ToCQString(),
ReferOriginMsg: true,
FromMsg: msg,
}
@@ -45,8 +52,14 @@ func xiBaoTemp(msg model.Message) (reply model.Reply, isTrigger bool) {
fileName := uuid.New().String()
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
xibao.GenerateCongratulationImage(msg.RawMsg, "./resource/xibao_background.png", filePath, true)
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{
File: "file:///tmp/qqbot/" + fileName + ".png",
},
}
return model.Reply{
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///tmp/qqbot/%s]", fileName+".png"),
ReplyMsg: imageMsg.ToCQString(),
ReferOriginMsg: true,
FromMsg: msg,
}, true
@@ -66,8 +79,14 @@ func beiBao(msg model.Message) (reply model.Reply) {
}
}
xibao.GenerateCongratulationImage(tokens[1], "./resource/beibao_background.png", filePath, false)
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{
File: "file:///tmp/qqbot/" + fileName + ".png",
},
}
return model.Reply{
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///tmp/qqbot/%s]", fileName+".png"),
ReplyMsg: imageMsg.ToCQString(),
ReferOriginMsg: true,
FromMsg: msg,
}
@@ -78,8 +97,14 @@ func beiBaoTemp(msg model.Message) (reply model.Reply, isTrigger bool) {
fileName := uuid.New().String()
filePath := util.GenTempFilePath(fmt.Sprintf("%s.png", fileName))
xibao.GenerateCongratulationImage(msg.RawMsg, "./resource/beibao_background.png", filePath, false)
imageMsg := message.ImageMessage{
Type: "image",
Data: message.ImageMessageData{
File: "file:///tmp/qqbot/" + fileName + ".png",
},
}
return model.Reply{
ReplyMsg: fmt.Sprintf("[CQ:image,file=file:///tmp/qqbot/%s]", fileName+".png"),
ReplyMsg: imageMsg.ToCQString(),
ReferOriginMsg: true,
FromMsg: msg,
}, true