python_bot/handlers/pipisa.py
hopelessjames da06b5ad23
All checks were successful
continuous-integration/drone/push Build is passing
minor fixes and PEP8'ification
2023-06-19 19:24:40 +10:00

186 lines
8.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from aiogram import types, Dispatcher
from sqlalchemy.orm.session import close_all_sessions
from create_bot import bot, dp
from sqdb import user, session, DickRequest
import random
import traceback
from random import randint
import datetime
from dicktxt import ForReadDict
size_change = None
dick_plus = None
dick_minus = None
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=["maintainance"])
async def stop_for_maintenance(message: types.Message): # остановить бота
if message.from_user.id == 226765676:
close_all_sessions() # TODO Вспомнить, как это, блять, делается.
@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 = ('<b>вырос</b>', '<b>подрос</b>', '<b>привстал</b>')
dick_minus = ('<b>уменьшился</b>', '<b>откусила злая гадюка</b>', '<b>продали евреи, он стал дешевле</b>')
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'ее размер <b>{current_dick}</b> см! ')
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() # запрос на поиск в таблице
updated_dick = a.dick_size = (a.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'ваш <em>{randomword}</em> {size_change}'
f'на <b>{abs(numb)}</b> см!\n'
f'Теперь он равен <b>{updated_dick}</b> см! ')
except Exception as e:
session.rollback()
print('ошибка в обновлении пиписы, трейсбек-бек-бек: ' +
"\n".join(traceback.format_tb(e.__traceback__)))
finally:
session.close_all()
print('пиписа обновлена')
else:
session.close()
try:
int_table = user(user_id=message.from_user.id,
user_fullname=message.from_user.full_name,
dick_size=numb,
date_updated=datetime.datetime.now(),
date_created=datetime.datetime.now(),
chat_id=message.chat.id)
dick_request.user = int_table
session.add(int_table)
session.add(dick_request)
session.flush()
session.commit()
session.close()
await bot.send_message(message.chat.id, f'@{message.from_user.username}, '
f'ваш писюн {size_change}\n'
f'на <b>{abs(numb)}</b> см!\n'
f'Теперь он равен <b>{numb}</b> см!')
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_topchat(message: types.Message):
try:
changechat_id = session.query(user).filter(user.user_id == message.from_user.id).first()
delimiter = ': '
chats = message.chat.id
if changechat_id.chat_id != chats:
changechat_id.chat_id = chats
session.commit()
session.close()
else:
session.close()
users = session.query(user.user_fullname, user.dick_size).order_by(user.dick_size.desc())
top = users.filter(user.chat_id == chats).limit(10).all()
# проверка на длину списка, если меньше 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_chat = session.query(user.user_fullname, user.dick_size).order_by(user.dick_size.desc()).limit(10).all()
delimiter = ': '
await bot.send_message(message.chat.id, top_dick_message(delimiter, top_chat))
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)