diff --git a/go.mod b/go.mod index 9e9fc04e..afb43700 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/FloatTech/ZeroBot-Plugin go 1.17 require ( - github.com/FloatTech/AnimeAPI v1.2.4-beta5 - github.com/FloatTech/zbputils v1.2.4-beta5 + github.com/FloatTech/AnimeAPI v1.2.4-beta17 + github.com/FloatTech/zbputils v1.2.4-beta6 github.com/antchfx/htmlquery v1.2.4 github.com/corona10/goimagehash v1.0.3 github.com/fogleman/gg v1.3.0 diff --git a/go.sum b/go.sum index 71dd880c..a3db2940 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ -github.com/FloatTech/AnimeAPI v1.2.4-beta5 h1:RQY8BodjVZ+j+3nc91OKCGHDaPxgUk9P4RDOQWcwT2E= -github.com/FloatTech/AnimeAPI v1.2.4-beta5/go.mod h1:GjOmVhaOji/ibu/s3RXoatd5gJ/4cp2lsvflRBGRw5s= +github.com/FloatTech/AnimeAPI v1.2.4-beta17 h1:oc6MBl0fNV80KAfDa0on+PZRTQ3tBvswCvQVRHBuynI= +github.com/FloatTech/AnimeAPI v1.2.4-beta17/go.mod h1:aKJNGX/M2tmPT0ECmPHnn2ZGzvGkjaZXLpKtcrbKU7Y= github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ= -github.com/FloatTech/zbputils v1.2.4-beta5 h1:puvvScHJ6G+VmQbj0LSBd3mJMVD+bVAf14P9kVUxfso= -github.com/FloatTech/zbputils v1.2.4-beta5/go.mod h1:yQwwXUoFhJeU+CkaDXia1E+k4iBqxIiobE3n3Vy/R3w= +github.com/FloatTech/zbputils v1.2.4-beta6 h1:r6NtdVznx42QVNgJ46ldIw4YmyQDrciyKufwflgKU7Q= +github.com/FloatTech/zbputils v1.2.4-beta6/go.mod h1:yQwwXUoFhJeU+CkaDXia1E+k4iBqxIiobE3n3Vy/R3w= github.com/Mrs4s/MiraiGo v0.0.0-20211120033824-43b23f4e6fcb h1:Rkj28fqIwGx/EgBzRYtpmJRfH6wqVn7cNdc7aJ0QE4M= github.com/Mrs4s/MiraiGo v0.0.0-20211120033824-43b23f4e6fcb/go.mod h1:imVKbfKqqeit+C/eaWGb4MKQ3z3gN6pRpBU5RMtp5so= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= diff --git a/main.go b/main.go index 24f657df..7e6086a1 100644 --- a/main.go +++ b/main.go @@ -13,9 +13,11 @@ import ( // webctrl "github.com/FloatTech/zbputils/control/web" // web 后端控制 // 词库类 + "github.com/FloatTech/AnimeAPI/imgpool" _ "github.com/FloatTech/ZeroBot-Plugin/plugin_ai_reply" // 人工智能回复 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_atri" // ATRI词库 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat" // 基础词库 + "github.com/FloatTech/zbputils/control" // 实用类 _ "github.com/FloatTech/ZeroBot-Plugin/plugin_b14" // base16384加解密 @@ -84,13 +86,14 @@ var ( "* Copyright © 2020 - 2021 FloatTech. All Rights Reserved.", "* Project: https://github.com/FloatTech/ZeroBot-Plugin", } - nicks = []string{"ATRI", "atri", "亚托莉", "アトリ"} - banner = strings.Join(contents, "\n") - token *string - url *string - adana *string - prefix *string - reg = registry.NewRegReader("reilia.fumiama.top:32664", "fumiama") + nicks = []string{"ATRI", "atri", "亚托莉", "アトリ"} + banner = strings.Join(contents, "\n") + token *string + url *string + adana *string + prefix *string + poolkey *string + reg = registry.NewRegReader("reilia.fumiama.top:32664", "fumiama") ) func init() { @@ -108,6 +111,7 @@ func init() { // 默认昵称 adana = flag.String("n", "椛椛", "Set default nickname.") prefix = flag.String("p", "/", "Set command prefix.") + poolkey = flag.String("pk", "", "Set imgpool key and enable listening.") flag.Parse() if *h { @@ -123,6 +127,11 @@ func init() { logrus.SetLevel(logrus.WarnLevel) } } + + if *poolkey != "" { + imgpool.RegisterListener(*poolkey, control.Register("imgpool", 1, &control.Options{})) + } + // 启用 gui // webctrl.InitGui(*g) } diff --git a/plugin_acgimage/classify.go b/plugin_acgimage/classify.go index 636bfcb9..929db041 100644 --- a/plugin_acgimage/classify.go +++ b/plugin_acgimage/classify.go @@ -7,7 +7,7 @@ import ( "time" "github.com/FloatTech/AnimeAPI/classify" - "github.com/FloatTech/AnimeAPI/picture" + "github.com/FloatTech/AnimeAPI/imgpool" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/extension/rate" "github.com/wdvxdr1123/ZeroBot/message" @@ -90,7 +90,7 @@ func init() { // 插件主体 } }) // 上传一张图进行评价 - engine.OnKeywordGroup([]string{"评价图片"}, zero.OnlyPublic, picture.CmdMatch, picture.MustGiven).SetBlock(true). + engine.OnKeywordGroup([]string{"评价图片"}, zero.OnlyPublic, ctxext.CmdMatch, ctxext.MustGiven).SetBlock(true). Handle(func(ctx *zero.Ctx) { ctx.SendChain(message.Text("少女祈祷中...")) for _, url := range ctx.State["image_url"].([]string) { @@ -103,11 +103,22 @@ func init() { // 插件主体 Handle(func(ctx *zero.Ctx) { dhash := ctx.State["regex_matched"].([]string)[1] if len(dhash) == 5*3 { + var u string if web.IsSupportIPv6 { - ctx.SendChain(message.Image(apiheadv6 + dhash + ".webp")) + u = apiheadv6 + dhash + ".webp" } else { - ctx.SendChain(message.Image(apihead + dhash)) + u = apihead + dhash } + + m, err := imgpool.NewImage(ctx, dhash, u) + var img message.MessageSegment + if err != nil { + img = message.Image(u) + } else { + img = message.Image(m.String()) + } + + ctx.SendChain(img) } }) } @@ -122,11 +133,19 @@ func replyClass(ctx *zero.Ctx, class int, dhash string, comment string, isupload return } - var img message.MessageSegment + var u string if web.IsSupportIPv6 { - img = message.Image(apiheadv6 + dhash + ".webp") + u = apiheadv6 + dhash + ".webp" } else { - img = message.Image(apihead + dhash) + u = apihead + dhash + } + + m, err := imgpool.NewImage(ctx, b14, u) + var img message.MessageSegment + if err != nil { + img = message.Image(u) + } else { + img = message.Image(m.String()) } if class > 5 { diff --git a/plugin_danbooru/main.go b/plugin_danbooru/main.go index 15ca5158..cab58a6d 100644 --- a/plugin_danbooru/main.go +++ b/plugin_danbooru/main.go @@ -7,9 +7,9 @@ import ( "os" "github.com/FloatTech/AnimeAPI/danbooru" - "github.com/FloatTech/AnimeAPI/picture" "github.com/FloatTech/AnimeAPI/saucenao" "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/ctxext" "github.com/FloatTech/zbputils/file" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" @@ -32,7 +32,7 @@ func init() { // 插件主体 "- 鉴赏图片[图片]", }) // 上传一张图进行评价 - engine.OnKeywordGroup([]string{"鉴赏图片"}, zero.OnlyPublic, picture.CmdMatch, picture.MustGiven).SetBlock(true). + engine.OnKeywordGroup([]string{"鉴赏图片"}, zero.OnlyPublic, ctxext.CmdMatch, ctxext.MustGiven).SetBlock(true). Handle(func(ctx *zero.Ctx) { ctx.SendChain(message.Text("少女祈祷中...")) for _, url := range ctx.State["image_url"].([]string) { diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index ceea08d1..8b5e74a9 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -7,6 +7,7 @@ import ( "encoding/hex" "encoding/json" "image" + "io" "math/rand" "os" "strconv" @@ -18,10 +19,10 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/utils/helper" + "github.com/FloatTech/AnimeAPI/imgpool" control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/math" - "github.com/FloatTech/zbputils/process" "github.com/FloatTech/zbputils/txt2img" "github.com/FloatTech/ZeroBot-Plugin/order" @@ -57,7 +58,6 @@ func init() { if err != nil { panic(err) } - process.SleepAbout1sTo2s() data, err := file.GetLazyData(omikujson, true, false) if err != nil { panic(err) @@ -142,27 +142,32 @@ func init() { digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text)) cachefile := cache + hex.EncodeToString(digest[:]) - var data []byte - switch file.IsExist(cachefile) { - case true: - data, err = os.ReadFile(cachefile) - if err == nil { - break + m, err := imgpool.NewImage(ctx, cachefile, cachefile) + if err != nil { + if file.IsNotExist(cachefile) { + f, err := os.Create(cachefile) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + _, err = draw(background, title, text, f) + f.Close() + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + } else { + m, err = imgpool.NewImage(ctx, cachefile, cachefile) } - _ = os.Remove(cachefile) - fallthrough - case false: - // 绘制背景 - data, err = draw(background, title, text) } if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return } - _ = os.WriteFile(cachefile, data, 0644) + // 发送图片 - ctx.SendChain(message.Image("base64://" + helper.BytesToString(data))) + ctx.SendChain(message.Image(m.String())) }) } @@ -206,20 +211,20 @@ func randtext(seed int64) (string, string) { // @param title 签名 // @param text 签文 // @return 错误信息 -func draw(back image.Image, title, text string) ([]byte, error) { +func draw(back image.Image, title, text string, f io.Writer) (int64, error) { canvas := gg.NewContext(back.Bounds().Size().Y, back.Bounds().Size().X) canvas.DrawImage(back, 0, 0) // 写标题 canvas.SetRGB(1, 1, 1) if err := canvas.LoadFontFace(font, 45); err != nil { - return nil, err + return -1, err } sw, _ := canvas.MeasureString(title) canvas.DrawString(title, 140-sw/2, 112) // 写正文 canvas.SetRGB(0, 0, 0) if err := canvas.LoadFontFace(font, 23); err != nil { - return nil, err + return -1, err } tw, th := canvas.MeasureString("测") tw, th = tw+10, th+10 @@ -247,7 +252,7 @@ func draw(back image.Image, title, text string) ([]byte, error) { } } } - return txt2img.TxtCanvas{Canvas: canvas}.ToBase64() + return txt2img.TxtCanvas{Canvas: canvas}.WriteTo(f) } func offest(total, now int, distance float64) float64 { diff --git a/plugin_image_finder/keyword.go b/plugin_image_finder/keyword.go index bcdfb062..5371e83a 100644 --- a/plugin_image_finder/keyword.go +++ b/plugin_image_finder/keyword.go @@ -6,8 +6,10 @@ import ( "fmt" "math/rand" "net/http" + "strconv" "time" + "github.com/FloatTech/AnimeAPI/imgpool" control "github.com/FloatTech/zbputils/control" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" @@ -73,7 +75,13 @@ func init() { pom1 := "https://i.pixiv.re" rannum := randintn(len(soutujson.Illusts)) pom2 := soutujson.Illusts[rannum].ImageUrls.Medium[19:] - ctx.SendChain(message.Image(pom1 + pom2)) + u := pom1 + pom2 + m, err := imgpool.NewImage(ctx, strconv.Itoa(soutujson.Illusts[rannum].ID), u) + if err != nil { + ctx.SendChain(message.Image(u)) + return + } + ctx.SendChain(message.Image(m.String())) }) } diff --git a/plugin_lolicon/lolicon.go b/plugin_lolicon/lolicon.go index eb6e3b78..2230f330 100644 --- a/plugin_lolicon/lolicon.go +++ b/plugin_lolicon/lolicon.go @@ -11,6 +11,7 @@ import ( zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" + "github.com/FloatTech/AnimeAPI/imgpool" control "github.com/FloatTech/zbputils/control" "github.com/FloatTech/zbputils/math" @@ -53,8 +54,13 @@ func init() { } url := json.Get("data.0.urls.original").Str url = strings.ReplaceAll(url, "i.pixiv.cat", "i.pixiv.re") - ctx.SendGroupMessage(0, message.Image(url)) - queue <- url + id := json.Get("data.0.pid").String() + m, err := imgpool.NewImage(ctx, id, url) + if err == nil { + queue <- m.String() + } else { + queue <- url + } } }() select { diff --git a/plugin_nativewife/main.go b/plugin_nativewife/main.go index 5c7138ca..b09a46da 100644 --- a/plugin_nativewife/main.go +++ b/plugin_nativewife/main.go @@ -12,7 +12,6 @@ import ( "strings" "time" - "github.com/FloatTech/AnimeAPI/picture" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/utils/helper" @@ -63,7 +62,7 @@ func init() { } }) // 上传一张图 - engine.OnPrefix("添加wife", zero.OnlyGroup, chkAddWifePermission, picture.MustGiven).SetBlock(true). + engine.OnPrefix("添加wife", zero.OnlyGroup, chkAddWifePermission, ctxext.MustGiven).SetBlock(true). Handle(func(ctx *zero.Ctx) { name := "" for _, elem := range ctx.Event.Message { diff --git a/plugin_saucenao/searcher.go b/plugin_saucenao/searcher.go index 6424fc88..326109d0 100644 --- a/plugin_saucenao/searcher.go +++ b/plugin_saucenao/searcher.go @@ -7,7 +7,6 @@ import ( "strconv" "github.com/FloatTech/AnimeAPI/ascii2d" - "github.com/FloatTech/AnimeAPI/picture" "github.com/FloatTech/AnimeAPI/pixiv" "github.com/FloatTech/AnimeAPI/saucenao" "github.com/sirupsen/logrus" @@ -17,6 +16,7 @@ import ( "github.com/FloatTech/ZeroBot-Plugin/order" control "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/ctxext" "github.com/FloatTech/zbputils/file" ) @@ -86,7 +86,7 @@ func init() { // 插件主体 } }) // 以图搜图 - engine.OnKeywordGroup([]string{"以图搜图", "搜索图片", "以图识图"}, picture.CmdMatch, picture.MustGiven).SetBlock(true). + engine.OnKeywordGroup([]string{"以图搜图", "搜索图片", "以图识图"}, ctxext.CmdMatch, ctxext.MustGiven).SetBlock(true). Handle(func(ctx *zero.Ctx) { // 开始搜索图片 ctx.SendChain(message.Text("少女祈祷中......")) diff --git a/plugin_tracemoe/moe.go b/plugin_tracemoe/moe.go index 75b1cdf7..c100e8f5 100644 --- a/plugin_tracemoe/moe.go +++ b/plugin_tracemoe/moe.go @@ -4,8 +4,8 @@ package tracemoe import ( "fmt" - "github.com/FloatTech/AnimeAPI/picture" control "github.com/FloatTech/zbputils/control" + "github.com/FloatTech/zbputils/ctxext" trmoe "github.com/fumiama/gotracemoe" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" @@ -23,7 +23,7 @@ func init() { // 插件主体 Help: "tracemoe\n- 搜番 | 搜索番剧[图片]", }) // 以图搜图 - engine.OnKeywordGroup([]string{"搜番", "搜索番剧"}, picture.CmdMatch, picture.MustGiven).SetBlock(true). + engine.OnKeywordGroup([]string{"搜番", "搜索番剧"}, ctxext.CmdMatch, ctxext.MustGiven).SetBlock(true). Handle(func(ctx *zero.Ctx) { // 开始搜索图片 ctx.SendChain(message.Text("少女祈祷中......"))