Skip to content
This repository was archived by the owner on Oct 14, 2018. It is now read-only.

Commit bd7c061

Browse files
committed
Anti-bot plugin
1 parent 71c4be7 commit bd7c061

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

plugins/anti-bot.lua

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
2+
local function isBotAllowed (userId, chatId)
3+
local hash = 'anti-bot:allowed:'..chatId..':'..userId
4+
local banned = redis:get(hash)
5+
return banned
6+
end
7+
8+
local function allowBot (userId, chatId)
9+
local hash = 'anti-bot:allowed:'..chatId..':'..userId
10+
redis:set(hash, true)
11+
end
12+
13+
local function disallowBot (userId, chatId)
14+
local hash = 'anti-bot:allowed:'..chatId..':'..userId
15+
redis:del(hash)
16+
end
17+
18+
-- Is anti-bot enabled on chat
19+
local function isAntiBotEnabled (chatId)
20+
local hash = 'anti-bot:enabled:'..chatId
21+
local enabled = redis:get(hash)
22+
return enabled
23+
end
24+
25+
local function enableAntiBot (chatId)
26+
local hash = 'anti-bot:enabled:'..chatId
27+
redis:set(hash, true)
28+
end
29+
30+
local function disableAntiBot (chatId)
31+
local hash = 'anti-bot:enabled:'..chatId
32+
redis:del(hash)
33+
end
34+
35+
local function isABot (user)
36+
-- Flag its a bot 0001000000000000
37+
local binFlagIsBot = 4096
38+
local result = bit32.band(user.flags, binFlagIsBot)
39+
return result == binFlagIsBot
40+
end
41+
42+
local function kickUser(userId, chatId)
43+
local chat = 'chat#id'..chatId
44+
local user = 'user#id'..userId
45+
chat_del_user(chat, user, function (data, success, result)
46+
if success ~= 1 then
47+
print('I can\'t kick '..data.user..' but should be kicked')
48+
end
49+
end, {chat=chat, user=user})
50+
end
51+
52+
local function run (msg, matches)
53+
-- We wont return text if is a service msg
54+
if matches[1] ~= 'chat_add_user' and matches[1] ~= 'chat_add_user_link' then
55+
if msg.to.type ~= 'chat' then
56+
return 'Anti-flood works only on channels'
57+
end
58+
end
59+
60+
local chatId = msg.to.id
61+
if matches[1] == 'enable' then
62+
enableAntiBot(chatId)
63+
return 'Anti-bot enabled on this chat'
64+
end
65+
if matches[1] == 'disable' then
66+
disableAntiBot(chatId)
67+
return 'Anti-bot disabled on this chat'
68+
end
69+
if matches[1] == 'allow' then
70+
local userId = matches[2]
71+
allowBot(userId, chatId)
72+
return 'Bot '..userId..' allowed'
73+
end
74+
if matches[1] == 'disallow' then
75+
local userId = matches[2]
76+
disallowBot(userId, chatId)
77+
return 'Bot '..userId..' disallowed'
78+
end
79+
if matches[1] == 'chat_add_user' or matches[1] == 'chat_add_user_link' then
80+
local user = msg.action.user or msg.from
81+
if isABot(user) then
82+
print('It\'s a bot!')
83+
if isAntiBotEnabled(chatId) then
84+
print('Anti bot is enabled')
85+
local userId = user.id
86+
if not isBotAllowed(userId, chatId) then
87+
kickUser(userId, chatId)
88+
else
89+
print('This bot is allowed')
90+
end
91+
end
92+
end
93+
end
94+
end
95+
96+
return {
97+
description = 'When bot enters group kick it.',
98+
usage = {
99+
'!antibot enable: Enable Anti-bot on current chat',
100+
'!antibot disable: Disable Anti-bot on current chat',
101+
'!antibot allow <botId>: Allow <botId> on this chat',
102+
'!antibot disallow <botId>: Disallow <botId> on this chat'
103+
},
104+
patterns = {
105+
'^!antibot (allow) (%d+)$',
106+
'^!antibot (disallow) (%d+)$',
107+
'^!antibot (enable)$',
108+
'^!antibot (disable)$',
109+
'^!!tgservice (chat_add_user)$',
110+
'^!!tgservice (chat_add_user_link)$'
111+
},
112+
run = run
113+
}

0 commit comments

Comments
 (0)