clash-telegram-bot/database/worker.py
2022-03-18 23:58:59 +02:00

69 lines
2.0 KiB
Python

from typing import Union
from aiogram import types
from .model import db, Users
db.create_tables([Users])
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
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