from aiogram import types from sqlalchemy.orm.session import close_all_sessions from create_bot import bot, dp from sqdb import User, session, DickRequest, UserDick import random import traceback from random import randint import datetime from dicktxt import ForReadDict size_change = None dick_plus = None dick_minus = None def get_dick(user_id, chat_id): dick = session.query(UserDick).filter(UserDick.user_id == user_id, UserDick.chat_id == chat_id).first() return dick def top_dick_message(delimiter, top_list): message = f'🏆Топ 10 бубылд чата🏆\n\n' f'🚀 {delimiter.join(map(str, top_list[0]))}\n' f'💫 {delimiter.join(map(str, top_list[1]))}\n' f'🍆 {delimiter.join(map(str, top_list[2]))}\n' f'🍌 {delimiter.join(map(str, top_list[3]))}\n' f'🐍 {delimiter.join(map(str, top_list[4]))}\n' f'🐎 {delimiter.join(map(str, top_list[5]))}\n' f'🌭 {delimiter.join(map(str, top_list[6]))}\n' f'🌶 {delimiter.join(map(str, top_list[7]))}\n' f'👌 {delimiter.join(map(str, top_list[8]))}\n' f'💩 {delimiter.join(map(str, top_list[9]))}\n' return message @dp.message_handler(commands=["maintenance"]) async def stop_for_maintenance(message: types.Message): # остановить бота if message.from_user.id == 226765676: bot.send_message(message.from_user.id, "Блять. Как же больно. Мне холодно. Наверное это конец...") close_all_sessions() dp.stop_polling() @dp.message_handler(commands=["dick"]) async def up_dick(message: types.Message): # рандомайзер numb = randint(-6, 10) global size_change, dick_plus, dick_minus dick_plus = ('вырос', 'подрос', 'привстал') dick_minus = ('уменьшился', 'откусила злая гадюка', 'продали евреи, он стал дешевле') if numb > 0: size_change = random.choice(dick_plus) + '🚀' # if numb = 0 size_change = random.choice(const) TODO else: size_change = random.choice(dick_minus) + '✂' # работа с таблицей try: dick_request = DickRequest(size_change=numb, date_changed=datetime.datetime.now()) b = session.query(User).filter(User.user_id == message.from_user.id).first() if b: last_time = b.date_updated now_time = datetime.datetime.now() diff = now_time.day - last_time.day if abs(diff) <= 0: try: current_dick = b.dick_size await bot.send_message(message.chat.id, f'@{message.from_user.username},' f'🚫 вы уже крутили пипису, ' f'ее размер {current_dick} см! ') except Exception as e: print('ошибка в if diff <=0 ' + "\n".join(traceback.format_tb(e.__traceback__))) session.rollback() finally: session.close_all() print('пользователь уже крутил') else: try: usr = session.query(User) a = usr.filter(User.user_id == message.from_user.id).first() # запрос на поиск в таблице dick = get_dick(User.user_id, message.chat.id) updated_dick = dick.dick_size = (dick.dick_size + numb) dick_request.user = a a.date_updated = datetime.datetime.now() session.add(dick_request) session.flush() session.commit() # обновляет запись randomword = ForReadDict.RandomDick() await bot.send_message(message.chat.id, f'@{message.from_user.username}, ' f'ваш {randomword} {size_change}' f'на {abs(numb)} см!\n' f'Теперь он равен {updated_dick} см! ') except Exception as e: session.rollback() print('ошибка в обновлении пиписы, трейсбек-бек-бек: ' + "\n".join(traceback.format_tb(e.__traceback__))) finally: session.close_all() print('пиписа обновлена') else: session.close() try: numb = randint(1, 10) int_table = User(user_id=message.from_user.id, user_fullname=message.from_user.full_name, date_updated=datetime.datetime.now(), date_created=datetime.datetime.now()) new_dick = DickRequest(user_id=message.from_user.id, chat_id=message.chat.id, dick_size=numb) dick_request.user = int_table session.add(int_table) session.add(new_dick) session.add(dick_request) session.flush() session.commit() session.close() await bot.send_message(message.chat.id, f'@{message.from_user.username}, ' f'ваш писюн показал головку 🚀 \n' f'на {abs(numb)} см!\n' f'Теперь он равен {numb} см!') except Exception as e: print('ошибка в добавлении нового пользователя, трейсбек: ' + '\n'.join(traceback.format_tb(e.__traceback__))) session.rollback() finally: session.close_all() print('Успешно добавлен пользователь') except Exception as e: session.rollback() print('ошибка в основном коде, трейсбек:' + '\n'.join(traceback.format_tb(e.__traceback__))) finally: session.close_all() @dp.message_handler(commands=["topdick"]) async def send_top_chat(message: types.Message): try: top = [] chats = message.chat.id changechat_id = session.query(DickRequest).filter(DickRequest.user_id == message.from_user.id, DickRequest.chat_id == chats).first() dicks = session.query(DickRequest)\ .filter(DickRequest.chat_id == chats)\ .limit(10).all()\ .order_by(DickRequest.dick_size.desc()) for dick in dicks: user = session.query(User.user_fullname).filter(User.user_id == dick.user_id) top.append([user.full_name, dick.dick_size]) delimiter = ': ' # проверка на длину списка, если меньше limit, то: size_len = len(top) if size_len < 10: len_minus = 10 - size_len top.extend([('-'*len_minus),""]) await bot.send_message(message.chat.id, top_dick_message(delimiter, top)) session.close() # если все нормально и участников минимум 10 else: await bot.send_message(message.chat.id, top_dick_message(delimiter, top)) except Exception as e: session.rollback() print('ошибка в /topdick. Трейсбэк:\n' + '\n'.join(traceback.format_tb(e.__traceback__))) finally: session.close_all() @dp.message_handler(commands=["globaldick"]) async def send_global_top(message: types.Message): try: top = [] dicks = session.query(DickRequest) \ .limit(10).all() \ .order_by(DickRequest.dick_size.desc()) for dick in dicks: user = session.query(User.user_fullname).filter(User.user_id == dick.user_id) top.append([user.full_name, dick.dick_size]) delimiter = ': ' await bot.send_message(message.chat.id, top_dick_message(delimiter, top)) except Exception as e: session.rollback() print('ошибка в /globaldick, трейсбэк:\n' + '\n'.join(traceback.format_tb(e.__traceback__))) finally: session.close_all() # def register_handlers_pipisa(dp: Dispatcher): # dp.register_message_handler(up_dick) # dp.register_message_handler(send_topchat) # dp.register_message_handler(send_global_top)