Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改加签逻辑 #620

Merged
merged 4 commits into from
Mar 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions plugin/drawlots/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
package drawlots

import (
"bytes"
"errors"
"image"
"image/color"
"image/gif"
"math/rand"
"os"
"strconv"
"strings"

"github.com/FloatTech/floatbox/file"
"github.com/FloatTech/floatbox/web"
"github.com/FloatTech/imgfactory"
ctrl "github.com/FloatTech/zbpctrl"
control "github.com/FloatTech/zbputils/control"
Expand Down Expand Up @@ -38,9 +41,11 @@ var (
return lotsList
}()
en = control.Register("drawlots", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "多功能抽签",
Help: "支持图包文件夹和gif抽签\n-------------\n- (刷新)抽签列表\n- 抽[签名]签\n- 看签[gif签名]\n- 加签[签名][gif图片]\n- 删签[gif签名]",
DisableOnDefault: false,
Brief: "多功能抽签",
Help: "支持图包文件夹和gif抽签\n" +
"-------------\n" +
"- (刷新)抽签列表\n- 抽[签名]签\n- 看[gif签名]签\n- 加[签名]签[gif图片]\n- 删[gif签名]签",
PrivateDataFolder: "drawlots",
}).ApplySingle(ctxext.DefaultSingle)
datapath = file.BOTPATH + "/" + en.DataFolder()
Expand Down Expand Up @@ -96,9 +101,9 @@ func init() {
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.ImageBytes(data))
})
en.OnPrefix("看签", zero.UserOrGrpAdmin).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
en.OnRegex(`^看(.+)签$`, zero.UserOrGrpAdmin).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
id := ctx.Event.MessageID
lotsName := strings.TrimSpace(ctx.State["args"].(string))
lotsName := ctx.State["regex_matched"].([]string)[1]
fileInfo, ok := lotsList[lotsName]
if !ok {
ctx.Send(message.ReplyWithMessage(id, message.Text("才...才没有", lotsName, "签这种东西啦")))
Expand All @@ -110,26 +115,25 @@ func init() {
}
ctx.Send(message.ReplyWithMessage(id, message.Image("file:///"+datapath+lotsName+"."+fileInfo.lotsType)))
})
en.OnPrefix("加签", zero.SuperUserPermission, zero.MustProvidePicture).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
en.OnRegex(`^加(.+)签.*`, zero.SuperUserPermission, zero.MustProvidePicture).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
id := ctx.Event.MessageID
lotsName := strings.TrimSpace(ctx.State["args"].(string))
lotsName := ctx.State["regex_matched"].([]string)[1]
if lotsName == "" {
ctx.Send(message.ReplyWithMessage(id, message.Text("请使用正确的指令形式哦~")))
return
}
picURL := ctx.State["image_url"].([]string)[0]
err := file.DownloadTo(picURL, datapath+"/"+lotsName+".gif")
gifdata, err := web.GetData(picURL)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
file, err := os.Open(datapath + "/" + lotsName + ".gif")
im, err := gif.DecodeAll(bytes.NewReader(gifdata))
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
im, err := gif.DecodeAll(file)
_ = file.Close()
fileName := datapath + "/" + lotsName + ".gif"
err = file.DownloadTo(picURL, fileName)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
Expand All @@ -140,9 +144,9 @@ func init() {
}
ctx.Send(message.ReplyWithMessage(id, message.Text("成功!")))
})
en.OnPrefix("删签", zero.SuperUserPermission).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
en.OnRegex(`^删(.+)签$`, zero.SuperUserPermission).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
id := ctx.Event.MessageID
lotsName := strings.TrimSpace(ctx.State["args"].(string))
lotsName := ctx.State["regex_matched"].([]string)[1]
fileInfo, ok := lotsList[lotsName]
if !ok {
ctx.Send(message.ReplyWithMessage(id, message.Text("才...才没有", lotsName, "签这种东西啦")))
Expand Down Expand Up @@ -244,6 +248,6 @@ func randGif(gifName string) (image.Image, error) {
/*/
// 如果gif图片出现信息缺失请使用上面注释掉的代码,把下面注释了(上面代码部分图存在bug)
v := im.Image[rand.Intn(len(im.Image))]
return imgfactory.Size(v, v.Bounds().Max.X, v.Bounds().Max.Y).Image(), err
return imgfactory.NewFactoryBG(v.Rect.Max.X, v.Rect.Max.Y, color.NRGBA{0, 0, 0, 255}).InsertUp(v, 0, 0, 0, 0).Clone().Image(), err
// */
}