refactor: 重构ss输出,重构url解析
This commit is contained in:
@@ -1,25 +1,150 @@
|
||||
package beatleader
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
|
||||
"git.lxtend.com/qqbot/handler"
|
||||
"git.lxtend.com/qqbot/model"
|
||||
"git.lxtend.com/qqbot/service/beatleader"
|
||||
)
|
||||
|
||||
func init() {
|
||||
handler.RegisterHandler("查bl", getMyBL)
|
||||
// handler.RegisterHandler("绑定bl", bindBL)
|
||||
// handler.RegisterHandler("解绑bl", unbindSS)
|
||||
// handler.RegisterHandler("最新bl", getMyRecentScore)
|
||||
// handler.RegisterHandler("最热bl", getRecentScore)
|
||||
handler.RegisterHandler("绑定bl", bindBL)
|
||||
handler.RegisterHandler("解绑bl", unbindBL)
|
||||
handler.RegisterHandler("最新bl", getMyRecentScore)
|
||||
handler.RegisterHandler("最热bl", getRecentScore)
|
||||
handler.RegisterHandler("截图bl", screenShotBL)
|
||||
}
|
||||
|
||||
func getMyBL(msg model.Message) (reply model.Reply) {
|
||||
if len(msg.Msg) <= len("查bl ") {
|
||||
return model.Reply{}
|
||||
var (
|
||||
resultStr string
|
||||
err error
|
||||
maxRetries = 3 // 最大重试次数
|
||||
attempts = 0
|
||||
)
|
||||
|
||||
userIdStr := strconv.Itoa(int(msg.UserId))
|
||||
for attempts < maxRetries {
|
||||
resultStr, err = beatleader.BLQuery.GetScore(userIdStr)
|
||||
if err == nil {
|
||||
break // 成功时退出循环
|
||||
}
|
||||
attempts++
|
||||
log.Printf("获取分数时出错,第 %d 次重试: %v", attempts, err)
|
||||
}
|
||||
bindResult := ""
|
||||
|
||||
// 如果所有尝试都失败,返回适当的错误消息
|
||||
if err != nil {
|
||||
resultStr = "获取您的分数时出现问题,请稍后重试。"
|
||||
}
|
||||
|
||||
return model.Reply{
|
||||
ReplyMsg: bindResult,
|
||||
ReplyMsg: resultStr,
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
func bindBL(msg model.Message) (reply model.Reply) {
|
||||
if len(msg.RawMsg) <= len("绑定bl ") {
|
||||
return model.Reply{
|
||||
ReplyMsg: "请输入绑定的beatleader账号",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
return model.Reply{
|
||||
ReplyMsg: beatleader.BLQuery.BindBL(strconv.Itoa(int(msg.UserId)), msg.RawMsg[len("绑定bl "):]),
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func unbindBL(msg model.Message) (reply model.Reply) {
|
||||
return model.Reply{
|
||||
ReplyMsg: beatleader.BLQuery.UnbindBL(strconv.Itoa(int(msg.UserId))),
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func getRecentScore(msg model.Message) (reply model.Reply) {
|
||||
count := 1
|
||||
if len(msg.RawMsg) > len("最热bl ") {
|
||||
var err error
|
||||
count, err = strconv.Atoi(msg.RawMsg[len("最热bl "):])
|
||||
if err != nil || count <= 0 {
|
||||
return model.Reply{
|
||||
ReplyMsg: "",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
}
|
||||
scoreMsg := ""
|
||||
for _, v := range beatleader.BlScoresManager.GetRecentScores(count, " WHERE country = 'CN' ") {
|
||||
scoreMsg += v.ToString() + "\n\n"
|
||||
}
|
||||
if len(scoreMsg) > 0 {
|
||||
scoreMsg = scoreMsg[:len(scoreMsg)-len("\n\n")]
|
||||
}
|
||||
return model.Reply{
|
||||
ReplyMsg: scoreMsg,
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func getMyRecentScore(msg model.Message) (reply model.Reply) {
|
||||
count := 1
|
||||
scoreMsg := ""
|
||||
if len(msg.RawMsg) > len("最新bl ") {
|
||||
var err error
|
||||
count, err = strconv.Atoi(msg.RawMsg[len("最新bl "):])
|
||||
if err != nil || count <= 0 {
|
||||
return model.Reply{
|
||||
ReplyMsg: "",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
}
|
||||
var userName string
|
||||
recordCount := 0
|
||||
records, err := beatleader.BLQuery.GetRecentScores(count, strconv.Itoa(int(msg.UserId)))
|
||||
if err != nil {
|
||||
return model.Reply{
|
||||
ReplyMsg: err.Error(),
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
for _, v := range records {
|
||||
scoreMsg += v.ToString() + "\n\n"
|
||||
userName = v.Name
|
||||
recordCount++
|
||||
}
|
||||
if len(scoreMsg) > 0 {
|
||||
scoreMsg = scoreMsg[:len(scoreMsg)-len("\n\n")] //去掉最后一个换行符
|
||||
} else {
|
||||
return model.Reply{
|
||||
ReplyMsg: "无最近游戏记录",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
return model.Reply{
|
||||
ReplyMsg: "玩家 " + userName + " 的" + strconv.Itoa(recordCount) + "条最近记录为:\n" + scoreMsg,
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func screenShotBL(msg model.Message) (reply model.Reply) {
|
||||
return model.Reply{
|
||||
ReplyMsg: "[CQ:image,file=file:///root/qqbot/tmp/" + beatleader.GetBLPicture(strconv.Itoa(int(msg.UserId))) + "]",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package scoresaber
|
||||
import (
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.lxtend.com/qqbot/handler"
|
||||
"git.lxtend.com/qqbot/model"
|
||||
@@ -10,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
handler.RegisterHandler("查ss", getMySS)
|
||||
handler.RegisterHandler("查ss", getSSProfile)
|
||||
handler.RegisterHelpInform("查ss", "查看您的最新分数")
|
||||
handler.RegisterHandler("绑定ss", bindSS)
|
||||
handler.RegisterHelpInform("绑定ss", "绑定您的scoresaber账号")
|
||||
@@ -25,17 +26,40 @@ func init() {
|
||||
handler.RegisterHelpInform("截ss/jss", "scoresaber主页截图")
|
||||
}
|
||||
|
||||
func getMySS(msg model.Message) (reply model.Reply) {
|
||||
func getSSProfile(msg model.Message) (reply model.Reply) {
|
||||
var (
|
||||
resultStr string
|
||||
err error
|
||||
maxRetries = 3 // 最大重试次数
|
||||
maxRetries = 5 // 最大重试次数
|
||||
attempts = 0
|
||||
noUpdate = false
|
||||
)
|
||||
var ssId string
|
||||
if len(msg.RawMsg) > len("查ss ") {
|
||||
// ssId = msg.RawMsg[len("查ss "):]
|
||||
ssId = strings.Split(msg.RawMsg, " ")[1]
|
||||
noUpdate = true
|
||||
}
|
||||
|
||||
userIdStr := strconv.Itoa(int(msg.UserId))
|
||||
for attempts < maxRetries {
|
||||
resultStr, err = scoresaber.SSQuery.GetScore(userIdStr)
|
||||
err = nil
|
||||
if ssId == "" {
|
||||
ssId, err = scoresaber.GetSSID(userIdStr)
|
||||
}
|
||||
if err != nil {
|
||||
// return "您未绑定ss账号,输入\"绑定ss [ssId]\"绑定", nil
|
||||
return model.Reply{
|
||||
ReplyMsg: "您未绑定ss账号,输入\"绑定ss [ssId]\"绑定",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
if !noUpdate {
|
||||
resultStr, err = scoresaber.SSQuery.GetScore(ssId)
|
||||
} else {
|
||||
resultStr, err = scoresaber.SSQuery.GetScoreWithoutUpdate(ssId)
|
||||
}
|
||||
if err == nil {
|
||||
break // 成功时退出循环
|
||||
}
|
||||
@@ -45,7 +69,7 @@ func getMySS(msg model.Message) (reply model.Reply) {
|
||||
|
||||
// 如果所有尝试都失败,返回适当的错误消息
|
||||
if err != nil {
|
||||
resultStr = "获取您的分数时出现问题,请稍后重试。"
|
||||
resultStr = "获取您的分数时出现问题,请稍后重试。" + err.Error()
|
||||
}
|
||||
|
||||
return model.Reply{
|
||||
|
||||
@@ -85,6 +85,8 @@ func extractQQDocURL(input string) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("解析 JSON 失败: %w", err)
|
||||
}
|
||||
url, _ = resolveFinalURL(url)
|
||||
url, _ = removeTrackingParams(url)
|
||||
return url, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user