forked from wdvxdr1123/ZeroBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.go
209 lines (193 loc) · 5.2 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
package zero
import (
"regexp"
"strconv"
"strings"
)
// Type check the ctx.Event's type
func Type(type_ string) Rule {
t := strings.SplitN(type_, "/", 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.Event.Message.String()
if regex.MatchString(msg) {
ctx.State["regex_matched"] = regex.FindStringSubmatch(msg)
return true
}
return false
}
}
// ReplyRule check if the message is replying some message
func ReplyRule(messageID int64) Rule {
mid := strconv.FormatInt(messageID, 10)
return func(ctx *Ctx) bool {
if len(ctx.Event.Message) == 0 {
return false
}
if ctx.Event.Message[0].Type != "reply" {
return false
}
return ctx.Event.Message[0].Data["id"] == mid
}
}
// KeywordRule check if the message has a keyword or keywords
func KeywordRule(src ...string) Rule {
return func(ctx *Ctx) bool {
msg := ctx.Event.Message.ExtractPlainText()
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.Event.Message.String()
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
}
}
// OnlyPrivate requires that the ctx.Event is private message
func OnlyPrivate(ctx *Ctx) bool {
return ctx.Event.PostType == "message" && ctx.Event.DetailType == "private"
}
// OnlyGroup requires that the ctx.Event is public/group message
func OnlyGroup(ctx *Ctx) bool {
return ctx.Event.PostType == "message" && ctx.Event.DetailType == "group"
}
// SuperUserPermission only triggered by the bot's owner
func SuperUserPermission(ctx *Ctx) bool {
uid := strconv.FormatInt(ctx.Event.UserID, 10)
for _, su := range BotConfig.SuperUsers {
if su == uid {
return true
}
}
return false
}
// AdminPermission only triggered by the group admins or higher permission
func AdminPermission(ctx *Ctx) bool {
return SuperUserPermission(ctx) || ctx.Event.Sender.Role != "member"
}
// OwnerPermission only triggered by the group owner or higher permission
func OwnerPermission(ctx *Ctx) bool {
return SuperUserPermission(ctx) ||
(ctx.Event.Sender.Role != "member" && ctx.Event.Sender.Role != "admin")
}