Skip to content

Commit

Permalink
fix(emozi): login
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Oct 14, 2024
1 parent 48a2703 commit 105553c
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions plugin/emozi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"strconv"
"strings"
"sync"
"time"

"github.com/FloatTech/AnimeAPI/emozi"
ctrl "github.com/FloatTech/zbpctrl"
Expand All @@ -24,7 +26,20 @@ func init() {
})
usr := emozi.Anonymous()
data, err := os.ReadFile(en.DataFolder() + "user.txt")
hasaccount := false
refresh := func() {
t := time.NewTicker(time.Hour)
defer t.Stop()
for range t.C {
if !usr.IsValid() {
time.Sleep(time.Second * 2)
err := usr.Login()
if err != nil {
logrus.Warnln("[emozi] 重新登录账号失败:", err)
}
}
}
}
refresher := sync.Once{}
if err == nil {
arr := strings.Split(string(data), "\n")
if len(arr) >= 2 {
Expand All @@ -35,7 +50,7 @@ func init() {
usr = emozi.Anonymous()
} else {
logrus.Infoln("[emozi]", "以", arr[0], "身份登录成功")
hasaccount = true
refresher.Do(refresh)
}
}
}
Expand All @@ -44,16 +59,8 @@ func init() {
txt := strings.TrimSpace(ctx.State["args"].(string))
out, chs, err := usr.Marshal(false, txt)
if err != nil {
if hasaccount {
err2 := usr.Login()
if err2 == nil {
out, chs, err = usr.Marshal(false, txt)
}
}
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Text("ERROR: ", err))
return
}
if len(chs) == 0 {
ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text(out)))
Expand Down Expand Up @@ -82,18 +89,6 @@ func init() {
en.OnPrefix("抽象还原").Limit(ctxext.LimitByUser).SetBlock(true).Handle(func(ctx *zero.Ctx) {
txt := strings.TrimSpace(ctx.State["args"].(string))
out, err := usr.Unmarshal(false, txt)
if err != nil {
if hasaccount {
err2 := usr.Login()
if err2 == nil {
out, err = usr.Unmarshal(false, txt)
}
}
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
}
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand All @@ -115,7 +110,7 @@ func init() {
return
}
usr = newusr
hasaccount = true
refresher.Do(refresh)
ctx.SendChain(message.Text("成功"))
})
}

0 comments on commit 105553c

Please sign in to comment.