/
bot_handlers.py
135 lines (99 loc) · 3.92 KB
/
bot_handlers.py
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
import random
from bot import bot
from text_constants import WELCOME_MESSAGE, FLOOD_MESSAGES, HELP_MESSAGE
message_storage = []
chat_storage = []
chats_id = []
members_id = []
MAX_MSG_BEFORE_FLOOD_WARN = 3
FLOOD_WARN_MSG_AMOUNT = 4
MIN_NORMAL_MSG_LENGTH = 15
MSG_BEFORE_NEW_GREETING = 4
class ChatMember():
global MAX_MSG_BEFORE_FLOOD_WARN
global FLOOD_WARN_MSG_AMOUNT
global MIN_NORMAL_MSG_LENGTH
def __init__(self, id):
self._id = id
self._message_history = []
self._reverse_history = []
self._short_message_counter = 0
self._is_not_short = 0
def append_message(self, msg):
self._message_history.append(msg)
def get_id(self):
return self._id
def clear_message_history(self):
self._message_history.clear()
def check_message_length(self):
self._reverse_history = list(reversed(self._message_history))
if len(self._reverse_history) >= FLOOD_WARN_MSG_AMOUNT:
for i in range(FLOOD_WARN_MSG_AMOUNT):
if len(self._reverse_history[i]) < MIN_NORMAL_MSG_LENGTH:
self._short_message_counter += 1
if self._short_message_counter == FLOOD_WARN_MSG_AMOUNT:
self._short_message_counter = 0
self.clear_message_history()
return True
else:
self._short_message_counter = 0
return False
def check_for_clear_history(self):
if len(self._reverse_history) >= MAX_MSG_BEFORE_FLOOD_WARN:
for i in range(MAX_MSG_BEFORE_FLOOD_WARN):
if len(self._reverse_history[i]) >= MIN_NORMAL_MSG_LENGTH:
self._is_not_short += 1
if self._is_not_short == MAX_MSG_BEFORE_FLOOD_WARN:
self.clear_message_history()
self._is_not_short = 0
class Chat():
def __init__(self, id, counter):
self._id = id
self._message_counter = counter
def get_id(self):
return self._id
def get_counter(self):
return self._message_counter
def set_counter(self, counter):
self._message_counter = counter
def add_one_to_message_counter_in_chats(message):
global chat_storage
global chats_id
if message.chat.id not in chats_id:
chats_id.append(message.chat.id)
chat_storage.append(Chat(message.chat.id, 0))
for chat in chat_storage:
if chat.get_id() == message.chat.id:
chat.set_counter(chat.get_counter() + 1)
break
@bot.message_handler(func=lambda message: True)
def count_all_messages(message):
global message_storage
global members_id
add_one_to_message_counter_in_chats(message)
if message.content_type == "text":
if message.from_user.id not in members_id:
members_id.append(message.from_user.id)
message_storage.append(ChatMember(message.from_user.id))
for chat_member in message_storage:
if chat_member.get_id() == message.from_user.id:
chat_member.append_message(message.text)
if chat_member.check_message_length() is True:
bot.reply_to(message, FLOOD_MESSAGES[random.randint(0, 1)],
parse_mode='HTML', disable_web_page_preview=True)
break
@bot.message_handler(content_types=["new_chat_members"])
def welcome_message(message):
global MSG_BEFORE_NEW_GREETING
add_one_to_message_counter_in_chats(message)
for chat in chat_storage:
if chat.get_id() == message.chat.id:
if chat.get_counter() > MSG_BEFORE_NEW_GREETING:
bot.send_message(message.chat.id, WELCOME_MESSAGE,
parse_mode='HTML', disable_web_page_preview=True)
chat.set_counter(0)
@bot.message_handler(commands=['help'])
def help_message(message):
bot.reply_to(message, HELP_MESSAGE)
if __name__ == '__main__':
bot.polling(none_stop=True)