-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
72 lines (61 loc) · 2.08 KB
/
helpers.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
// ratelimiter Project
// Copyright (C) 2021~2022 ALiwoto and other Contributors
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of the source code.
package ratelimiter
import (
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
)
// NewLimiter creates a new `Limiter` with the given dispatcher.
// pass true for the second parameter if you want the limiter to check
// messages in channels too.
// pass true for the third parameter if you want the limiter to check
// edited messages too.
func NewLimiter(dispatcher *ext.Dispatcher, config *LimiterConfig) *Limiter {
l := new(Limiter)
if config == nil {
config = DefaultConfig
}
l.filter = l.limiterFilter
l.handler = l.limiterHandler
l.timeout = config.Timeout
l.punishment = config.PunishmentTime
l.maxCount = config.MessageCount
l.maxTimeout = config.MaxTimeout
l.IgnoreMediaGroup = config.IgnoreMediaGroup
l.TextOnly = config.TextOnly
l.ConsiderUser = config.ConsiderUser
l.ConsiderInline = config.ConsiderInline
l.IsStrict = config.IsStrict
h := handlers.NewMessage(l.filter, l.handler)
cb := handlers.NewCallback(l.callbackFilter, l.handler)
l.msgHandler = &h
l.msgHandler.AllowChannel = config.ConsiderChannel
l.msgHandler.AllowEdited = config.ConsiderEdits
l.allHandlers = append(l.allHandlers, h, cb)
for _, currentHandler := range l.allHandlers {
if len(config.HandlerGroups) != 0 {
for _, current := range config.HandlerGroups {
dispatcher.AddHandlerToGroup(currentHandler, current)
}
} else {
dispatcher.AddHandler(currentHandler)
}
}
return l
}
// NewFullLimiter creates a new `Limiter` with the given dispatcher.
// it will initialize a limiter which checks for messages received from
// channels and edited messages.
func NewFullLimiter(dispatcher *ext.Dispatcher) *Limiter {
return NewLimiter(dispatcher, &LimiterConfig{
ConsiderChannel: true,
ConsiderUser: true,
ConsiderEdits: true,
IgnoreMediaGroup: false,
TextOnly: false,
IsStrict: false,
ConsiderInline: true,
})
}