рабочий вариант выдачи ключей через подтверждение

This commit is contained in:
Vlad
2024-09-17 01:45:12 +03:00
parent b3a8989927
commit 20b0e09b6e
3 changed files with 164 additions and 29 deletions
+39 -5
View File
@@ -11,19 +11,47 @@ async def init_db():
client_id TEXT NOT NULL,
email TEXT NOT NULL,
expiry_time INTEGER NOT NULL,
balance REAL NOT NULL DEFAULT 0.0, -- Добавлено поле для баланса
PRIMARY KEY (tg_id, client_id)
)
''')
await db.execute('''
CREATE TABLE IF NOT EXISTS keys (
client_id TEXT NOT NULL,
email TEXT NOT NULL,
created_at INTEGER NOT NULL,
key TEXT NOT NULL,
PRIMARY KEY (client_id)
)
''')
await db.commit()
async def add_connection(tg_id: int, client_id: str, email: str, expiry_time: int):
async def add_connection(tg_id: int, client_id: str, email: str, expiry_time: int, balance: float = 0.0):
async with aiosqlite.connect(DATABASE_PATH) as db:
await db.execute('''
INSERT INTO connections (tg_id, client_id, email, expiry_time)
VALUES (?, ?, ?, ?)
''', (tg_id, client_id, email, expiry_time))
INSERT INTO connections (tg_id, client_id, email, expiry_time, balance)
VALUES (?, ?, ?, ?, ?)
''', (tg_id, client_id, email, expiry_time, balance))
await db.commit()
async def store_key(client_id: str, email: str, key: str):
async with aiosqlite.connect(DATABASE_PATH) as db:
await db.execute('''
INSERT INTO keys (client_id, email, created_at, key)
VALUES (?, ?, ?, ?)
''', (client_id, email, int(datetime.utcnow().timestamp() * 1000), key))
await db.commit()
async def get_keys(tg_id: int):
async with aiosqlite.connect(DATABASE_PATH) as db:
async with db.execute('''
SELECT client_id, email, created_at, key
FROM keys
WHERE tg_id = ?
''', (tg_id,)) as cursor:
return await cursor.fetchall()
async def has_active_key(tg_id: int) -> bool:
async with aiosqlite.connect(DATABASE_PATH) as db:
async with db.execute("SELECT COUNT(*) FROM connections WHERE tg_id = ? AND expiry_time > ?",
@@ -46,4 +74,10 @@ async def get_key_expiry_time(tg_id: int) -> datetime:
if record:
expiry_time = record[0]
return datetime.utcfromtimestamp(expiry_time / 1000)
return None
return None
async def get_balance(tg_id: int) -> str:
async with aiosqlite.connect(DATABASE_PATH) as db:
async with db.execute("SELECT balance FROM connections WHERE tg_id = ?", (tg_id,)) as cursor:
record = await cursor.fetchone()
return record[0] if record else "Неизвестно"