You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.8 KiB

import telebot
import time
import os
import json
last_bonk_time = {} # словарь для хранения времени последней команды "хрясь"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_NAME = 'data.json'
DB_PATH = os.path.join(BASE_DIR, BASE_NAME)
# если базы данных нет, то создаем новую
if not os.path.exists(DB_PATH):
with open(DB_PATH, 'w') as f:
json.dump({}, f)
# загружаем базу данных
with open(DB_PATH, 'r') as f:
last_bonk_time = json.load(f)
# Загружаем токен из файла
bot = telebot.TeleBot(last_bonk_time['token'])
###########CATCH ERRORS##########
import logging
import traceback
from io import StringIO # For catch log to variable
# Basic init
global log_stream
log_stream = StringIO()
logging.basicConfig(stream=log_stream)
def catch_error(message, err_type = None):
try:
if not err_type:
global log_stream
logging.error(traceback.format_exc()) # Log error
err = log_stream.getvalue() # Error to variable
bot.reply_to(message, "Critical error :\n\n" + telebot.formatting.hcode(err), parse_mode='HTML')
except:
pass
#################################
@bot.message_handler(func=lambda message: True)
def bonk(message):
chat_id = message.chat.id
reply_message = message.reply_to_message
if reply_message:
bonk_target = reply_message.from_user.id
user_id = str(message.from_user.id)
current_time = time.time()
if 'хрясь' in message.text.lower():
if user_id not in last_bonk_time or current_time - last_bonk_time[user_id] > 1800:
try:
#if True:
bot.restrict_chat_member(chat_id, bonk_target, until_date = message.date+ 1800)
text = f'{message.from_user.full_name} угостил дубиной {reply_message.from_user.full_name} 👊'
bot.send_message(chat_id, text)
last_bonk_time[user_id] = current_time
print(current_time)
print(message.date)
with open(DB_PATH, 'w') as f:
json.dump(last_bonk_time, f)
except:
#bot.send_message(chat_id, 'Что-то пошло не так...')
catch_error(message)
else:
wait_time = int(1800 - current_time + last_bonk_time[user_id])
text = f"{message.from_user.full_name}, подождите, вы уже использовали 'хрясь' недавно. Попробуйте снова через {wait_time // 60} мин. {wait_time % 60} сек."
bot.send_message(chat_id, text)
while True:
try:
bot.polling()
except KeyboardInterrupt:
exit()
except:
catch_error(message)