From d44171de614e5d8e358f4a930b205a022e34bfdd Mon Sep 17 00:00:00 2001 From: fumiama Date: Fri, 7 Jan 2022 21:57:30 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E2=9C=A8=20=F0=9F=94=A5?= =?UTF-8?q?=20=20=E4=BC=98=E5=8C=96=20acgimage,=20=E6=96=B0=E5=A2=9E=20ipv?= =?UTF-8?q?6=20=E5=88=86=E6=B5=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- go.sum | 2 + plugin_acgimage/classify.go | 91 +++++++++++++++++++------------------ utils/web/ipv6.go | 20 ++++++++ 4 files changed, 69 insertions(+), 46 deletions(-) create mode 100644 utils/web/ipv6.go diff --git a/go.mod b/go.mod index 4e4edc29..a033afc6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/FloatTech/ZeroBot-Plugin go 1.17 require ( - github.com/FloatTech/AnimeAPI v1.1.11 + github.com/FloatTech/AnimeAPI v1.1.12 github.com/FloatTech/ZeroBot-Plugin-Gif v0.2.4 github.com/FloatTech/bot-manager v1.0.1-0.20211112011524-85b9895271ed github.com/antchfx/htmlquery v1.2.4 diff --git a/go.sum b/go.sum index 04e40fbe..7fc903c1 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/FloatTech/AnimeAPI v1.1.9/go.mod h1:CC+vF30UGBlcIUxwFOcXIEHoJ4r7c5x2iLQsnUCVdDI= github.com/FloatTech/AnimeAPI v1.1.11 h1:uuV4v5qweh0mI0E2KMiG5XGt0pKboV/EFAlIfSJxIi8= github.com/FloatTech/AnimeAPI v1.1.11/go.mod h1:CC+vF30UGBlcIUxwFOcXIEHoJ4r7c5x2iLQsnUCVdDI= +github.com/FloatTech/AnimeAPI v1.1.12 h1:tEIAYumjti+FXYw5dhANU9lpoHY7i/sLm96Ly6kPeLE= +github.com/FloatTech/AnimeAPI v1.1.12/go.mod h1:CC+vF30UGBlcIUxwFOcXIEHoJ4r7c5x2iLQsnUCVdDI= github.com/FloatTech/ZeroBot-Plugin v1.1.5/go.mod h1:kWuUARvU7gs4xLggi8Sy37ja2GRL6k0X6kewe5TiZRs= github.com/FloatTech/ZeroBot-Plugin-Gif v0.2.4 h1:WW0BmmLLqAg+m6qGkrKbsfSIm91fkj3/udt3R7Myodo= github.com/FloatTech/ZeroBot-Plugin-Gif v0.2.4/go.mod h1:W7ag6hml1pZTNzRXKU74OMr6rS8awQKSU+o2g7Gj4O0= diff --git a/plugin_acgimage/classify.go b/plugin_acgimage/classify.go index 5f65ce07..f2d627a7 100644 --- a/plugin_acgimage/classify.go +++ b/plugin_acgimage/classify.go @@ -3,7 +3,6 @@ package acgimage import ( "net/url" - "strconv" "strings" "time" @@ -14,17 +13,16 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" - "github.com/FloatTech/ZeroBot-Plugin/utils/file" + "github.com/FloatTech/ZeroBot-Plugin/utils/web" ) const ( - lolipxy = "https://sayuri.fumiama.top/dice?class=0&loli=true&r18=true" - apihead = "https://sayuri.fumiama.top/img?path=" + lolipxy = "https://sayuri.fumiama.top/dice?class=0&loli=true&r18=true" + apihead = "https://sayuri.fumiama.top/img?path=" + apiheadv6 = "http://aikae.v6.army:62002/img?arg=get&name=" ) var ( - datapath = file.BOTPATH + "/data/acgimage/" - cacheuri = "file:///" + datapath + "cache" // r18有一定保护,一般不会发出图片 randapi = "&loli=true&r18=true" msgof = make(map[int64]int64) @@ -33,8 +31,6 @@ var ( ) func init() { // 插件主体 - // 初始化 classify - classify.Init(datapath) engine := control.Register("acgimage", &control.Options{ DisableOnDefault: false, Help: "随机图片与AI点评\n" + @@ -57,14 +53,12 @@ func init() { // 插件主体 // 有保护的随机图片 engine.OnFullMatch("随机图片", zero.OnlyGroup).SetBlock(true).SetPriority(24). Handle(func(ctx *zero.Ctx) { - if classify.CanVisit(5) && limit.Load(ctx.Event.UserID).Acquire() { - go func() { - class, lastvisit, dhash, comment := classify.Classify(randapi, false) - replyClass(ctx, dhash, class, false, lastvisit, comment) - }() - } else { - ctx.SendChain(message.Text("你太快啦!")) + if limit.Load(ctx.Event.UserID).Acquire() { + class, dhash, comment, _ := classify.Classify(randapi, true) + replyClass(ctx, class, dhash, comment, false) + return } + ctx.SendChain(message.Text("你太快啦!")) }) // 直接随机图片,无r18保护,后果自负。如果出r18图可尽快通过发送"太涩了"撤回 engine.OnFullMatch("直接随机", zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(24). @@ -86,24 +80,31 @@ func init() { // 插件主体 // 撤回最后的直接随机图片 engine.OnFullMatch("太涩了").SetBlock(true).SetPriority(24). Handle(func(ctx *zero.Ctx) { - go cancel(ctx) + msg, ok := msgof[ctx.Event.GroupID] + if ok { + ctx.DeleteMessage(msg) + delete(msgof, ctx.Event.GroupID) + } }) // 上传一张图进行评价 engine.OnKeywordGroup([]string{"评价图片"}, zero.OnlyGroup, picture.CmdMatch, picture.MustGiven).SetBlock(true).SetPriority(24). Handle(func(ctx *zero.Ctx) { ctx.SendChain(message.Text("少女祈祷中...")) for _, url := range ctx.State["image_url"].([]string) { - go func(target string) { - class, lastvisit, dhash, comment := classify.Classify(target, true) - replyClass(ctx, dhash, class, true, lastvisit, comment) - }(url) + class, dhash, comment, _ := classify.Classify(url, true) + replyClass(ctx, class, dhash, comment, true) + break } }) engine.OnRegex(`^给你点提示哦:(.*)$`, zero.OnlyPrivate).SetBlock(true).SetPriority(20). Handle(func(ctx *zero.Ctx) { dhash := ctx.State["regex_matched"].([]string)[1] if len(dhash) == 5*3 { - ctx.SendChain(message.Image(apihead + dhash)) + if web.IsSupportIPv6 { + ctx.SendChain(message.Image(apiheadv6 + dhash + ".webp")) + } else { + ctx.SendChain(message.Image(apihead + dhash)) + } } }) } @@ -112,32 +113,32 @@ func setLastMsg(id int64, msg int64) { msgof[id] = msg } -func cancel(ctx *zero.Ctx) { - msg, ok := msgof[ctx.Event.GroupID] - if ok { - ctx.DeleteMessage(msg) - delete(msgof, ctx.Event.GroupID) +func replyClass(ctx *zero.Ctx, class int, dhash string, comment string, isupload bool) { + b14, err := url.QueryUnescape(dhash) + if err != nil { + return } -} -func replyClass(ctx *zero.Ctx, dhash string, class int, noimg bool, lv int64, comment string) { - img := message.Image(cacheuri + strconv.FormatInt(lv, 10)) - if class > 5 { - if dhash != "" && !noimg { - b14, err3 := url.QueryUnescape(dhash) - if err3 == nil { - ctx.SendChain(message.Text(comment + "\n给你点提示哦:" + b14)) - ctx.Event.GroupID = 0 - ctx.SendChain(img) - } - } else { - ctx.SendChain(message.Text(comment)) - } + var img message.MessageSegment + if web.IsSupportIPv6 { + img = message.Image(apiheadv6 + b14 + ".webp") } else { - if !noimg { - ctx.SendChain(img, message.Text(comment)) - } else { - ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(comment)) - } + img = message.Image(apihead + b14) } + + if class > 5 { + if dhash != "" && !isupload { + ctx.SendChain(message.Text(comment + "\n给你点提示哦:" + b14)) + ctx.Event.GroupID = 0 + ctx.SendChain(img) + return + } + ctx.SendChain(message.Text(comment)) + return + } + if isupload { + ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(comment)) + return + } + ctx.SendChain(img, message.Text(comment)) } diff --git a/utils/web/ipv6.go b/utils/web/ipv6.go new file mode 100644 index 00000000..cbbb2bf0 --- /dev/null +++ b/utils/web/ipv6.go @@ -0,0 +1,20 @@ +package web + +import ( + "io" + "net/http" + + "github.com/sirupsen/logrus" +) + +var IsSupportIPv6 = func() bool { + resp, err := http.Get("http://v6.ipv6-test.com/json/widgetdata.php?callback=?") + if err != nil { + logrus.Infoln("[web] 本机不支持ipv6") + return false + } + _, _ = io.ReadAll(resp.Body) + _ = resp.Body.Close() + logrus.Infoln("[web] 本机支持ipv6") + return true +}()