diff --git a/plugin/lolicon/lolicon.go b/plugin/lolicon/lolicon.go index 4b7cbc25..2ae53fdb 100644 --- a/plugin/lolicon/lolicon.go +++ b/plugin/lolicon/lolicon.go @@ -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 +} diff --git a/plugin/saucenao/searcher.go b/plugin/saucenao/searcher.go index 89eb1082..2b33325d 100644 --- a/plugin/saucenao/searcher.go +++ b/plugin/saucenao/searcher.go @@ -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: 可能被风控了")) } } diff --git a/plugin/setutime/setu_geter.go b/plugin/setutime/setu_geter.go index 05904cee..bcd08200 100644 --- a/plugin/setutime/setu_geter.go +++ b/plugin/setutime/setu_geter.go @@ -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: 可能被风控了")) } }) diff --git a/plugin/tarot/tarot.go b/plugin/tarot/tarot.go index fab90556..488ba05b 100644 --- a/plugin/tarot/tarot.go +++ b/plugin/tarot/tarot.go @@ -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"))) }