Fix some bugs, add catch errors.

main
t 1 year ago
parent ce08187356
commit 0a36be02b3

@ -22,7 +22,34 @@ with open(DB_PATH, 'r') as f:
# Загружаем токен из файла
bot = telebot.TeleBot(last_bonk_time['token'])
print(last_bonk_time)
###########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):
@ -35,18 +62,29 @@ def bonk(message):
if 'хрясь' in message.text.lower():
if user_id not in last_bonk_time or current_time - last_bonk_time[user_id] > 1800:
try:
bot.restrict_chat_member(chat_id, bonk_target, until_date=current_time+300)
#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, 'Что-то пошло не так...')
#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)
bot.polling()
while True:
try:
bot.polling()
except KeyboardInterrupt:
exit()
except:
catch_error(message)

Loading…
Cancel
Save