Skip to content

Commit

Permalink
更明确的错误提示
Browse files Browse the repository at this point in the history
  • Loading branch information
IJNKAWAKAZE committed Aug 22, 2024
1 parent c48d43b commit d7c8e7c
Show file tree
Hide file tree
Showing 26 changed files with 98 additions and 68 deletions.
3 changes: 3 additions & 0 deletions src/core/web/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func init() {

func Base(r *gin.Engine) {
r.GET("/base", func(c *gin.Context) {
utils.WebC = make(chan error, 10)
defer close(utils.WebC)
r.LoadHTMLFiles("./template/Base.tmpl")
var playerBase PlayerBase
var userAccount account.UserAccount
Expand All @@ -158,6 +160,7 @@ func Base(r *gin.Engine) {
playerData, skAccount, err := skland.GetPlayerInfo(uid, skAccount)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/web/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Char struct {

func Box(r *gin.Engine) {
r.GET("/box", func(c *gin.Context) {
utils.WebC = make(chan error, 10)
defer close(utils.WebC)
r.LoadHTMLFiles("./template/Box.tmpl")
var box BoxInfo
var userAccount account.UserAccount
Expand All @@ -47,6 +49,7 @@ func Box(r *gin.Engine) {
playerData, _, err := skland.GetPlayerInfo(uid, skAccount)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/web/box_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Equip struct {

func BoxDetail(r *gin.Engine) {
r.GET("/boxDetail", func(c *gin.Context) {
utils.WebC = make(chan error, 10)
defer close(utils.WebC)
r.LoadHTMLFiles("./template/BoxDetail.tmpl")
var detailList []Detail
var userAccount account.UserAccount
Expand All @@ -49,11 +51,13 @@ func BoxDetail(r *gin.Engine) {
playerCultivate, err := skland.GetPlayerCultivate(uid, skAccount)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}
playerData, _, err := skland.GetPlayerInfo(uid, skAccount)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}

Expand Down
5 changes: 5 additions & 0 deletions src/core/web/calendar.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"arknights_bot/utils"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/gin-gonic/gin"
Expand All @@ -23,16 +24,20 @@ type CalendarInfo struct {

func Calendar(r *gin.Engine) {
r.GET("/calendar", func(c *gin.Context) {
utils.WebC = make(chan error, 10)
defer close(utils.WebC)
r.LoadHTMLFiles("./template/Calendar.tmpl")
var calendarMap = make(map[string]template.HTML)
resp, err := http.Get(viper.GetString("api.calendar"))
if err != nil {
log.Println(err)
utils.WebC <- err
return
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}
text := doc.Text()
Expand Down
10 changes: 9 additions & 1 deletion src/core/web/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,32 @@ func init() {

func Card(r *gin.Engine) {
r.GET("/card", func(c *gin.Context) {
utils.WebC = make(chan error, 10)
defer close(utils.WebC)
r.LoadHTMLFiles("./template/Card.tmpl")
userId, _ := strconv.ParseInt(c.Query("userId"), 10, 64)
uid := c.Query("uid")
sklandId := c.Query("sklandId")
playerCard, err := cardData(userId, sklandId, uid)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}
c.HTML(http.StatusOK, "Card.tmpl", playerCard)
})

r.GET("/oldCard", func(c *gin.Context) {
utils.WebC = make(chan error, 10)
defer close(utils.WebC)
r.LoadHTMLFiles("./template/OldCard.tmpl")
userId, _ := strconv.ParseInt(c.Query("userId"), 10, 64)
uid := c.Query("uid")
sklandId := c.Query("sklandId")
playerCard, err := cardData(userId, sklandId, uid)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}
c.HTML(http.StatusOK, "OldCard.tmpl", playerCard)
Expand All @@ -113,7 +121,7 @@ func cardData(userId int64, sklandId, uid string) (PlayerCard, error) {
playerData, skAccount, err := skland.GetPlayerInfo(uid, skAccount)
if err != nil {
log.Println(err)
return playerCard, fmt.Errorf("获取名片信息失败")
return playerCard, err
}

charMap := playerData.CharInfoMap
Expand Down
3 changes: 3 additions & 0 deletions src/core/web/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func init() {

func Depot(r *gin.Engine) {
r.GET("/depot", func(c *gin.Context) {
utils.WebC = make(chan error, 10)
defer close(utils.WebC)
r.LoadHTMLFiles("./template/Depot.tmpl")
var depotItems []DepotItem
var userAccount account.UserAccount
Expand All @@ -57,6 +59,7 @@ func Depot(r *gin.Engine) {
playerCultivate, err := skland.GetPlayerCultivate(uid, skAccount)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}
for _, item := range playerCultivate.Items {
Expand Down
3 changes: 3 additions & 0 deletions src/core/web/missing.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type MissingChar struct {

func Missing(r *gin.Engine) {
r.GET("/missing", func(c *gin.Context) {
utils.WebC = make(chan error, 10)
defer close(utils.WebC)
r.LoadHTMLFiles("./template/Missing.tmpl")
var missingInfo MissingInfo
param := c.Query("param")
Expand All @@ -40,6 +42,7 @@ func Missing(r *gin.Engine) {
playerData, _, err := skland.GetPlayerInfo(uid, skAccount)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}

Expand Down
4 changes: 4 additions & 0 deletions src/core/web/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

func State(r *gin.Engine) {
r.GET("/state", func(c *gin.Context) {
utils.WebC = make(chan error, 10)
defer close(utils.WebC)
r.LoadHTMLFiles("./template/State.tmpl")
var userAccount account.UserAccount
var skAccount skland.Account
Expand All @@ -25,11 +27,13 @@ func State(r *gin.Engine) {
playerData, skAccount, err := skland.GetPlayerInfo(uid, skAccount)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}
playStatistic, _, err := skland.GetPlayerStatistic(uid, skAccount)
if err != nil {
log.Println(err)
utils.WebC <- err
return
}

Expand Down
6 changes: 3 additions & 3 deletions src/plugins/enemy/enemy_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func EnemyHandle(update tgbotapi.Update) error {
}

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/enemy?name=%s", port, name), 0, 1.5)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。")
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/enemy?name=%s", port, name), 0, 1.5)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
return nil
Expand Down
13 changes: 4 additions & 9 deletions src/plugins/material/material_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
tgbotapi "github.com/ijnkawakaze/telegram-bot-api"
"github.com/spf13/viper"
"log"
)

// MaterialHandle 材料查询
Expand Down Expand Up @@ -52,19 +51,15 @@ func MaterialHandle(update tgbotapi.Update) error {
bot.Arknights.Send(sendAction)

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/material?name=%s", port, name), 0, 1.5)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。")
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/material?name=%s", port, name), 0, 1.5)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
return nil
}
sendPhoto := tgbotapi.NewPhoto(chatId, tgbotapi.FileBytes{Bytes: pic})
sendPhoto.ReplyToMessageID = messageId
_, err := bot.Arknights.Send(sendPhoto)
if err != nil {
log.Println(err)
return err
}
bot.Arknights.Send(sendPhoto)
return nil
}
6 changes: 3 additions & 3 deletions src/plugins/operator/operator_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func OperatorHandle(update tgbotapi.Update) error {
}

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/operator?name=%s", port, name), 0, 1.5)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。")
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/operator?name=%s", port, name), 0, 1.5)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
return nil
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/player/base_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func (_ PlayerOperationBase) Run(uid string, userAccount account.UserAccount, ch
bot.Arknights.Send(sendAction)

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/base?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1.5)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name")))
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/base?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1.5)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ParseMode = tgbotapi.ModeMarkdownV2
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/player/box_detail_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (_ PlayerOperationBoxDetail) Run(uid string, userAccount account.UserAccoun
}

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/boxDetail?userId=%d&uid=%s&param=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name")))
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/boxDetail?userId=%d&uid=%s&param=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ParseMode = tgbotapi.ModeMarkdownV2
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/player/box_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ func (_ PlayerOperationBox) Run(uid string, userAccount account.UserAccount, cha
}

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/box?userId=%d&uid=%s&param=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name")))
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/box?userId=%d&uid=%s&param=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ParseMode = tgbotapi.ModeMarkdownV2
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/player/card_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func (_ PlayerOperationCard) Run(uid string, userAccount account.UserAccount, ch
r = "oldCard"
}
port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/%s?userId=%d&uid=%s&sklandId=%s", port, r, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name")))
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/%s?userId=%d&uid=%s&sklandId=%s", port, r, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ParseMode = tgbotapi.ModeMarkdownV2
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/player/depot_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func (_ PlayerOperationDepot) Run(uid string, userAccount account.UserAccount, c
bot.Arknights.Send(sendAction)

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/depot?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1.5)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name")))
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/depot?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1.5)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ParseMode = tgbotapi.ModeMarkdownV2
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/player/gacha_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func (_ PlayerOperationGacha) Run(uid string, userAccount account.UserAccount, c
bot.Arknights.Send(sendAction)

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/gacha?userId=%d&uid=%s", port, userAccount.UserNumber, uid), 3000, 1.5)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, "生成图片失败,请重试。")
pic, e := utils.Screenshot(fmt.Sprintf("http://localhost:%s/gacha?userId=%d&uid=%s", port, userAccount.UserNumber, uid), 3000, 1.5)
if e != nil {
sendMessage := tgbotapi.NewMessage(chatId, e.Error())
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
return nil
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/player/missing_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func (_ PlayerOperationMissing) Run(uid string, userAccount account.UserAccount,
}

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/missing?userId=%d&uid=%s&param=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name")))
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/missing?userId=%d&uid=%s&param=%s&sklandId=%s", port, userAccount.UserNumber, uid, param, userAccount.SklandId), 0, 1.5)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ParseMode = tgbotapi.ModeMarkdownV2
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/player/state_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func (_ PlayerOperationState) Run(uid string, userAccount account.UserAccount, c
bot.Arknights.Send(sendAction)

port := viper.GetString("http.port")
pic := utils.Screenshot(fmt.Sprintf("http://localhost:%s/state?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1)
if pic == nil {
sendMessage := tgbotapi.NewMessage(chatId, fmt.Sprintf("生成图片失败,token可能已失效请[重设token](https://t.me/%s)。", viper.GetString("bot.name")))
pic, err := utils.Screenshot(fmt.Sprintf("http://localhost:%s/state?userId=%d&uid=%s&sklandId=%s", port, userAccount.UserNumber, uid, userAccount.SklandId), 0, 1)
if err != nil {
sendMessage := tgbotapi.NewMessage(chatId, err.Error())
sendMessage.ParseMode = tgbotapi.ModeMarkdownV2
sendMessage.ReplyToMessageID = messageId
bot.Arknights.Send(sendMessage)
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/skland/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func CheckUser(cred string) (bool, error) {
return false, err
}
if res.StatusCode() == 401 {
return false, fmt.Errorf("[skland] %s", "用户未登录!")
return false, fmt.Errorf("无效的cred,请重新登录获取。")
}
return true, nil
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/skland/skland_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func SklandRequest[T any](r *resty.Request, method, path string, vs ...any) (t T
resp, respErr := r.SetError(&SKBaseResp[any]{}).SetResult(&SKBaseResp[T]{}).Execute(method, sklandAddr+path)
if resp.StatusCode() == 405 {
log.Println(string(resp.Body()))
return t, fmt.Errorf("[skland] %s", "服务器被墙了!")
return t, fmt.Errorf("服务器被墙了!")
}
res, err := resty.ParseResp[*SKBaseResp[any], *SKBaseResp[T]](
resp, respErr,
Expand All @@ -67,7 +67,7 @@ func SklandRequestPlayerData(r *resty.Request, method, path string, vs ...any) (
}
if res.StatusCode() == 405 {
log.Println(string(res.Body()))
return d, fmt.Errorf("[skland] %s", "服务器被墙了!")
return d, fmt.Errorf("服务器被墙了!")
}
return string(res.Body()), nil
}
Loading

0 comments on commit d7c8e7c

Please sign in to comment.