mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-20 06:20:08 +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 (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -17,7 +18,7 @@ import (
|
|||||||
ctrl "github.com/FloatTech/zbpctrl"
|
ctrl "github.com/FloatTech/zbpctrl"
|
||||||
"github.com/FloatTech/zbputils/control"
|
"github.com/FloatTech/zbputils/control"
|
||||||
"github.com/FloatTech/zbputils/ctxext"
|
"github.com/FloatTech/zbputils/ctxext"
|
||||||
"github.com/FloatTech/zbputils/img/pool"
|
imagepool "github.com/FloatTech/zbputils/img/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -26,8 +27,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
queue = make(chan string, capacity)
|
queue = make(chan string, capacity)
|
||||||
custapi = ""
|
customapi = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -40,10 +41,21 @@ func init() {
|
|||||||
}).ApplySingle(ctxext.DefaultSingle)
|
}).ApplySingle(ctxext.DefaultSingle)
|
||||||
en.OnPrefix("随机图片").Limit(ctxext.LimitByUser).SetBlock(true).
|
en.OnPrefix("随机图片").Limit(ctxext.LimitByUser).SetBlock(true).
|
||||||
Handle(func(ctx *zero.Ctx) {
|
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() {
|
go func() {
|
||||||
for i := 0; i < math.Min(cap(queue)-len(queue), 2); i++ {
|
for i := 0; i < math.Min(cap(queue)-len(queue), 2); i++ {
|
||||||
if custapi != "" {
|
if customapi != "" {
|
||||||
data, err := web.GetData(custapi)
|
data, err := web.GetData(customapi)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
continue
|
continue
|
||||||
@ -51,34 +63,22 @@ func init() {
|
|||||||
queue <- "base64://" + base64.StdEncoding.EncodeToString(data)
|
queue <- "base64://" + base64.StdEncoding.EncodeToString(data)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rapi := api
|
imageurl, err := getimgurl(api)
|
||||||
args := strings.TrimSpace(ctx.State["args"].(string))
|
|
||||||
if args != "" {
|
|
||||||
rapi += "?tag=" + url.QueryEscape(args)
|
|
||||||
}
|
|
||||||
data, err := web.GetData(rapi)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.SendChain(message.Text("ERROR: ", err))
|
ctx.SendChain(message.Text("ERROR: ", err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
json := gjson.ParseBytes(data)
|
name := imageurl[strings.LastIndex(imageurl, "/")+1 : len(imageurl)-4]
|
||||||
if e := json.Get("error").Str; e != "" {
|
m, err := imagepool.GetImage(name)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.SetFile(url)
|
m.SetFile(imageurl)
|
||||||
_, err = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
|
||||||
process.SleepAbout1sTo2s()
|
process.SleepAbout1sTo2s()
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
queue <- m.String()
|
queue <- m.String()
|
||||||
} else {
|
} else {
|
||||||
queue <- url
|
queue <- imageurl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -98,6 +98,22 @@ func init() {
|
|||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
u := strings.TrimSpace(ctx.State["args"].(string))
|
u := strings.TrimSpace(ctx.State["args"].(string))
|
||||||
ctx.SendChain(message.Text("成功设置随机图片地址为", u))
|
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,
|
"直链: ", "https://pixivel.moe/detail?id=", illust.Pid,
|
||||||
)
|
)
|
||||||
if imgs != nil {
|
if imgs != nil {
|
||||||
if zero.OnlyGroup(ctx) {
|
ctx.Send(message.Message{ctxext.FakeSenderForwardNode(ctx, txt),
|
||||||
ctx.SendGroupForwardMessage(ctx.Event.GroupID, message.Message{
|
ctxext.FakeSenderForwardNode(ctx, imgs...)})
|
||||||
ctxext.FakeSenderForwardNode(ctx, txt),
|
|
||||||
ctxext.FakeSenderForwardNode(ctx, imgs...),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
// 发送搜索结果
|
|
||||||
ctx.Send(append(imgs, message.Text("\n"), txt))
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 图片下载失败,仅发送文字结果
|
// 图片下载失败,仅发送文字结果
|
||||||
ctx.SendChain(txt)
|
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) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
// 开始搜索图片
|
// 开始搜索图片
|
||||||
ctx.SendChain(message.Text("少女祈祷中..."))
|
ctx.SendChain(message.Text("少女祈祷中..."))
|
||||||
@ -154,7 +147,7 @@ func init() { // 插件主体
|
|||||||
msg = append(msg, message.Image(pic))
|
msg = append(msg, message.Image(pic))
|
||||||
}
|
}
|
||||||
msg = append(msg, message.Text("\n图源: ", result.Header.IndexName, binary.BytesToString(b)))
|
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 {
|
if s > 80.0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -182,10 +175,7 @@ func init() { // 插件主体
|
|||||||
))),
|
))),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if id := ctx.SendGroupForwardMessage(
|
if id := ctx.Send(msg).ID(); id == 0 {
|
||||||
ctx.Event.GroupID,
|
|
||||||
msg,
|
|
||||||
).Get("message_id").Int(); id == 0 {
|
|
||||||
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
|
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: 可能被风控了"))
|
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@ -162,7 +162,7 @@ func init() {
|
|||||||
message.Text("\n其释义为: ", description)}
|
message.Text("\n其释义为: ", description)}
|
||||||
msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotMsg...)
|
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) {
|
engine.OnRegex(`^解塔罗牌\s?(.*)`, getTarot).SetBlock(true).Limit(ctxext.LimitByGroup).Handle(func(ctx *zero.Ctx) {
|
||||||
@ -248,7 +248,7 @@ func init() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
msg[info.CardsNum] = ctxext.FakeSenderForwardNode(ctx, []message.MessageSegment{message.Image("base64://" + binary.BytesToString(formation))}...)
|
msg[info.CardsNum] = ctxext.FakeSenderForwardNode(ctx, []message.MessageSegment{message.Image("base64://" + binary.BytesToString(formation))}...)
|
||||||
ctx.SendGroupForwardMessage(ctx.Event.GroupID, msg)
|
ctx.Send(msg)
|
||||||
} else {
|
} else {
|
||||||
ctx.SendChain(message.Text("没有找到", match, "噢~\n现有牌阵列表: \n", strings.Join(formationName, "\n")))
|
ctx.SendChain(message.Text("没有找到", match, "噢~\n现有牌阵列表: \n", strings.Join(formationName, "\n")))
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user