forked from fumiama/ReiBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
518 lines (435 loc) · 15.8 KB
/
engine.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package rei
// 生成空引擎
func newEngine() *Engine {
return &Engine{
preHandler: []Rule{},
midHandler: []Rule{},
postHandler: []Process{},
}
}
var defaultEngine = newEngine()
// Engine is the pre_handler, mid_handler, post_handler manager
type Engine struct {
preHandler []Rule
midHandler []Rule
postHandler []Process
matchers []*Matcher
prio int
service string
datafolder string
}
// Delete 移除该 Engine 注册的所有 Matchers
func (e *Engine) Delete() {
for _, m := range e.matchers {
m.Delete()
}
}
// UsePreHandler 向该 Engine 添加新 PreHandler(Rule),
// 会在 Rule 判断前触发,如果 preHandler
// 没有通过,则 Rule, Matcher 不会触发
//
// 可用于分群组管理插件等
func (e *Engine) UsePreHandler(rules ...Rule) {
e.preHandler = append(e.preHandler, rules...)
}
// UseMidHandler 向该 Engine 添加新 MidHandler(Rule),
// 会在 Rule 判断后, Matcher 触发前触发,如果 midHandler
// 没有通过,则 Matcher 不会触发
//
// 可用于速率限制等
func (e *Engine) UseMidHandler(rules ...Rule) {
e.midHandler = append(e.midHandler, rules...)
}
// UsePostHandler 向该 Engine 添加新 PostHandler(Rule),
// 会在 Matcher 触发后触发,如果 PostHandler 返回 false,
// 则后续的 post handler 不会触发
//
// 可用于速率限制等
func (e *Engine) UsePostHandler(handler ...Process) {
e.postHandler = append(e.postHandler, handler...)
}
// ApplySingle 应用反并发
func (e *Engine) ApplySingle(s *Single[int64]) *Engine {
s.Apply(e)
return e
}
// DataFolder 本插件数据目录, 默认 data/rbp/
func (e *Engine) DataFolder() string {
return e.datafolder
}
// On 添加新的指定消息类型的匹配器(默认Engine)
func On(typ string, rules ...Rule) *Matcher { return defaultEngine.On(typ, rules...) }
// On 添加新的指定消息类型的匹配器
func (e *Engine) On(typ string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: typ,
Rules: rules,
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessage 消息触发器
func (e *Engine) OnMessage(rules ...Rule) *Matcher { return e.On("Message", rules...) }
// OnMessage 消息触发器
func OnMessage(rules ...Rule) *Matcher { return On("Message", rules...) }
// OnEditedMessage 修改消息触发器
func (e *Engine) OnEditedMessage(rules ...Rule) *Matcher { return e.On("EditedMessage", rules...) }
// OnEditedMessage 修改消息触发器
func OnEditedMessage(rules ...Rule) *Matcher { return On("EditedMessage", rules...) }
// OnChannelPost ...
func (e *Engine) OnChannelPost(rules ...Rule) *Matcher { return e.On("ChannelPost", rules...) }
// OnChannelPost ...
func OnChannelPost(rules ...Rule) *Matcher { return On("ChannelPost", rules...) }
// OnEditedChannelPost ...
func (e *Engine) OnEditedChannelPost(rules ...Rule) *Matcher {
return e.On("EditedChannelPost", rules...)
}
// OnEditedChannelPost ...
func OnEditedChannelPost(rules ...Rule) *Matcher {
return On("EditedChannelPost", rules...)
}
// OnInlineQuery ...
func (e *Engine) OnInlineQuery(rules ...Rule) *Matcher { return e.On("InlineQuery", rules...) }
// OnInlineQuery ...
func OnInlineQuery(rules ...Rule) *Matcher { return On("InlineQuery", rules...) }
// OnChosenInlineResult ...
func (e *Engine) OnChosenInlineResult(rules ...Rule) *Matcher {
return e.On("ChosenInlineResult", rules...)
}
// OnChosenInlineResult ...
func OnChosenInlineResult(rules ...Rule) *Matcher { return On("ChosenInlineResult", rules...) }
// OnCallbackQuery ...
func (e *Engine) OnCallbackQuery(rules ...Rule) *Matcher { return e.On("CallbackQuery", rules...) }
// OnCallbackQuery ...
func OnCallbackQuery(rules ...Rule) *Matcher { return On("CallbackQuery", rules...) }
// OnShippingQuery ...
func (e *Engine) OnShippingQuery(rules ...Rule) *Matcher { return e.On("ShippingQuery", rules...) }
// OnShippingQuery ...
func OnShippingQuery(rules ...Rule) *Matcher { return On("ShippingQuery", rules...) }
// OnPreCheckoutQuery ...
func (e *Engine) OnPreCheckoutQuery(rules ...Rule) *Matcher {
return e.On("PreCheckoutQuery", rules...)
}
// OnPreCheckoutQuery ...
func OnPreCheckoutQuery(rules ...Rule) *Matcher { return On("PreCheckoutQuery", rules...) }
// OnPoll ...
func (e *Engine) OnPoll(rules ...Rule) *Matcher { return e.On("Poll", rules...) }
// OnPoll ...
func OnPoll(rules ...Rule) *Matcher { return On("Poll", rules...) }
// OnPollAnswer ...
func (e *Engine) OnPollAnswer(rules ...Rule) *Matcher { return e.On("PollAnswer", rules...) }
// OnPollAnswer ...
func OnPollAnswer(rules ...Rule) *Matcher { return On("PollAnswer", rules...) }
// OnMyChatMember ...
func (e *Engine) OnMyChatMember(rules ...Rule) *Matcher { return e.On("MyChatMember", rules...) }
// OnMyChatMember ...
func OnMyChatMember(rules ...Rule) *Matcher { return On("MyChatMember", rules...) }
// OnChatMember ...
func (e *Engine) OnChatMember(rules ...Rule) *Matcher { return e.On("ChatMember", rules...) }
// OnChatMember ...
func OnChatMember(rules ...Rule) *Matcher { return On("ChatMember", rules...) }
// OnChatJoinRequest ...
func (e *Engine) OnChatJoinRequest(rules ...Rule) *Matcher { return e.On("ChatJoinRequest", rules...) }
// OnChatJoinRequest ...
func OnChatJoinRequest(rules ...Rule) *Matcher { return On("ChatJoinRequest", rules...) }
// OnMessagePrefix 前缀触发器
func OnMessagePrefix(prefix string, rules ...Rule) *Matcher {
return defaultEngine.OnMessagePrefix(prefix, rules...)
}
// OnMessageReactionUpdated React Action Updated
func (e *Engine) OnMessageReactionUpdated(rules ...Rule) *Matcher {
return e.On("MessageReaction", rules...)
}
// OnMessageReactionUpdated React Action Updated
func OnMessageReactionUpdated(rules ...Rule) *Matcher { return On("MessageReaction", rules...) }
// OnMessagePrefix 前缀触发器
func (e *Engine) OnMessagePrefix(prefix string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{PrefixRule(prefix)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageSuffix 后缀触发器
func OnMessageSuffix(suffix string, rules ...Rule) *Matcher {
return defaultEngine.OnMessageSuffix(suffix, rules...)
}
// OnMessageSuffix 后缀触发器
func (e *Engine) OnMessageSuffix(suffix string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{SuffixRule(suffix)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageCommand 命令触发器
func OnMessageCommand(commands string, rules ...Rule) *Matcher {
return defaultEngine.OnMessageCommand(commands, rules...)
}
// OnMessageCommand 命令触发器
func (e *Engine) OnMessageCommand(commands string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{CommandRule(commands)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageRegex 正则触发器
func OnMessageRegex(regexPattern string, rules ...Rule) *Matcher {
return defaultEngine.OnMessageRegex(regexPattern, rules...)
}
// OnMessageRegex 正则触发器
func (e *Engine) OnMessageRegex(regexPattern string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{RegexRule(regexPattern)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageKeyword 关键词触发器
func OnMessageKeyword(keyword string, rules ...Rule) *Matcher {
return defaultEngine.OnMessageKeyword(keyword, rules...)
}
// OnMessageKeyword 关键词触发器
func (e *Engine) OnMessageKeyword(keyword string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{KeywordRule(keyword)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageFullMatch 完全匹配触发器
func OnMessageFullMatch(src string, rules ...Rule) *Matcher {
return defaultEngine.OnMessageFullMatch(src, rules...)
}
// OnMessageFullMatch 完全匹配触发器
func (e *Engine) OnMessageFullMatch(src string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{FullMatchRule(src)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageFullMatchGroup 完全匹配触发器组
func OnMessageFullMatchGroup(src []string, rules ...Rule) *Matcher {
return defaultEngine.OnMessageFullMatchGroup(src, rules...)
}
// OnMessageFullMatchGroup 完全匹配触发器组
func (e *Engine) OnMessageFullMatchGroup(src []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{FullMatchRule(src...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageKeywordGroup 关键词触发器组
func OnMessageKeywordGroup(keywords []string, rules ...Rule) *Matcher {
return defaultEngine.OnMessageKeywordGroup(keywords, rules...)
}
// OnMessageKeywordGroup 关键词触发器组
func (e *Engine) OnMessageKeywordGroup(keywords []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{KeywordRule(keywords...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageCommandGroup 命令触发器组
func OnMessageCommandGroup(commands []string, rules ...Rule) *Matcher {
return defaultEngine.OnMessageCommandGroup(commands, rules...)
}
// OnMessageCommandGroup 命令触发器组
func (e *Engine) OnMessageCommandGroup(commands []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{CommandRule(commands...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessagePrefixGroup 前缀触发器组
func OnMessagePrefixGroup(prefix []string, rules ...Rule) *Matcher {
return defaultEngine.OnMessagePrefixGroup(prefix, rules...)
}
// OnMessagePrefixGroup 前缀触发器组
func (e *Engine) OnMessagePrefixGroup(prefix []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{PrefixRule(prefix...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageSuffixGroup 后缀触发器组
func OnMessageSuffixGroup(suffix []string, rules ...Rule) *Matcher {
return defaultEngine.OnMessageSuffixGroup(suffix, rules...)
}
// OnMessageSuffixGroup 后缀触发器组
func (e *Engine) OnMessageSuffixGroup(suffix []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{SuffixRule(suffix...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnMessageShell shell命令触发器
func OnMessageShell(command string, model interface{}, rules ...Rule) *Matcher {
return defaultEngine.OnMessageShell(command, model, rules...)
}
// OnMessageShell shell命令触发器
func (e *Engine) OnMessageShell(command string, model interface{}, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "Message",
Rules: append([]Rule{ShellRule(command, model)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCallbackQueryPrefix 前缀触发器
func OnCallbackQueryPrefix(prefix string, rules ...Rule) *Matcher {
return defaultEngine.OnCallbackQueryPrefix(prefix, rules...)
}
// OnCallbackQueryPrefix 前缀触发器
func (e *Engine) OnCallbackQueryPrefix(prefix string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "CallbackQuery",
Rules: append([]Rule{PrefixRule(prefix)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCallbackQuerySuffix 后缀触发器
func OnCallbackQuerySuffix(suffix string, rules ...Rule) *Matcher {
return defaultEngine.OnCallbackQuerySuffix(suffix, rules...)
}
// OnCallbackQuerySuffix 后缀触发器
func (e *Engine) OnCallbackQuerySuffix(suffix string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "CallbackQuery",
Rules: append([]Rule{SuffixRule(suffix)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCallbackQueryRegex 正则触发器
func OnCallbackQueryRegex(regexPattern string, rules ...Rule) *Matcher {
return defaultEngine.OnCallbackQueryRegex(regexPattern, rules...)
}
// OnCallbackQueryRegex 正则触发器
func (e *Engine) OnCallbackQueryRegex(regexPattern string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "CallbackQuery",
Rules: append([]Rule{RegexRule(regexPattern)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCallbackQueryKeyword 关键词触发器
func OnCallbackQueryKeyword(keyword string, rules ...Rule) *Matcher {
return defaultEngine.OnCallbackQueryKeyword(keyword, rules...)
}
// OnCallbackQueryKeyword 关键词触发器
func (e *Engine) OnCallbackQueryKeyword(keyword string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "CallbackQuery",
Rules: append([]Rule{KeywordRule(keyword)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCallbackQueryFullMatch 完全匹配触发器
func OnCallbackQueryFullMatch(src string, rules ...Rule) *Matcher {
return defaultEngine.OnCallbackQueryFullMatch(src, rules...)
}
// OnCallbackQueryFullMatch 完全匹配触发器
func (e *Engine) OnCallbackQueryFullMatch(src string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "CallbackQuery",
Rules: append([]Rule{FullMatchRule(src)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCallbackQueryFullMatchGroup 完全匹配触发器组
func OnCallbackQueryFullMatchGroup(src []string, rules ...Rule) *Matcher {
return defaultEngine.OnCallbackQueryFullMatchGroup(src, rules...)
}
// OnCallbackQueryFullMatchGroup 完全匹配触发器组
func (e *Engine) OnCallbackQueryFullMatchGroup(src []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "CallbackQuery",
Rules: append([]Rule{FullMatchRule(src...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCallbackQueryKeywordGroup 关键词触发器组
func OnCallbackQueryKeywordGroup(keywords []string, rules ...Rule) *Matcher {
return defaultEngine.OnCallbackQueryKeywordGroup(keywords, rules...)
}
// OnCallbackQueryKeywordGroup 关键词触发器组
func (e *Engine) OnCallbackQueryKeywordGroup(keywords []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "CallbackQuery",
Rules: append([]Rule{KeywordRule(keywords...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCallbackQueryPrefixGroup 前缀触发器组
func OnCallbackQueryPrefixGroup(prefix []string, rules ...Rule) *Matcher {
return defaultEngine.OnCallbackQueryPrefixGroup(prefix, rules...)
}
// OnCallbackQueryPrefixGroup 前缀触发器组
func (e *Engine) OnCallbackQueryPrefixGroup(prefix []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "CallbackQuery",
Rules: append([]Rule{PrefixRule(prefix...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}
// OnCallbackQuerySuffixGroup 后缀触发器组
func OnCallbackQuerySuffixGroup(suffix []string, rules ...Rule) *Matcher {
return defaultEngine.OnCallbackQuerySuffixGroup(suffix, rules...)
}
// OnCallbackQuerySuffixGroup 后缀触发器组
func (e *Engine) OnCallbackQuerySuffixGroup(suffix []string, rules ...Rule) *Matcher {
matcher := &Matcher{
Type: "CallbackQuery",
Rules: append([]Rule{SuffixRule(suffix...)}, rules...),
Engine: e,
}
e.matchers = append(e.matchers, matcher)
return StoreMatcher(matcher)
}