适配多群组

This commit is contained in:
fumiama 2021-06-17 21:15:38 +08:00
parent cda7bec982
commit 445d2e96ce

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/msgext" "github.com/Yiwen-Chan/ZeroBot-Plugin/api/msgext"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
@ -21,10 +20,8 @@ var (
BLOCK_REQUEST = false BLOCK_REQUEST = false
CACHE_IMG_FILE = "/tmp/setugt" CACHE_IMG_FILE = "/tmp/setugt"
CACHE_URI = "file:///" + CACHE_IMG_FILE CACHE_URI = "file:///" + CACHE_IMG_FILE
last_message_id int64 msgofgrp = make(map[int64]int64)
last_dhash string dhashofmsg = make(map[int64]string)
last_visit = 0
last_group_id int64
) )
func init() { // 插件主体 func init() { // 插件主体
@ -42,10 +39,9 @@ func init() { // 插件主体
zero.OnFullMatch("随机图片").SetBlock(true).SetPriority(24). zero.OnFullMatch("随机图片").SetBlock(true).SetPriority(24).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
if ctx.Event.GroupID > 0 { if ctx.Event.GroupID > 0 {
if BLOCK_REQUEST && time.Now().Unix()-last_message_id < 30 { if BLOCK_REQUEST {
ctx.Send("请稍后再试哦") ctx.Send("请稍后再试哦")
} else { } else {
last_message_id = time.Now().Unix()
BLOCK_REQUEST = true BLOCK_REQUEST = true
if CLASSIFY_RANDOM_API_URL != "" { if CLASSIFY_RANDOM_API_URL != "" {
resp, err := http.Get(CLASSIFY_RANDOM_API_URL) resp, err := http.Get(CLASSIFY_RANDOM_API_URL)
@ -72,9 +68,10 @@ func init() { // 插件主体
} }
} }
} else { } else {
last_message_id = ctx.Send(msgext.ImageNoCache(CACHE_URI)) last_message_id := ctx.Send(msgext.ImageNoCache(CACHE_URI))
last_dhash = dhash last_group_id := ctx.Event.GroupID
last_group_id = ctx.Event.GroupID msgofgrp[last_group_id] = last_message_id
dhashofmsg[last_message_id] = dhash
if class > 2 { if class > 2 {
ctx.Send("我好啦!") ctx.Send("我好啦!")
} }
@ -91,22 +88,23 @@ func init() { // 插件主体
}) })
zero.OnFullMatch("不许好").SetBlock(true).SetPriority(24). zero.OnFullMatch("不许好").SetBlock(true).SetPriority(24).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
if last_message_id != 0 && last_group_id == ctx.Event.GroupID { vote(ctx, 5)
ctx.DeleteMessage(last_message_id)
last_message_id = 0
vote(5)
}
}) })
zero.OnFullMatch("太涩了").SetBlock(true).SetPriority(24). zero.OnFullMatch("太涩了").SetBlock(true).SetPriority(24).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
if last_message_id != 0 && last_group_id == ctx.Event.GroupID { vote(ctx, 6)
ctx.DeleteMessage(last_message_id)
last_message_id = 0
vote(6)
}
}) })
} }
func vote(class int) { func vote(ctx *zero.Ctx, class int) {
http.Get(fmt.Sprintf(VOTE_API_URL, last_dhash, class)) 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)
}
}
} }