clash-telegram-bot/database/worker.py

69 lines
2.0 KiB
Python
Raw Normal View History

2022-03-19 00:58:59 +03:00
from typing import Union
2022-03-18 22:25:50 +03:00
from aiogram import types
from .model import db, Users
db.create_tables([Users])
2022-03-19 00:58:59 +03:00
def register(user: Union[types.User, int], data: dict, task_update: bool=False):
if task_update and (Users.select().where(Users.user_id==user, Users.tag == data['tag']).exists()):
Users.update(
tag = data['tag'],
nickname = data['name'],
townhall = data['townHallLevel'],
attackwins = data['attackWins']
).where(Users.user_id == user, Users.tag == data['tag']).execute()
return
2022-03-18 22:25:50 +03:00
if not Users.select().where(Users.user_id==user.id, Users.tag == data['tag']).exists():
Users.create(
user_id = user.id,
first_name = user.first_name,
last_name = user.last_name,
username = user.username,
tag = data['tag'],
nickname = data['name'],
townhall = data['townHallLevel'],
attackwins = data['attackWins']
)
else:
Users.update(
user_id = user.id,
first_name = user.first_name,
last_name = user.last_name,
username = user.username,
tag = data['tag'],
nickname = data['name'],
townhall = data['townHallLevel'],
attackwins = data['attackWins']
).where(Users.user_id == user.id, Users.tag == data['tag']).execute()
def delete(user_id: int):
Users.delete().where(Users.user_id == user_id).execute()
def check_register(user_id: int):
return Users.select().where(Users.user_id==user_id).exists()
def get_users():
users = []
for p in Users.select():
users.append(
{
'user_id': p.user_id,
'first_name': p.first_name,
'last_name': p.last_name,
'username': p.username,
'tag': p.tag,
'nickname': p.nickname,
'townhall': p.townhall,
'attackwins': p.attackwins
}
)
return users