♻️ 🔥 优化 acgimage, 新增 ipv6 分流

This commit is contained in:
fumiama 2022-01-07 21:57:30 +08:00
parent 62a9e413fb
commit d44171de61
4 changed files with 69 additions and 46 deletions

2
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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))
}

20
utils/web/ipv6.go Normal file
View File

@ -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
}()