clash-telegram-bot/database/worker.py
2022-03-18 21:25:50 +02:00

58 lines
1.6 KiB
Python

from aiogram import types
from .model import db, Users
db.create_tables([Users])
def register(user: types.User, data: dict):
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