mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-19 22:00:11 +08:00
fix lolicon panic and more plugin use forward (#426)
* fix lolicon panic and more plugin use forward * add back lolicon imgpool * 带tag不使用缓存
This commit is contained in:
parent
76e0c13f89
commit
f9b84f8cb1
@ -3,6 +3,7 @@ package lolicon
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
@ -17,7 +18,7 @@ import (
|
||||
ctrl "github.com/FloatTech/zbpctrl"
|
||||
"github.com/FloatTech/zbputils/control"
|
||||
"github.com/FloatTech/zbputils/ctxext"
|
||||
"github.com/FloatTech/zbputils/img/pool"
|
||||
imagepool "github.com/FloatTech/zbputils/img/pool"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -26,8 +27,8 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
queue = make(chan string, capacity)
|
||||
custapi = ""
|
||||
queue = make(chan string, capacity)
|
||||
customapi = ""
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -40,10 +41,21 @@ func init() {
|
||||
}).ApplySingle(ctxext.DefaultSingle)
|
||||
en.OnPrefix("随机图片").Limit(ctxext.LimitByUser).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
if imgtype := strings.TrimSpace(ctx.State["args"].(string)); imgtype != "" {
|
||||
imageurl, err := getimgurl(api + "?tag=" + url.QueryEscape(imgtype))
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
if id := ctx.Send(message.Message{ctxext.FakeSenderForwardNode(ctx, message.Image(imageurl))}).ID(); id == 0 {
|
||||
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
|
||||
}
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
for i := 0; i < math.Min(cap(queue)-len(queue), 2); i++ {
|
||||
if custapi != "" {
|
||||
data, err := web.GetData(custapi)
|
||||
if customapi != "" {
|
||||
data, err := web.GetData(customapi)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
continue
|
||||
@ -51,34 +63,22 @@ func init() {
|
||||
queue <- "base64://" + base64.StdEncoding.EncodeToString(data)
|
||||
continue
|
||||
}
|
||||
rapi := api
|
||||
args := strings.TrimSpace(ctx.State["args"].(string))
|
||||
if args != "" {
|
||||
rapi += "?tag=" + url.QueryEscape(args)
|
||||
}
|
||||
data, err := web.GetData(rapi)
|
||||
imageurl, err := getimgurl(api)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
continue
|
||||
}
|
||||
json := gjson.ParseBytes(data)
|
||||
if e := json.Get("error").Str; e != "" {
|
||||
ctx.SendChain(message.Text("ERROR: ", e))
|
||||
continue
|
||||
}
|
||||
url := json.Get("data.0.urls.original").Str
|
||||
url = strings.ReplaceAll(url, "i.pixiv.cat", "i.pixiv.re")
|
||||
name := url[strings.LastIndex(url, "/")+1 : len(url)-4]
|
||||
m, err := pool.GetImage(name)
|
||||
name := imageurl[strings.LastIndex(imageurl, "/")+1 : len(imageurl)-4]
|
||||
m, err := imagepool.GetImage(name)
|
||||
if err != nil {
|
||||
m.SetFile(url)
|
||||
_, err = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
||||
m.SetFile(imageurl)
|
||||
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
||||
process.SleepAbout1sTo2s()
|
||||
}
|
||||
if err == nil {
|
||||
queue <- m.String()
|
||||
} else {
|
||||
queue <- url
|
||||
queue <- imageurl
|
||||
}
|
||||
}
|
||||
}()
|
||||
@ -98,6 +98,22 @@ func init() {
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
u := strings.TrimSpace(ctx.State["args"].(string))
|
||||
ctx.SendChain(message.Text("成功设置随机图片地址为", u))
|
||||
custapi = u
|
||||
customapi = u
|
||||
})
|
||||
}
|
||||
|
||||
func getimgurl(url string) (string, error) {
|
||||
data, err := web.GetData(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
json := gjson.ParseBytes(data)
|
||||
if e := json.Get("error").Str; e != "" {
|
||||
return "", errors.New(e)
|
||||
}
|
||||
var imageurl string
|
||||
if imageurl = json.Get("data.0.urls.original").Str; imageurl == "" {
|
||||
return "", errors.New("未找到相关内容, 换个tag试试吧")
|
||||
}
|
||||
return strings.ReplaceAll(imageurl, "i.pixiv.cat", "i.pixiv.re"), nil
|
||||
}
|
||||
|
||||
@ -95,15 +95,8 @@ func init() { // 插件主体
|
||||
"直链: ", "https://pixivel.moe/detail?id=", illust.Pid,
|
||||
)
|
||||
if imgs != nil {
|
||||
if zero.OnlyGroup(ctx) {
|
||||
ctx.SendGroupForwardMessage(ctx.Event.GroupID, message.Message{
|
||||
ctxext.FakeSenderForwardNode(ctx, txt),
|
||||
ctxext.FakeSenderForwardNode(ctx, imgs...),
|
||||
})
|
||||
} else {
|
||||
// 发送搜索结果
|
||||
ctx.Send(append(imgs, message.Text("\n"), txt))
|
||||
}
|
||||
ctx.Send(message.Message{ctxext.FakeSenderForwardNode(ctx, txt),
|
||||
ctxext.FakeSenderForwardNode(ctx, imgs...)})
|
||||
} else {
|
||||
// 图片下载失败,仅发送文字结果
|
||||
ctx.SendChain(txt)
|
||||
@ -113,7 +106,7 @@ func init() { // 插件主体
|
||||
}
|
||||
})
|
||||
// 以图搜图
|
||||
engine.OnKeywordGroup([]string{"以图搜图", "搜索图片", "以图识图"}, zero.OnlyGroup, zero.MustProvidePicture).SetBlock(true).
|
||||
engine.OnKeywordGroup([]string{"以图搜图", "搜索图片", "以图识图"}, zero.MustProvidePicture).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
// 开始搜索图片
|
||||
ctx.SendChain(message.Text("少女祈祷中..."))
|
||||
@ -154,7 +147,7 @@ func init() { // 插件主体
|
||||
msg = append(msg, message.Image(pic))
|
||||
}
|
||||
msg = append(msg, message.Text("\n图源: ", result.Header.IndexName, binary.BytesToString(b)))
|
||||
ctx.Send(msg)
|
||||
ctx.Send(ctxext.FakeSenderForwardNode(ctx, msg...))
|
||||
if s > 80.0 {
|
||||
continue
|
||||
}
|
||||
@ -182,10 +175,7 @@ func init() { // 插件主体
|
||||
))),
|
||||
)
|
||||
}
|
||||
if id := ctx.SendGroupForwardMessage(
|
||||
ctx.Event.GroupID,
|
||||
msg,
|
||||
).Get("message_id").Int(); id == 0 {
|
||||
if id := ctx.Send(msg).ID(); id == 0 {
|
||||
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +94,7 @@ func init() { // 插件主体
|
||||
}
|
||||
}
|
||||
// 从缓冲池里抽一张
|
||||
if id := ctx.SendChain(*pool.pop(imgtype)); id.ID() == 0 {
|
||||
if id := ctx.Send(ctxext.FakeSenderForwardNode(ctx, *pool.pop(imgtype))); id.ID() == 0 {
|
||||
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
|
||||
}
|
||||
})
|
||||
|
||||
@ -162,7 +162,7 @@ func init() {
|
||||
message.Text("\n其释义为: ", description)}
|
||||
msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotMsg...)
|
||||
}
|
||||
ctx.SendGroupForwardMessage(ctx.Event.GroupID, msg)
|
||||
ctx.Send(msg)
|
||||
})
|
||||
|
||||
engine.OnRegex(`^解塔罗牌\s?(.*)`, getTarot).SetBlock(true).Limit(ctxext.LimitByGroup).Handle(func(ctx *zero.Ctx) {
|
||||
@ -248,7 +248,7 @@ func init() {
|
||||
return
|
||||
}
|
||||
msg[info.CardsNum] = ctxext.FakeSenderForwardNode(ctx, []message.MessageSegment{message.Image("base64://" + binary.BytesToString(formation))}...)
|
||||
ctx.SendGroupForwardMessage(ctx.Event.GroupID, msg)
|
||||
ctx.Send(msg)
|
||||
} else {
|
||||
ctx.SendChain(message.Text("没有找到", match, "噢~\n现有牌阵列表: \n", strings.Join(formationName, "\n")))
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user