-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
139 lines (112 loc) · 4.08 KB
/
types.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
// 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 (
"sync"
"time"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters"
)
// UserStatus is the status of a user in the map.
type UserStatus struct {
// Last field is the last time that we received a message
// from the user.
Last time.Time
// limited will be true if and only if the current user is
// banned in the limiter.
limited bool
// count is the counts of the messages of the user received
// by limiter.
count int
custom *customIgnore
}
type customIgnore struct {
startTime time.Time
duration time.Duration
ignoreException bool
}
// Limiter is the main struct of this library.
type Limiter struct {
mutex *sync.RWMutex
// IsEnable will be true if and only if the limiter is enabled
// and should check for the incoming messages.
isEnabled bool
// IsStopped will be false when the limiter is stopped.
isStopped bool
// userMap is a map of user status with their user id
// as its key (int64).
userMap map[int64]*UserStatus
// trigger function will run when a user is limited
// by the limiter. It should be set by user, users can do everything
// they want in this function, such as logging the person's id who
// has been limited by the limiter, etc...
triggers []handlers.Response
filter filters.Message
handler handlers.Response
// msgHandler is the original message handler of this limiter.
// it should remain private.
msgHandler *handlers.Message
allHandlers []ext.Handler
exceptions []filters.Message
conditions []filters.Message
exceptionIDs []int64
ignoredExceptions []int64
// timeout is the floodwait checking time. a user is allowed to
// send `maxCount` messages per `timeout`.
timeout time.Duration
// maxTimeout is the maximum time out of clearing user status
// cache in the memory.
maxTimeout time.Duration
// punishment is the necessary time a user needs to spend after
// being limiter as its punishment; the user will be freed after
// this time is passed.
punishment time.Duration
// maxCount is the maximum number of messages we can accept from the
// user in `timeout` amount of time; if the user sends more than
// this much message, it will be limited and so the bot will ignore
// their messages.
maxCount int
// IgnoreMediaGroup should be set to true when we have to ignore
// album messages (such as album musics, album photos, etc...) and
// don't check them at all.
// default value for this field is true.
IgnoreMediaGroup bool
// TextOnly should be set to true when we have to ignore
// media messages (such as photos, videos, audios, etc...) and
// don't check them at all.
// If your bot has nothing to do with media messages, you can set
// this to true.
TextOnly bool
// IsStrict will tell the limiter whether it should act more strict
// or not. If this value is set to `true`, the user should NOT send
// any messages to the bot until it's limit time is completely over.
// otherwise the limitation will remain on the user until it stops
// sending any messages to the bot.
// (A truly bad way of handling anti-floodwait... we recommend not to
// set this value to `true`, unless it's very very necessary).
IsStrict bool
// ConsiderUser will be true when the limiter needs to consider users
// for their checking the messages. so the user's ID will be used as key
// to access the map.
ConsiderUser bool
// ConsiderInline fields will determine whether we need to
ConsiderInline bool
}
// LimiterConfig is the config type of the limiter.
type LimiterConfig struct {
ConsiderChannel bool
ConsiderUser bool
ConsiderEdits bool
IgnoreMediaGroup bool
TextOnly bool
IsStrict bool
HandlerGroups []int
ConsiderInline bool
Timeout time.Duration
PunishmentTime time.Duration
MaxTimeout time.Duration
MessageCount int
}