Skip to content

Commit f9b84f8

Browse files
authored
fix lolicon panic and more plugin use forward (#426)
* fix lolicon panic and more plugin use forward * add back lolicon imgpool * 带tag不使用缓存
1 parent 76e0c13 commit f9b84f8

File tree

4 files changed

+48
-42
lines changed

4 files changed

+48
-42
lines changed

plugin/lolicon/lolicon.go

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lolicon
33

44
import (
55
"encoding/base64"
6+
"errors"
67
"net/url"
78
"strings"
89
"time"
@@ -17,7 +18,7 @@ import (
1718
ctrl "github.com/FloatTech/zbpctrl"
1819
"github.com/FloatTech/zbputils/control"
1920
"github.com/FloatTech/zbputils/ctxext"
20-
"github.com/FloatTech/zbputils/img/pool"
21+
imagepool "github.com/FloatTech/zbputils/img/pool"
2122
)
2223

2324
const (
@@ -26,8 +27,8 @@ const (
2627
)
2728

2829
var (
29-
queue = make(chan string, capacity)
30-
custapi = ""
30+
queue = make(chan string, capacity)
31+
customapi = ""
3132
)
3233

3334
func init() {
@@ -40,45 +41,44 @@ func init() {
4041
}).ApplySingle(ctxext.DefaultSingle)
4142
en.OnPrefix("随机图片").Limit(ctxext.LimitByUser).SetBlock(true).
4243
Handle(func(ctx *zero.Ctx) {
44+
if imgtype := strings.TrimSpace(ctx.State["args"].(string)); imgtype != "" {
45+
imageurl, err := getimgurl(api + "?tag=" + url.QueryEscape(imgtype))
46+
if err != nil {
47+
ctx.SendChain(message.Text("ERROR: ", err))
48+
return
49+
}
50+
if id := ctx.Send(message.Message{ctxext.FakeSenderForwardNode(ctx, message.Image(imageurl))}).ID(); id == 0 {
51+
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
52+
}
53+
return
54+
}
4355
go func() {
4456
for i := 0; i < math.Min(cap(queue)-len(queue), 2); i++ {
45-
if custapi != "" {
46-
data, err := web.GetData(custapi)
57+
if customapi != "" {
58+
data, err := web.GetData(customapi)
4759
if err != nil {
4860
ctx.SendChain(message.Text("ERROR: ", err))
4961
continue
5062
}
5163
queue <- "base64://" + base64.StdEncoding.EncodeToString(data)
5264
continue
5365
}
54-
rapi := api
55-
args := strings.TrimSpace(ctx.State["args"].(string))
56-
if args != "" {
57-
rapi += "?tag=" + url.QueryEscape(args)
58-
}
59-
data, err := web.GetData(rapi)
66+
imageurl, err := getimgurl(api)
6067
if err != nil {
6168
ctx.SendChain(message.Text("ERROR: ", err))
6269
continue
6370
}
64-
json := gjson.ParseBytes(data)
65-
if e := json.Get("error").Str; e != "" {
66-
ctx.SendChain(message.Text("ERROR: ", e))
67-
continue
68-
}
69-
url := json.Get("data.0.urls.original").Str
70-
url = strings.ReplaceAll(url, "i.pixiv.cat", "i.pixiv.re")
71-
name := url[strings.LastIndex(url, "/")+1 : len(url)-4]
72-
m, err := pool.GetImage(name)
71+
name := imageurl[strings.LastIndex(imageurl, "/")+1 : len(imageurl)-4]
72+
m, err := imagepool.GetImage(name)
7373
if err != nil {
74-
m.SetFile(url)
75-
_, err = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
74+
m.SetFile(imageurl)
75+
_, _ = m.Push(ctxext.SendToSelf(ctx), ctxext.GetMessage(ctx))
7676
process.SleepAbout1sTo2s()
7777
}
7878
if err == nil {
7979
queue <- m.String()
8080
} else {
81-
queue <- url
81+
queue <- imageurl
8282
}
8383
}
8484
}()
@@ -98,6 +98,22 @@ func init() {
9898
Handle(func(ctx *zero.Ctx) {
9999
u := strings.TrimSpace(ctx.State["args"].(string))
100100
ctx.SendChain(message.Text("成功设置随机图片地址为", u))
101-
custapi = u
101+
customapi = u
102102
})
103103
}
104+
105+
func getimgurl(url string) (string, error) {
106+
data, err := web.GetData(url)
107+
if err != nil {
108+
return "", err
109+
}
110+
json := gjson.ParseBytes(data)
111+
if e := json.Get("error").Str; e != "" {
112+
return "", errors.New(e)
113+
}
114+
var imageurl string
115+
if imageurl = json.Get("data.0.urls.original").Str; imageurl == "" {
116+
return "", errors.New("未找到相关内容, 换个tag试试吧")
117+
}
118+
return strings.ReplaceAll(imageurl, "i.pixiv.cat", "i.pixiv.re"), nil
119+
}

plugin/saucenao/searcher.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,8 @@ func init() { // 插件主体
9595
"直链: ", "https://pixivel.moe/detail?id=", illust.Pid,
9696
)
9797
if imgs != nil {
98-
if zero.OnlyGroup(ctx) {
99-
ctx.SendGroupForwardMessage(ctx.Event.GroupID, message.Message{
100-
ctxext.FakeSenderForwardNode(ctx, txt),
101-
ctxext.FakeSenderForwardNode(ctx, imgs...),
102-
})
103-
} else {
104-
// 发送搜索结果
105-
ctx.Send(append(imgs, message.Text("\n"), txt))
106-
}
98+
ctx.Send(message.Message{ctxext.FakeSenderForwardNode(ctx, txt),
99+
ctxext.FakeSenderForwardNode(ctx, imgs...)})
107100
} else {
108101
// 图片下载失败,仅发送文字结果
109102
ctx.SendChain(txt)
@@ -113,7 +106,7 @@ func init() { // 插件主体
113106
}
114107
})
115108
// 以图搜图
116-
engine.OnKeywordGroup([]string{"以图搜图", "搜索图片", "以图识图"}, zero.OnlyGroup, zero.MustProvidePicture).SetBlock(true).
109+
engine.OnKeywordGroup([]string{"以图搜图", "搜索图片", "以图识图"}, zero.MustProvidePicture).SetBlock(true).
117110
Handle(func(ctx *zero.Ctx) {
118111
// 开始搜索图片
119112
ctx.SendChain(message.Text("少女祈祷中..."))
@@ -154,7 +147,7 @@ func init() { // 插件主体
154147
msg = append(msg, message.Image(pic))
155148
}
156149
msg = append(msg, message.Text("\n图源: ", result.Header.IndexName, binary.BytesToString(b)))
157-
ctx.Send(msg)
150+
ctx.Send(ctxext.FakeSenderForwardNode(ctx, msg...))
158151
if s > 80.0 {
159152
continue
160153
}
@@ -182,10 +175,7 @@ func init() { // 插件主体
182175
))),
183176
)
184177
}
185-
if id := ctx.SendGroupForwardMessage(
186-
ctx.Event.GroupID,
187-
msg,
188-
).Get("message_id").Int(); id == 0 {
178+
if id := ctx.Send(msg).ID(); id == 0 {
189179
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
190180
}
191181
}

plugin/setutime/setu_geter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func init() { // 插件主体
9494
}
9595
}
9696
// 从缓冲池里抽一张
97-
if id := ctx.SendChain(*pool.pop(imgtype)); id.ID() == 0 {
97+
if id := ctx.Send(ctxext.FakeSenderForwardNode(ctx, *pool.pop(imgtype))); id.ID() == 0 {
9898
ctx.SendChain(message.Text("ERROR: 可能被风控了"))
9999
}
100100
})

plugin/tarot/tarot.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func init() {
162162
message.Text("\n其释义为: ", description)}
163163
msg[i] = ctxext.FakeSenderForwardNode(ctx, tarotMsg...)
164164
}
165-
ctx.SendGroupForwardMessage(ctx.Event.GroupID, msg)
165+
ctx.Send(msg)
166166
})
167167

168168
engine.OnRegex(`^解塔罗牌\s?(.*)`, getTarot).SetBlock(true).Limit(ctxext.LimitByGroup).Handle(func(ctx *zero.Ctx) {
@@ -248,7 +248,7 @@ func init() {
248248
return
249249
}
250250
msg[info.CardsNum] = ctxext.FakeSenderForwardNode(ctx, []message.MessageSegment{message.Image("base64://" + binary.BytesToString(formation))}...)
251-
ctx.SendGroupForwardMessage(ctx.Event.GroupID, msg)
251+
ctx.Send(msg)
252252
} else {
253253
ctx.SendChain(message.Text("没有找到", match, "噢~\n现有牌阵列表: \n", strings.Join(formationName, "\n")))
254254
}

0 commit comments

Comments
 (0)