From e468ac1933adf0da849a5d619416bb34da6bf988 Mon Sep 17 00:00:00 2001 From: fumiama Date: Tue, 22 Feb 2022 22:16:49 +0800 Subject: [PATCH] apply new api ImageBytes --- go.mod | 6 +-- go.sum | 12 +++--- plugin_score/sign_in.go | 10 ++--- plugin_wordle/pack.go | 2 + plugin_wordle/wordle.go | 85 ++++++++++++++++++++++++++--------------- 5 files changed, 68 insertions(+), 47 deletions(-) diff --git a/go.mod b/go.mod index d65f7e7c..64ab5e8f 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/FloatTech/ZeroBot-Plugin go 1.17 require ( - github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222045052-a7f28580c5d8 + github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222140454-0d15c275f385 github.com/FloatTech/sqlite v0.2.0 - github.com/FloatTech/zbputils v1.3.0-beta8.0.20220221142658-9be64984adc5 + github.com/FloatTech/zbputils v1.3.0-beta8.0.20220222140356-fceefeb900dc github.com/antchfx/htmlquery v1.2.4 github.com/corona10/goimagehash v1.0.3 github.com/fogleman/gg v1.3.0 @@ -20,7 +20,7 @@ require ( github.com/shirou/gopsutil/v3 v3.21.12 github.com/sirupsen/logrus v1.8.1 github.com/tidwall/gjson v1.13.0 - github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220216133336-08b3b9bc74b2 + github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220222135901-74f9f7b24555 golang.org/x/image v0.0.0-20211028202545-6944b10bf410 ) diff --git a/go.sum b/go.sum index be9600b5..1e090505 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,10 @@ -github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222045052-a7f28580c5d8 h1:549qKZCemMWO6iBg/3w0XwCeHL46HFsajtoCIvq6TSU= -github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222045052-a7f28580c5d8/go.mod h1:KGZ/fb3ggu0YMeG/Pa1Ei2mtV1ebVdnvVW3gcUgBDQw= +github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222140454-0d15c275f385 h1:MdGiGCZJh/Q9Z9pmZw0xkU7CFfbptmsJECRnT/aUNq4= +github.com/FloatTech/AnimeAPI v1.3.0-beta8.0.20220222140454-0d15c275f385/go.mod h1:zYjxb/MpWOjBgyiMli6356fegX2tJ21+u2TNStzVrHg= github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ= github.com/FloatTech/sqlite v0.2.0 h1:x3uls/hExXH1+bbaNLkvilce6ATtWlDx4IqoxBW/bv8= github.com/FloatTech/sqlite v0.2.0/go.mod h1:xIDWIvpOFl8AXmZm0FC8t3PZjiR6ZutytCpBv2EWCns= -github.com/FloatTech/zbputils v1.3.0-beta8.0.20220221142658-9be64984adc5 h1:f1ZQ3Yvi92MqTKI0gqDlCqSHPUoEr5MFAKW8TB9wk9M= -github.com/FloatTech/zbputils v1.3.0-beta8.0.20220221142658-9be64984adc5/go.mod h1:lNDjXvvNHlo4LHiAMyP/FVWcvw6kWB5onuQ024QvHfw= +github.com/FloatTech/zbputils v1.3.0-beta8.0.20220222140356-fceefeb900dc h1:4oQAPzmpfzdz+8aTXuQYfcN5/v1rNVIxRpwHNJXqN40= +github.com/FloatTech/zbputils v1.3.0-beta8.0.20220222140356-fceefeb900dc/go.mod h1:tUXu86+Hoi6vcLHfcVwjZa9ZYF4JRoNaCC510EELjfs= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0= github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc/go.mod h1:OMmITAib6POA37xCichWM0aRnoVpSMZO1rB/G01wrr0= @@ -154,8 +154,8 @@ github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVM github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn0= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw= -github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220216133336-08b3b9bc74b2 h1:nt1cAlrT0eFrnqzR1WsMx2VMrWZsAl2FrrHHwh4Z9QM= -github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220216133336-08b3b9bc74b2/go.mod h1:NwXIp7PgjV+kUALMXJ4v4/3QcsRSOodtjhLekuPXFog= +github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220222135901-74f9f7b24555 h1:SXcMlq17J6lJxb2uzZwf8QXMEE+FfqnEsAwmOoQa/Fw= +github.com/wdvxdr1123/ZeroBot v1.4.2-0.20220222135901-74f9f7b24555/go.mod h1:NwXIp7PgjV+kUALMXJ4v4/3QcsRSOodtjhLekuPXFog= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= diff --git a/plugin_score/sign_in.go b/plugin_score/sign_in.go index e76fc983..e2c3d5dc 100644 --- a/plugin_score/sign_in.go +++ b/plugin_score/sign_in.go @@ -12,7 +12,6 @@ import ( "github.com/tidwall/gjson" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" - "github.com/wdvxdr1123/ZeroBot/utils/helper" control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/ctxext" @@ -144,12 +143,9 @@ func init() { f, err := os.Create(drawedFile) if err != nil { log.Errorln("[score]", err) - canvasBase64, err := writer.ToBase64(canvas.Image()) - if err != nil { - ctx.SendChain(message.Text("ERROR:", err)) - return - } - ctx.SendChain(message.Image("base64://" + helper.BytesToString(canvasBase64))) + data, cl := writer.ToBytes(canvas.Image()) + ctx.SendChain(message.ImageBytes(data)) + cl() return } _, err = writer.WriteTo(canvas.Image(), f) diff --git a/plugin_wordle/pack.go b/plugin_wordle/pack.go index 07d643d9..783759f2 100644 --- a/plugin_wordle/pack.go +++ b/plugin_wordle/pack.go @@ -11,6 +11,7 @@ import ( type wordpack [3]byte +/* func pack(word string) (w wordpack) { if len(word) != 5 { panic("word must be 5 letters") @@ -34,6 +35,7 @@ func pack(word string) (w wordpack) { binary.PutWriter(wt) return } +*/ func (w wordpack) String() (word string) { wt := binary.SelectWriter() diff --git a/plugin_wordle/wordle.go b/plugin_wordle/wordle.go index b71f78bf..a501de3e 100644 --- a/plugin_wordle/wordle.go +++ b/plugin_wordle/wordle.go @@ -9,7 +9,6 @@ import ( "strings" "time" - "github.com/FloatTech/zbputils/binary" "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/control/order" "github.com/FloatTech/zbputils/ctxext" @@ -54,8 +53,7 @@ func init() { single.WithKeyFn(func(ctx *zero.Ctx) interface{} { return ctx.Event.GroupID }), single.WithPostFn(func(ctx *zero.Ctx) { ctx.Send( - message.ReplyWithMessage( - ctx.Event.MessageID, + message.ReplyWithMessage(ctx.Event.MessageID, message.Text("已经有正在进行的游戏..."), ), ) @@ -66,21 +64,24 @@ func init() { if err != nil { panic(err) } - var wordpacks = loadwords(data) - words = make([]string, 0, len(wordpacks)) + wordpacks := loadwords(data) + words = make([]string, len(wordpacks)) for i := range wordpacks { - words = append(words, wordpacks[i].String()) + words[i] = wordpacks[i].String() } sort.Strings(words) }() en.OnRegex(`(个人|团队)猜单词`, zero.OnlyGroup).SetBlock(true).Limit(ctxext.LimitByUser). Handle(func(ctx *zero.Ctx) { game := newWordleGame() - _, img, _ := game("") + _, img, cl, _ := game("") typ := ctx.State["regex_matched"].([]string)[1] - ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, - message.Image("base64://"+binary.BytesToString(img)), - message.Text("你有6次机会猜出单词,单词长度为5,请发送单词"))) + ctx.Send( + message.ReplyWithMessage(ctx.Event.MessageID, + message.ImageBytes(img), + message.Text("你有6次机会猜出单词,单词长度为5,请发送单词"), + ), + ) var next *zero.FutureEvent if typ == "个人" { next = zero.NewFutureEvent("message", 999, false, zero.RegexRule(`^[A-Z]{5}$|^[a-z]{5}$`), zero.OnlyGroup, zero.CheckUser(ctx.Event.UserID)) @@ -92,44 +93,66 @@ func init() { for { select { case <-time.After(time.Second * 120): - ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, - message.Image("base64://"+binary.BytesToString(img)), - message.Text("猜单词超时,游戏结束..."))) + ctx.Send( + message.ReplyWithMessage(ctx.Event.MessageID, + message.ImageBytes(img), + message.Text("猜单词超时,游戏结束..."), + ), + ) + cl() return case e := <-recv: - win, img, err := game(e.Message.String()) + cl() + win, img, cl, err := game(e.Message.String()) switch { case win: - ctx.Send(message.ReplyWithMessage(e.MessageID, - message.Image("base64://"+binary.BytesToString(img)), - message.Text("太棒了,你猜出来了!"))) + ctx.Send( + message.ReplyWithMessage(e.MessageID, + message.ImageBytes(img), + message.Text("太棒了,你猜出来了!"), + ), + ) + cl() return case err == errTimesRunOut: - ctx.Send(message.ReplyWithMessage(e.MessageID, - message.Image("base64://"+binary.BytesToString(img)), - message.Text("游戏结束..."))) + ctx.Send( + message.ReplyWithMessage(e.MessageID, + message.ImageBytes(img), + message.Text("游戏结束..."), + ), + ) + cl() return case err == errLengthNotEnough: - ctx.Send(message.ReplyWithMessage(e.MessageID, - message.Image("base64://"+binary.BytesToString(img)), - message.Text("单词长度错误"))) + ctx.Send( + message.ReplyWithMessage(e.MessageID, + message.ImageBytes(img), + message.Text("单词长度错误"), + ), + ) case err == errUnknownWord: - ctx.Send(message.ReplyWithMessage(e.MessageID, - message.Image("base64://"+binary.BytesToString(img)), - message.Text("你确定存在这样的单词吗?"))) + ctx.Send( + message.ReplyWithMessage(e.MessageID, + message.ImageBytes(img), + message.Text("你确定存在这样的单词吗?"), + ), + ) default: - ctx.Send(message.ReplyWithMessage(e.MessageID, - message.Image("base64://"+binary.BytesToString(img)))) + ctx.Send( + message.ReplyWithMessage(e.MessageID, + message.ImageBytes(img), + ), + ) } } } }) } -func newWordleGame() func(string) (bool, []byte, error) { +func newWordleGame() func(string) (bool, []byte, func(), error) { onhand := words[rand.Intn(len(words))] record := make([]string, 0, len(onhand)+1) - return func(s string) (win bool, base64Image []byte, err error) { + return func(s string) (win bool, data []byte, cl func(), err error) { if s != "" { s = strings.ToLower(s) if onhand == s { @@ -177,7 +200,7 @@ func newWordleGame() func(string) (bool, []byte, error) { } } } - base64Image, _ = writer.ToBase64(ctx.Image()) + data, cl = writer.ToBytes(ctx.Image()) return } }