mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 18:20:27 +00:00
🎨 改进代码结构
This commit is contained in:
111
plugin_rand_image/classify.go
Normal file
111
plugin_rand_image/classify.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package plugin_rand_image
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var (
|
||||
CACHE_IMG_FILE = "/tmp/setugt"
|
||||
CACHE_URI = "file:///" + CACHE_IMG_FILE
|
||||
VOTE_API_URL = "http://saki.fumiama.top/vote?uuid=零号&img=%s&class=%d"
|
||||
CLASSIFY_HEAD = "http://saki.fumiama.top:62002/dice?url="
|
||||
MsgofGrp = make(map[int64]int64)
|
||||
dhashofmsg = make(map[int64]string)
|
||||
)
|
||||
|
||||
func Classify(ctx *zero.Ctx, targeturl string, noimg bool) {
|
||||
get_url := CLASSIFY_HEAD + url.QueryEscape(targeturl)
|
||||
if noimg {
|
||||
get_url += "&noimg=true"
|
||||
}
|
||||
resp, err := http.Get(get_url)
|
||||
if err != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err))
|
||||
} else {
|
||||
if noimg {
|
||||
data, err1 := ioutil.ReadAll(resp.Body)
|
||||
if err1 == nil {
|
||||
dhash := gjson.GetBytes(data, "img").String()
|
||||
class := int(gjson.GetBytes(data, "class").Int())
|
||||
replyClass(ctx, dhash, class, noimg)
|
||||
} else {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err1))
|
||||
}
|
||||
} else {
|
||||
class, err1 := strconv.Atoi(resp.Header.Get("Class"))
|
||||
dhash := resp.Header.Get("DHash")
|
||||
if err1 != nil {
|
||||
ctx.Send(fmt.Sprintf("ERROR: %v", err1))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
// 写入文件
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
f, _ := os.OpenFile(CACHE_IMG_FILE, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
|
||||
defer f.Close()
|
||||
f.Write(data)
|
||||
replyClass(ctx, dhash, class, noimg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Vote(ctx *zero.Ctx, class int) {
|
||||
msg, ok := MsgofGrp[ctx.Event.GroupID]
|
||||
if ok {
|
||||
ctx.DeleteMessage(msg)
|
||||
delete(MsgofGrp, ctx.Event.GroupID)
|
||||
dhash, ok2 := dhashofmsg[msg]
|
||||
if ok2 {
|
||||
http.Get(fmt.Sprintf(VOTE_API_URL, dhash, class))
|
||||
delete(dhashofmsg, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func replyClass(ctx *zero.Ctx, dhash string, class int, noimg bool) {
|
||||
if class > 4 {
|
||||
switch class {
|
||||
case 5:
|
||||
ctx.Send("[5]影响不好啦!")
|
||||
case 6:
|
||||
ctx.Send("[6]太涩啦,🐛了!")
|
||||
}
|
||||
if dhash != "" && !noimg {
|
||||
b14, err3 := url.QueryUnescape(dhash)
|
||||
if err3 == nil {
|
||||
ctx.Send("给你点提示哦:" + b14)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var last_message_id int64
|
||||
if !noimg {
|
||||
last_message_id = ctx.SendChain(message.Image(CACHE_URI).Add("no_cache", "0"))
|
||||
last_group_id := ctx.Event.GroupID
|
||||
MsgofGrp[last_group_id] = last_message_id
|
||||
dhashofmsg[last_message_id] = dhash
|
||||
} else {
|
||||
last_message_id = ctx.Event.MessageID
|
||||
}
|
||||
switch class {
|
||||
case 0:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[0]一堆像素点"))
|
||||
case 1:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[1]普通"))
|
||||
case 2:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[2]有点意思"))
|
||||
case 3:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[3]不错"))
|
||||
case 4:
|
||||
ctx.SendChain(message.Reply(last_message_id), message.Text("[4]我好啦!"))
|
||||
}
|
||||
}
|
||||
}
|
||||
105
plugin_rand_image/rand_image.go
Normal file
105
plugin_rand_image/rand_image.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package plugin_rand_image
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
)
|
||||
|
||||
var (
|
||||
RANDOM_API_URL = "https://api.pixivweb.com/anime18r.php?return=img"
|
||||
BLOCK_REQUEST = false
|
||||
)
|
||||
|
||||
func init() { // 插件主体
|
||||
zero.OnRegex(`^设置随机图片网址(.*)$`, zero.SuperUserPermission).SetBlock(true).SetPriority(20).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
url := ctx.State["regex_matched"].([]string)[1]
|
||||
if !strings.HasPrefix(url, "http") {
|
||||
ctx.Send("URL非法!")
|
||||
} else {
|
||||
RANDOM_API_URL = url
|
||||
}
|
||||
return
|
||||
})
|
||||
// 有保护的随机图片
|
||||
zero.OnFullMatch("随机图片").SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
if ctx.Event.GroupID > 0 {
|
||||
Classify(ctx, RANDOM_API_URL, false)
|
||||
}
|
||||
return
|
||||
})
|
||||
// 直接随机图片,危险,仅管理员可用
|
||||
zero.OnFullMatch("直接随机", zero.AdminPermission).SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
if ctx.Event.GroupID > 0 {
|
||||
if BLOCK_REQUEST {
|
||||
ctx.Send("请稍后再试哦")
|
||||
} else {
|
||||
BLOCK_REQUEST = true
|
||||
last_message_id := ctx.SendChain(message.Image(RANDOM_API_URL).Add("cache", "0"))
|
||||
last_group_id := ctx.Event.GroupID
|
||||
MsgofGrp[last_group_id] = last_message_id
|
||||
BLOCK_REQUEST = false
|
||||
}
|
||||
}
|
||||
return
|
||||
})
|
||||
// 撤回最后的随机图片
|
||||
zero.OnFullMatch("不许好").SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
Vote(ctx, 5)
|
||||
})
|
||||
// 撤回最后的随机图片
|
||||
zero.OnFullMatch("太涩了").SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
Vote(ctx, 6)
|
||||
})
|
||||
// 上传一张图进行评价
|
||||
zero.OnFullMatch("评价图片").SetBlock(true).SetPriority(24).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
// 匹配图片
|
||||
rule := func() zero.Rule {
|
||||
return func(ctx *zero.Ctx) bool {
|
||||
var urls = []string{}
|
||||
for _, elem := range ctx.Event.Message {
|
||||
if elem.Type == "image" {
|
||||
urls = append(urls, elem.Data["url"])
|
||||
}
|
||||
}
|
||||
if len(urls) > 0 {
|
||||
ctx.State["image_url"] = urls
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
// 索取图片
|
||||
ctx.SendChain(message.Text("请发送一张图片"))
|
||||
next := zero.NewFutureEvent("message", 999, false, zero.CheckUser(ctx.Event.UserID), rule())
|
||||
recv, cancel := next.Repeat()
|
||||
select {
|
||||
case <-time.After(time.Second * 120):
|
||||
return
|
||||
case e := <-recv:
|
||||
cancel()
|
||||
newCtx := &zero.Ctx{Event: e, State: zero.State{}}
|
||||
if rule()(newCtx) {
|
||||
ctx.State["image_url"] = newCtx.State["image_url"]
|
||||
}
|
||||
}
|
||||
if ctx.Event.GroupID > 0 {
|
||||
ctx.Send("少女祈祷中......")
|
||||
for _, pic := range ctx.State["image_url"].([]string) {
|
||||
fmt.Println(pic)
|
||||
Classify(ctx, pic, true)
|
||||
}
|
||||
}
|
||||
return
|
||||
})
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user