-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathrules.go
312 lines (287 loc) · 7.93 KB
/
rules.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package zero
import (
"hash/crc64"
"regexp"
"strconv"
"strings"
"time"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
)
// Type check the ctx.Event's type
func Type(typ string) Rule {
t := strings.SplitN(typ, "/", 3)
return func(ctx *Ctx) bool {
if len(t) > 0 && t[0] != ctx.Event.PostType {
return false
}
if len(t) > 1 && t[1] != ctx.Event.DetailType {
return false
}
if len(t) > 2 && t[2] != ctx.Event.SubType {
return false
}
return true
}
}
// PrefixRule check if the message has the prefix and trim the prefix
//
// 检查消息前缀
func PrefixRule(prefixes ...string) Rule {
return func(ctx *Ctx) bool {
if len(ctx.Event.Message) == 0 || ctx.Event.Message[0].Type != "text" { // 确保无空指针
return false
}
first := ctx.Event.Message[0]
firstMessage := first.Data["text"]
for _, prefix := range prefixes {
if strings.HasPrefix(firstMessage, prefix) {
ctx.State["prefix"] = prefix
arg := strings.TrimLeft(firstMessage[len(prefix):], " ")
if len(ctx.Event.Message) > 1 {
arg += ctx.Event.Message[1:].ExtractPlainText()
}
ctx.State["args"] = arg
return true
}
}
return false
}
}
// SuffixRule check if the message has the suffix and trim the suffix
//
// 检查消息后缀
func SuffixRule(suffixes ...string) Rule {
return func(ctx *Ctx) bool {
mLen := len(ctx.Event.Message)
if mLen <= 0 { // 确保无空指针
return false
}
last := ctx.Event.Message[mLen-1]
if last.Type != "text" {
return false
}
lastMessage := last.Data["text"]
for _, suffix := range suffixes {
if strings.HasSuffix(lastMessage, suffix) {
ctx.State["suffix"] = suffix
arg := strings.TrimRight(lastMessage[:len(lastMessage)-len(suffix)], " ")
if mLen >= 2 {
arg += ctx.Event.Message[:mLen].ExtractPlainText()
}
ctx.State["args"] = arg
return true
}
}
return false
}
}
// CommandRule check if the message is a command and trim the command name
func CommandRule(commands ...string) Rule {
return func(ctx *Ctx) bool {
if len(ctx.Event.Message) == 0 || ctx.Event.Message[0].Type != "text" {
return false
}
first := ctx.Event.Message[0]
firstMessage := first.Data["text"]
if !strings.HasPrefix(firstMessage, BotConfig.CommandPrefix) {
return false
}
cmdMessage := firstMessage[len(BotConfig.CommandPrefix):]
for _, command := range commands {
if strings.HasPrefix(cmdMessage, command) {
ctx.State["command"] = command
arg := strings.TrimLeft(cmdMessage[len(command):], " ")
if len(ctx.Event.Message) > 1 {
arg += ctx.Event.Message[1:].ExtractPlainText()
}
ctx.State["args"] = arg
return true
}
}
return false
}
}
// RegexRule check if the message can be matched by the regex pattern
func RegexRule(regexPattern string) Rule {
regex := regexp.MustCompile(regexPattern)
return func(ctx *Ctx) bool {
msg := ctx.MessageString()
if matched := regex.FindStringSubmatch(msg); matched != nil {
ctx.State["regex_matched"] = matched
return true
}
return false
}
}
// ReplyRule check if the message is replying some message
func ReplyRule(messageID int64) Rule {
return func(ctx *Ctx) bool {
if len(ctx.Event.Message) == 0 {
return false
}
if ctx.Event.Message[0].Type != "reply" {
return false
}
if id, err := strconv.ParseInt(ctx.Event.Message[0].Data["id"], 10, 64); err == nil {
return id == messageID
}
c := crc64.New(crc64.MakeTable(crc64.ISO))
c.Write(helper.StringToBytes(ctx.Event.Message[0].Data["id"]))
return int64(c.Sum64()) == messageID
}
}
// KeywordRule check if the message has a keyword or keywords
func KeywordRule(src ...string) Rule {
return func(ctx *Ctx) bool {
msg := ctx.MessageString()
for _, str := range src {
if strings.Contains(msg, str) {
ctx.State["keyword"] = str
return true
}
}
return false
}
}
// FullMatchRule check if src has the same copy of the message
func FullMatchRule(src ...string) Rule {
return func(ctx *Ctx) bool {
msg := ctx.MessageString()
for _, str := range src {
if str == msg {
ctx.State["matched"] = msg
return true
}
}
return false
}
}
// OnlyToMe only triggered in conditions of @bot or begin with the nicknames
func OnlyToMe(ctx *Ctx) bool {
return ctx.Event.IsToMe
}
// CheckUser only triggered by specific person
func CheckUser(userID ...int64) Rule {
return func(ctx *Ctx) bool {
for _, uid := range userID {
if ctx.Event.UserID == uid {
return true
}
}
return false
}
}
// CheckGroup only triggered in specific group
func CheckGroup(grpID ...int64) Rule {
return func(ctx *Ctx) bool {
for _, gid := range grpID {
if ctx.Event.GroupID == gid {
return true
}
}
return false
}
}
// OnlyPrivate requires that the ctx.Event is private message
func OnlyPrivate(ctx *Ctx) bool {
return ctx.Event.PostType == "message" && ctx.Event.DetailType == "private"
}
// OnlyPublic requires that the ctx.Event is public/group or public/guild message
func OnlyPublic(ctx *Ctx) bool {
return ctx.Event.PostType == "message" && (ctx.Event.DetailType == "group" || ctx.Event.DetailType == "guild")
}
// OnlyGroup requires that the ctx.Event is public/group message
func OnlyGroup(ctx *Ctx) bool {
return ctx.Event.PostType == "message" && ctx.Event.DetailType == "group"
}
// OnlyGuild requires that the ctx.Event is public/guild message
func OnlyGuild(ctx *Ctx) bool {
return ctx.Event.PostType == "message" && ctx.Event.DetailType == "guild"
}
func issu(id int64) bool {
for _, su := range BotConfig.SuperUsers {
if su == id {
return true
}
}
return false
}
// SuperUserPermission only triggered by the bot's owner
func SuperUserPermission(ctx *Ctx) bool {
return issu(ctx.Event.UserID)
}
// AdminPermission only triggered by the group admins or higher permission
func AdminPermission(ctx *Ctx) bool {
return SuperUserPermission(ctx) || ctx.Event.Sender.Role == "owner" || ctx.Event.Sender.Role == "admin"
}
// OwnerPermission only triggered by the group owner or higher permission
func OwnerPermission(ctx *Ctx) bool {
return SuperUserPermission(ctx) || ctx.Event.Sender.Role == "owner"
}
// UserOrGrpAdmin 允许用户单独使用或群管使用
func UserOrGrpAdmin(ctx *Ctx) bool {
if OnlyGroup(ctx) {
return AdminPermission(ctx)
}
return OnlyToMe(ctx)
}
// GroupHigherPermission 群发送者权限高于 target
//
// 隐含 OnlyGroup 判断
func GroupHigherPermission(gettarget func(ctx *Ctx) int64) Rule {
return func(ctx *Ctx) bool {
if !OnlyGroup(ctx) {
return false
}
target := gettarget(ctx)
if target == ctx.Event.UserID { // 特判, 自己和自己比
return false
}
if SuperUserPermission(ctx) {
sender := ctx.Event.UserID
return BotConfig.GetFirstSuperUser(sender, target) == sender
}
if ctx.Event.Sender.Role == "owner" {
return !issu(target) && ctx.GetThisGroupMemberInfo(target, false).Get("role").Str != "owner"
}
if ctx.Event.Sender.Role == "admin" {
tgtrole := ctx.GetThisGroupMemberInfo(target, false).Get("role").Str
return !issu(target) && tgtrole != "owner" && tgtrole != "admin"
}
return false // member is the lowest
}
}
// HasPicture 消息含有图片返回 true
func HasPicture(ctx *Ctx) bool {
var urls = []string{}
for _, elem := range ctx.Event.Message {
if elem.Type == "image" {
if elem.Data["url"] != "" {
urls = append(urls, elem.Data["url"])
}
}
}
if len(urls) > 0 {
ctx.State["image_url"] = urls
return true
}
return false
}
// MustProvidePicture 消息不存在图片阻塞120秒至有图片,超时返回 false
func MustProvidePicture(ctx *Ctx) bool {
if HasPicture(ctx) {
return true
}
// 没有图片就索取
ctx.SendChain(message.Text("请发送一张图片"))
next := NewFutureEvent("message", 999, true, ctx.CheckSession(), HasPicture).Next()
select {
case <-time.After(time.Second * 120):
return false
case newCtx := <-next:
ctx.State["image_url"] = newCtx.State["image_url"]
ctx.Event.MessageID = newCtx.Event.MessageID
return true
}
}