added statistics

This commit is contained in:
hopelessjames 2023-06-19 17:09:04 +10:00
parent 2357ebe6bd
commit 61075491ec
3 changed files with 62 additions and 35 deletions

View File

@ -1,42 +1,49 @@
from aiogram import types, Dispatcher
from sqlalchemy.orm.session import close_all_sessions
from create_bot import bot, dp
from sqdb import user, session
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
@dp.message_handler(commands=["dick"])
async def up_dick(message: types.Message):
# рандомайзер
numb = randint(-6,10)
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) + ''
# 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.datetimes
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},🚫 вы уже крутили пипису, ее размер <b>{current_dick}</b> см! ')
except:
print('ошибка в if diff <=0')
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()
@ -44,41 +51,51 @@ async def up_dick(message: types.Message):
else:
try:
a = session.query(user).filter(user.user_id == message.from_user.id).first() # запрос на поиск в таблице
updetedDick = a.dick_size = (a.dick_size + numb)
a.datetimes = datetime.datetime.now()
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() #обновляет запись
session.commit() # обновляет запись
randomword = ForReadDict.RandomDick()
await bot.send_message(message.chat.id, f'@{message.from_user.username}, ваш <em>{randomword}</em> {size_change}\
на <b>{abs(numb)}</b> см!\n\
Теперь он равен <b>{updetedDick}</b> см! ')
except:
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('ошибка в обновлении пиписы')
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, datetimes = datetime.datetime.now(),\
chat_id = message.chat.id)
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}, ваш писюн {size_change}\n\
на <b>{abs(numb)}</b> см!\n\
Теперь он равен <b>{numb}</b> см!')
except:
print('ошибка в добавлении нового пользователя')
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()
@ -95,6 +112,7 @@ async def up_dick(message: types.Message):
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
@ -103,7 +121,6 @@ async def send_topchat(message: types.Message):
else:
session.close()
top = session.query(user.user_fullname, user.dick_size).order_by(user.dick_size.desc()).filter(user.chat_id == chats).limit(10).all()
delimiter = ': '
# проверка на длину списка, если меньше limit, то:

14
sqdb.py
View File

@ -1,7 +1,7 @@
from sqlalchemy import create_engine, MetaData, Table, Integer, String, BIGINT, \
Column, DateTime, ForeignKey, Numeric
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import select, update
engine = create_engine('postgresql+psycopg2://postgres:postgres@postgres.vdk2ch.ru:5432/jack_bot', echo=True)
@ -19,12 +19,22 @@ class user(Base):
id = Column(Integer, primary_key=True)
user_id = Column(Integer)
date_created = (Column(DateTime))
user_fullname = Column(String)
dick_size = (Column(Integer))
datetimes = (Column(DateTime))
date_updated = (Column(DateTime))
chat_id = Column(BIGINT)
dick_requests = relationship("DickRequest", back_populates="user")
class DickRequest(Base):
__tablename__ = 'dick_requests'
id = Column(Integer, primary_key=True)
size_change = Column(Integer)
user_id = Column(Integer, ForeignKey("users.user_id"))
user = relationship("User", back_populates="dick_requests")
date_changed = (Column(DateTime))