-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
36 lines (28 loc) · 1.61 KB
/
bot.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
import telebot
from config import *
import time
from collections import defaultdict, deque
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', filename='log.txt', encoding='utf-8')
logger = logging.getLogger(__name__)
bot = telebot.TeleBot(TOKEN)
user_messages = defaultdict(lambda: deque(maxlen=MSGS_LIMIT))
def mute_user(chat_id, user_id, duration):
bot.restrict_chat_member(chat_id, user_id, until_date=int(time.time()) + duration, can_send_messages=False)
logger.info(f"User {user_id} has been muted in chat {chat_id} for {duration} seconds.")
@bot.message_handler(content_types=['text', 'sticker', 'animation', 'dice'])
def handle_message(message):
user_id = message.from_user.id
chat_id = message.chat.id
current_time = time.time()
content = message.text if message.content_type == 'text' else message.content_type
logger.info(f"Received message from user {user_id} in chat {chat_id}: {content}")
user_messages[(chat_id, user_id)].append(current_time)
recent_messages = user_messages[(chat_id, user_id)]
if len(recent_messages) == MSGS_LIMIT and (current_time - recent_messages[0]) <= SPAM_TIME:
mute_user(chat_id, user_id, 60)
user_link = f'<a href="tg://user?id={user_id}">{message.from_user.first_name}</a>'
bot.send_message(chat_id, f"Пользователь {user_link} замучен на {MUTE_MSG} за спам.", parse_mode='HTML')
user_messages[(chat_id, user_id)].clear()
logger.info(f"User {user_id} has been muted for spamming in chat {chat_id}.")
bot.infinity_polling()