refactor: 优化 getMyRecentScore 函数中的消息构建逻辑,使用结构化消息提升代码可读性和可维护性

This commit is contained in:
lixiangwuxian 2025-05-05 02:40:53 +08:00
parent 5b7a5a5aca
commit 6662f3ccfc
2 changed files with 38 additions and 9 deletions

View File

@ -287,7 +287,12 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) {
} }
//如果消息行数太多,使用合并转发 //如果消息行数太多,使用合并转发
if len(records) > 5 { if len(records) > 5 {
nodeMsg := util.NewSelfNodeMessage("玩家 " + userName + " 的" + strconv.Itoa(recordCount) + "条最近记录为:\n" + scoreMsg) nodeMsg := util.NewSelfNodeMessage(&message.TextMessage{
Type: "text",
Data: message.TextMessageData{
Text: "玩家 " + userName + " 的" + strconv.Itoa(recordCount) + "条最近记录为:\n" + scoreMsg,
},
})
action.ActionManager.SendForward( action.ActionManager.SendForward(
&model.Reply{ &model.Reply{
ReplyMsg: []any{&nodeMsg}, ReplyMsg: []any{&nodeMsg},

View File

@ -232,7 +232,7 @@ func unbindSS(msg model.Message) (reply *model.Reply) {
func getMyRecentScore(msg model.Message) (reply *model.Reply) { func getMyRecentScore(msg model.Message) (reply *model.Reply) {
count := 1 count := 1
scoreMsg := "" scoreMsg := []any{}
if len(msg.RawMsg) > len("最新ss ") { if len(msg.RawMsg) > len("最新ss ") {
var err error var err error
count, err = strconv.Atoi(msg.RawMsg[len("最新ss "):]) count, err = strconv.Atoi(msg.RawMsg[len("最新ss "):])
@ -285,13 +285,17 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) {
File: coverImageMap[record.SongHash], File: coverImageMap[record.SongHash],
}, },
} }
scoreMsg += imageMsg.ToCQString() + record.ToString() + "\n" textMsg := message.TextMessage{
Type: "text",
Data: message.TextMessageData{
Text: record.ToString(),
},
}
scoreMsg = append(scoreMsg, imageMsg, textMsg)
userName = record.Name userName = record.Name
recordCount++ recordCount++
} }
if len(scoreMsg) > 0 { if len(scoreMsg) <= 0 {
scoreMsg = scoreMsg[:len(scoreMsg)-len("\n")] //去掉最后一个换行符
} else {
return &model.Reply{ return &model.Reply{
ReplyMsg: "无最近游戏记录", ReplyMsg: "无最近游戏记录",
ReferOriginMsg: true, ReferOriginMsg: true,
@ -300,10 +304,20 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) {
} }
//如果消息行数太多,使用合并转发 //如果消息行数太多,使用合并转发
if len(records) > 5 { if len(records) > 5 {
nodeMsg := util.NewSelfNodeMessage("玩家 " + userName + " 的" + strconv.Itoa(recordCount) + "条最近记录为:\n" + scoreMsg) var nodeMsgs []message.NodeMessage
nodeMsg := util.NewSelfNodeMessage(&message.TextMessage{
Type: "text",
Data: message.TextMessageData{
Text: "玩家 " + userName + " 的" + strconv.Itoa(recordCount) + "条最近记录为:\n",
},
})
nodeMsgs = append(nodeMsgs, *nodeMsg)
for _, msg := range scoreMsg {
nodeMsgs = append(nodeMsgs, *util.NewSelfNodeMessage(msg))
}
action.ActionManager.SendForward( action.ActionManager.SendForward(
&model.Reply{ &model.Reply{
ReplyMsg: []any{&nodeMsg}, ReplyMsg: nodeMsgs,
ReferOriginMsg: false, ReferOriginMsg: false,
FromMsg: msg, FromMsg: msg,
}, },
@ -311,7 +325,17 @@ func getMyRecentScore(msg model.Message) (reply *model.Reply) {
return nil return nil
} }
return &model.Reply{ return &model.Reply{
ReplyMsg: "玩家 " + userName + " 的" + strconv.Itoa(recordCount) + "条最近记录为:\n" + scoreMsg, ReplyMsg: append(
[]any{
&message.TextMessage{
Type: "text",
Data: message.TextMessageData{
Text: "玩家 " + userName + " 的" + strconv.Itoa(recordCount) + "条最近记录为:",
},
},
},
scoreMsg...,
),
ReferOriginMsg: true, ReferOriginMsg: true,
FromMsg: msg, FromMsg: msg,
} }