Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 满足合并需要 #1093

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 52 additions & 61 deletions plugin/tarot/tarot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@
fcext "github.com/FloatTech/floatbox/ctxext"
"github.com/FloatTech/floatbox/file"
"github.com/FloatTech/floatbox/process"
"github.com/FloatTech/floatbox/web"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/img/pool"
"github.com/FloatTech/zbputils/img/text"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)

const bed = "https://gitcode.net/shudorcl/zbp-tarot/-/raw/master/"

type cardInfo struct {
Description string `json:"description"`
ReverseDescription string `json:"reverseDescription"`
Expand Down Expand Up @@ -61,13 +57,42 @@
PublicDataFolder: "Tarot",
}).ApplySingle(ctxext.DefaultSingle)

cache := engine.DataFolder() + "cache"
_ = os.RemoveAll(cache)
err := os.MkdirAll(cache, 0755)
if err != nil {
panic(err)
logrus.Infof("[tarot]uri: %s", engine.DataFolder())

MajorArcana := engine.DataFolder() + "MajorArcana"
if _, err := os.Stat(MajorArcana); os.IsNotExist(err) {
err := os.MkdirAll(MajorArcana, 0755)
if err != nil {
return
}
}

MinorArcana := engine.DataFolder() + "MinorArcana"
if _, err := os.Stat(MinorArcana); os.IsNotExist(err) {
err := os.MkdirAll(MinorArcana, 0755)
if err != nil {
return
}
}

ReverseMajorArcana := engine.DataFolder() + "Reverse/MajorArcana"

Check failure on line 78 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

ReverseMajorArcana declared and not used

Check failure on line 78 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

ReverseMajorArcana declared and not used

Check failure on line 78 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

ReverseMajorArcana declared and not used

Check failure on line 78 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

ReverseMajorArcana declared and not used

Check failure on line 78 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / lint

ReverseMajorArcana declared and not used

Check failure on line 78 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / lint

ReverseMajorArcana declared and not used
if _, err := os.Stat(Reverse_MajorArcana); os.IsNotExist(err) {

Check failure on line 79 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: Reverse_MajorArcana

Check failure on line 79 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: Reverse_MajorArcana

Check failure on line 79 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: Reverse_MajorArcana

Check failure on line 79 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: Reverse_MajorArcana

Check failure on line 79 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Reverse_MajorArcana

Check failure on line 79 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Reverse_MajorArcana
err := os.MkdirAll(Reverse_MajorArcana, 0755)

Check failure on line 80 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: Reverse_MajorArcana

Check failure on line 80 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: Reverse_MajorArcana

Check failure on line 80 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: Reverse_MajorArcana

Check failure on line 80 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: Reverse_MajorArcana

Check failure on line 80 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Reverse_MajorArcana

Check failure on line 80 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Reverse_MajorArcana
if err != nil {
return
}
}

ReverseMinorArcana := engine.DataFolder() + "Reverse/MinorArcana"

Check failure on line 86 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

ReverseMinorArcana declared and not used

Check failure on line 86 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

ReverseMinorArcana declared and not used

Check failure on line 86 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

ReverseMinorArcana declared and not used

Check failure on line 86 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

ReverseMinorArcana declared and not used

Check failure on line 86 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / lint

ReverseMinorArcana declared and not used
if _, err := os.Stat(Reverse_MinorArcana); os.IsNotExist(err) {

Check failure on line 87 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: Reverse_MinorArcana

Check failure on line 87 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: Reverse_MinorArcana

Check failure on line 87 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: Reverse_MinorArcana

Check failure on line 87 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: Reverse_MinorArcana

Check failure on line 87 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Reverse_MinorArcana
err := os.MkdirAll(Reverse_MinorArcana, 0755)

Check failure on line 88 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, 386)

undefined: Reverse_MinorArcana

Check failure on line 88 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (windows, amd64)

undefined: Reverse_MinorArcana

Check failure on line 88 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, arm)

undefined: Reverse_MinorArcana

Check failure on line 88 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / Build binary CI (linux, amd64)

undefined: Reverse_MinorArcana

