/
main.go
277 lines (269 loc) · 7.74 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Package drawlots 多功能抽签插件
package drawlots
import (
"bytes"
"errors"
"image"
"image/draw"
"image/gif"
"io"
"math/rand"
"os"
"strconv"
"strings"
fcext "github.com/FloatTech/floatbox/ctxext"
"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"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/img/text"
"github.com/fumiama/jieba/util/helper"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
type info struct {
lotsType string // 文件后缀
quantity int // 签数
}
var (
lotsList = func() map[string]info {
lotsList, err := getList()
if err != nil {
logrus.Infoln("[drawlots]加载失败:", err)
} else {
logrus.Infoln("[drawlots]加载", len(lotsList), "个抽签")
}
return lotsList
}()
en = control.AutoRegister(&ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "多功能抽签",
Help: "支持图包文件夹和gif抽签\n" +
"-------------\n" +
"- (刷新)抽签列表\n- 抽[签名]签\n- 看[gif签名]签\n- 加[签名]签[gif图片]\n- 删[gif签名]签",
PrivateDataFolder: "drawlots",
}).ApplySingle(ctxext.DefaultSingle)
datapath = file.BOTPATH + "/" + en.DataFolder()
)
func init() {
en.OnFullMatchGroup([]string{"抽签列表", "刷新抽签列表"}).SetBlock(true).Handle(func(ctx *zero.Ctx) {
var err error
lotsList, err = getList() // 刷新列表
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
messageText := &strings.Builder{}
messageText.WriteString(" 签 名 [ 类 型 ]----签数\n")
messageText.WriteString("———————————\n")
for name, fileInfo := range lotsList {
messageText.WriteString(name + "[" + fileInfo.lotsType + "]----" + strconv.Itoa(fileInfo.quantity) + "\n")
messageText.WriteString("----------\n")
}
textPic, err := text.RenderToBase64(messageText.String(), text.BoldFontFile, 400, 50)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Image("base64://" + helper.BytesToString(textPic)))
})
en.OnRegex(`^抽(.+)签$`).SetBlock(true).Handle(func(ctx *zero.Ctx) {
lotsType := ctx.State["regex_matched"].([]string)[1]
fileInfo, ok := lotsList[lotsType]
if !ok {
ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text("才...才没有", lotsType, "签这种东西啦")))
return
}
if fileInfo.lotsType == "folder" {
picPath, err := randFile(lotsType, 3)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image("file:///"+picPath))
return
}
lotsImg, err := randGif(lotsType+"."+fileInfo.lotsType, ctx.Event.UserID)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
// 生成图片
data, err := imgfactory.ToBytes(lotsImg)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.ImageBytes(data))
})
en.OnRegex(`^看(.+)签$`, zero.UserOrGrpAdmin).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
id := ctx.Event.MessageID
lotsName := ctx.State["regex_matched"].([]string)[1]
fileInfo, ok := lotsList[lotsName]
if !ok {
ctx.Send(message.ReplyWithMessage(id, message.Text("才...才没有", lotsName, "签这种东西啦")))
return
}
if fileInfo.lotsType == "folder" {
ctx.Send(message.ReplyWithMessage(id, message.Text("只能查看gif签哦~")))
return
}
ctx.Send(message.ReplyWithMessage(id, message.Image("file:///"+datapath+lotsName+"."+fileInfo.lotsType)))
})
en.OnRegex(`^加(.+)签.*`, zero.SuperUserPermission, zero.MustProvidePicture).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
id := ctx.Event.MessageID
lotsName := ctx.State["regex_matched"].([]string)[1]
if lotsName == "" {
ctx.Send(message.ReplyWithMessage(id, message.Text("请使用正确的指令形式哦~")))
return
}
picURL := ctx.State["image_url"].([]string)[0]
gifdata, err := web.GetData(picURL)
if err != nil {
return
}
im, err := gif.DecodeAll(bytes.NewReader(gifdata))
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
fileName := datapath + "/" + lotsName + ".gif"
err = file.DownloadTo(picURL, fileName)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
lotsList[lotsName] = info{
lotsType: "gif",
quantity: len(im.Image),
}
ctx.Send(message.ReplyWithMessage(id, message.Text("成功!")))
})
en.OnRegex(`^删(.+)签$`, zero.SuperUserPermission).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
id := ctx.Event.MessageID
lotsName := ctx.State["regex_matched"].([]string)[1]
fileInfo, ok := lotsList[lotsName]
if !ok {
ctx.Send(message.ReplyWithMessage(id, message.Text("才...才没有", lotsName, "签这种东西啦")))
return
}
if fileInfo.lotsType == "folder" {
ctx.Send(message.ReplyWithMessage(id, message.Text("为了防止误删图源,图包请手动移除哦~")))
return
}
err := os.Remove(datapath + lotsName + "." + fileInfo.lotsType)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
delete(lotsList, lotsName)
ctx.Send(message.ReplyWithMessage(id, message.Text("成功!")))
})
}
func getList() (list map[string]info, err error) {
list = make(map[string]info, 100)
files, err := os.ReadDir(datapath)
if err != nil {
return
}
if len(files) == 0 {
err = errors.New("什么签也没有哦~")
return
}
for _, lots := range files {
if lots.IsDir() {
files, _ := os.ReadDir(datapath + "/" + lots.Name())
list[lots.Name()] = info{
lotsType: "folder",
quantity: len(files),
}
continue
}
before, after, ok := strings.Cut(lots.Name(), ".")
if !ok || before == "" {
continue
}
file, err := os.Open(datapath + "/" + lots.Name())
if err != nil {
return nil, err
}
im, err := gif.DecodeAll(file)
_ = file.Close()
if err != nil {
return nil, err
}
list[before] = info{
lotsType: after,
quantity: len(im.Image),
}
}
return
}
func randFile(path string, indexMax int) (string, error) {
picPath := datapath + path
files, err := os.ReadDir(picPath)
if err != nil {
return "", err
}
if len(files) > 0 {
drawFile := files[rand.Intn(len(files))]
// 如果是文件夹就递归
if drawFile.IsDir() {
indexMax--
if indexMax <= 0 {
return "", errors.New("图包[" + path + "]存在太多非图片文件,请清理~")
}
return randFile(path, indexMax)
}
return picPath + "/" + drawFile.Name(), err
}
return "", errors.New("图包[" + path + "]不存在签内容!")
}
func randGif(gifName string, uid int64) (image.Image, error) {
name := datapath + gifName
file, err := os.Open(name)
if err != nil {
return nil, err
}
im, err := gif.DecodeAll(file)
if err != nil {
return nil, err
}
_, err = file.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
config, err := gif.DecodeConfig(file)
if err != nil {
return nil, err
}
_ = file.Close()
// https://zhuanlan.zhihu.com/p/27718135
rect := image.Rect(0, 0, config.Width, config.Height)
if rect.Min == rect.Max {
var max image.Point
for _, frame := range im.Image {
maxF := frame.Bounds().Max
if max.X < maxF.X {
max.X = maxF.X
}
if max.Y < maxF.Y {
max.Y = maxF.Y
}
}
rect.Max = max
}
img := image.NewRGBA(rect)
b := fcext.RandSenderPerDayN(uid, len(im.Image)) + 1
a := 0
if b > 8 {
a = b - 8
}
for _, srcimg := range im.Image[a:b] {
draw.Draw(img, srcimg.Bounds(), srcimg, srcimg.Rect.Min, draw.Over)
}
return img, err
}