-
Notifications
You must be signed in to change notification settings - Fork 269
/
floodProtection.js
56 lines (39 loc) · 1.18 KB
/
floodProtection.js
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
/*
Simple flood protection plugin.
Note: Received Telegram message time accuracy is one second.
*/
const userList = {};
// Export bot module
module.exports = {
id: 'floodProtection',
defaultConfig: {
interval: 1,
message: 'Too many messages, relax!'
},
plugin(bot, pluginConfig) {
const interval = Number(pluginConfig.interval) || 1;
const text = pluginConfig.message;
bot.mod('message', (data) => {
const msg = data.message;
const id = msg.from.id;
const user = userList[id];
const now = new Date(msg.date);
if (user) {
const diff = now - user.lastTime;
user.lastTime = now;
if (diff <= interval) {
if (!user.flood) {
if (text) bot.sendMessage(id, text);
user.flood = true;
}
data.message = {};
} else {
user.flood = false;
}
} else {
userList[id] = {lastTime: now};
}
return data;
});
}
};