Check failure on line 88 in plugin/tarot/tarot.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Reverse_MinorArcana (typecheck)
if err != nil {
return
}
}



getTarot := fcext.DoOnceOnSuccess(func(ctx *zero.Ctx) bool {
data, err := engine.GetLazyData("tarots.json", true)
if err != nil {
Expand Down Expand Up @@ -140,30 +165,14 @@
if p == 1 {
description = card.ReverseDescription
}
imgurl := bed + reverse[p] + card.ImgURL
imgname := ""
if p == 1 {
imgname = reverse[p][:len(reverse[p])-1] + name
} else {
imgname = name
}
imgpath := cache + "/" + imgname + ".png"
err := pool.SendImageFromPool(imgpath, func(pth string) error {
data, err := web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "gitcode.net", web.RandUA(), nil)
if err != nil {
return err
}
f, err := os.Create(pth)
if err != nil {
return err
}
defer f.Close()
return os.WriteFile(f.Name(), data, 0755)
}, ctxext.Send(ctx))
imgurl := reverse[p] + card.ImgURL
_, err := engine.GetLazyData(imgurl, true) // 获取数据和错误
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
logrus.Infof("[tarot]下载数据错误: %s", err)
return
}
imgpath := "/" + engine.DataFolder() + "/"+ imgurl
ctx.SendChain(message.Image("file:///" + file.BOTPATH + imgpath))
process.SleepAbout1sTo2s()
ctx.SendChain(message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n其释义为: ", description))
return
Expand All @@ -185,15 +194,11 @@
if p == 1 {
description = card.ReverseDescription
}
imgurl := bed + reverse[p] + card.ImgURL
imgurl := reverse[p] + card.ImgURL
tarotmsg := message.Message{message.Text(reasons[rand.Intn(len(reasons))], position[p], "的『", name, "』\n")}
var imgmsg message.Segment
var err error
if p == 1 {
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
} else {
imgmsg, err = poolimg(imgurl, name, cache)
}
imgmsg, err = poolimg(engine, imgurl, engine.DataFolder())
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand All @@ -211,9 +216,9 @@
match := ctx.State["regex_matched"].([]string)[1]
info, ok := infoMap[match]
if ok {
imgurl := bed + info.ImgURL
imgurl := info.ImgURL
var tarotmsg message.Message
imgmsg, err := poolimg(imgurl, match, cache)
imgmsg, err := poolimg(engine, imgurl, engine.DataFolder())
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand Down Expand Up @@ -280,14 +285,10 @@
description = card.ReverseDescription
}
var tarotmsg message.Message
imgurl := bed + reverse[p] + card.ImgURL
imgurl := reverse[p] + card.ImgURL
var imgmsg message.Segment
var err error
if p == 1 {
imgmsg, err = poolimg(imgurl, reverse[p][:len(reverse[p])-1]+name, cache)
} else {
imgmsg, err = poolimg(imgurl, name, cache)
}
imgmsg, err = poolimg(engine, imgurl, engine.DataFolder())
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand Down Expand Up @@ -319,26 +320,16 @@
})
}

func poolimg(imgurl, imgname, cache string) (msg message.Segment, err error) {
imgfile := cache + "/" + imgname + ".png"
func poolimg(engine *control.Engine, imgurl, cache string) (msg message.Segment, err error) {
imgfile := cache + "/" + imgurl
aimgfile := file.BOTPATH + "/" + imgfile
if file.IsNotExist(aimgfile) {
var data []byte
data, err = web.RequestDataWith(web.NewTLS12Client(), imgurl, "GET", "gitcode.net", web.RandUA(), nil)
if err != nil {
return
}
var f *os.File
f, err = os.Create(imgfile)
_, err = engine.GetLazyData(imgurl, true) // 获取数据和错误
if err != nil {
return
}
defer f.Close()
err = os.WriteFile(f.Name(), data, 0755)
if err != nil {
return
logrus.Infof("[tarot]下载数据错误: %s", err)
return
}
}
msg = message.Image("file:///" + aimgfile)
return
return
}
Loading