исправление ошибки в моих ключах
This commit is contained in:
+24
-38
@@ -1,48 +1,46 @@
|
||||
import aiosqlite
|
||||
from datetime import datetime
|
||||
from config import DATABASE_PATH
|
||||
import aiosqlite
|
||||
|
||||
async def init_db():
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS connections (
|
||||
tg_id INTEGER NOT NULL,
|
||||
client_id TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
expiry_time INTEGER NOT NULL,
|
||||
tg_id INTEGER PRIMARY KEY NOT NULL,
|
||||
balance REAL NOT NULL DEFAULT 0.0,
|
||||
PRIMARY KEY (tg_id, client_id)
|
||||
trial INTEGER NOT NULL DEFAULT 0
|
||||
)
|
||||
''')
|
||||
await db.execute('''
|
||||
CREATE TABLE IF NOT EXISTS keys (
|
||||
tg_id INTEGER NOT NULL, -- Добавлено поле tg_id
|
||||
tg_id INTEGER NOT NULL,
|
||||
client_id TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
expiry_time INTEGER NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
PRIMARY KEY (tg_id, client_id) -- Изменено на (tg_id, client_id)
|
||||
PRIMARY KEY (tg_id, client_id)
|
||||
)
|
||||
''')
|
||||
await db.commit()
|
||||
|
||||
async def add_connection(tg_id: int, client_id: str, email: str, expiry_time: int, balance: float = 0.0):
|
||||
async def add_connection(tg_id: int, balance: float = 0.0, trial: int = 0):
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
await db.execute('''
|
||||
INSERT INTO connections (tg_id, client_id, email, expiry_time, balance)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
''', (tg_id, client_id, email, expiry_time, balance))
|
||||
INSERT INTO connections (tg_id, balance, trial)
|
||||
VALUES (?, ?, ?)
|
||||
''', (tg_id, balance, trial))
|
||||
await db.commit()
|
||||
|
||||
async def store_key(tg_id: int, client_id: str, email: str, key: str):
|
||||
async def store_key(tg_id: int, client_id: str, email: str, expiry_time: int, key: str):
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
await db.execute('''
|
||||
INSERT INTO keys (tg_id, client_id, email, created_at, key)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
''', (tg_id, client_id, email, int(datetime.utcnow().timestamp() * 1000), key))
|
||||
INSERT INTO keys (tg_id, client_id, email, created_at, expiry_time, key)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
''', (tg_id, client_id, email, int(datetime.utcnow().timestamp() * 1000), expiry_time, key))
|
||||
await db.commit()
|
||||
|
||||
|
||||
async def get_keys(tg_id: int):
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute('''
|
||||
@@ -54,33 +52,15 @@ async def get_keys(tg_id: int):
|
||||
|
||||
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 > ?",
|
||||
(tg_id, int(datetime.utcnow().timestamp() * 1000))) as cursor:
|
||||
async with db.execute("SELECT COUNT(*) FROM keys WHERE tg_id = ?", (tg_id,)) as cursor:
|
||||
count = await cursor.fetchone()
|
||||
return count[0] > 0
|
||||
|
||||
async def get_active_key_email(tg_id: int) -> str:
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute("SELECT email FROM connections WHERE tg_id = ? AND expiry_time > ?",
|
||||
(tg_id, int(datetime.utcnow().timestamp() * 1000))) as cursor:
|
||||
record = await cursor.fetchone()
|
||||
return record[0] if record else None
|
||||
|
||||
async def get_key_expiry_time(tg_id: int) -> datetime:
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute("SELECT expiry_time FROM connections WHERE tg_id = ? AND expiry_time > ?",
|
||||
(tg_id, int(datetime.utcnow().timestamp() * 1000))) as cursor:
|
||||
record = await cursor.fetchone()
|
||||
if record:
|
||||
expiry_time = record[0]
|
||||
return datetime.utcfromtimestamp(expiry_time / 1000)
|
||||
return None
|
||||
|
||||
async def get_balance(tg_id: int) -> str:
|
||||
async def get_balance(tg_id: int) -> float:
|
||||
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 "Неизвестно"
|
||||
return record[0] if record else 0.0
|
||||
|
||||
async def update_balance(tg_id: int, amount: float):
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
@@ -91,8 +71,14 @@ async def update_balance(tg_id: int, amount: float):
|
||||
''', (amount, tg_id))
|
||||
await db.commit()
|
||||
|
||||
async def get_trial(tg_id: int) -> int:
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute("SELECT trial FROM connections WHERE tg_id = ?", (tg_id,)) as cursor:
|
||||
record = await cursor.fetchone()
|
||||
return record[0] if record else 0
|
||||
|
||||
async def get_key_count(tg_id: int) -> int:
|
||||
async with aiosqlite.connect(DATABASE_PATH) as db:
|
||||
async with db.execute('SELECT COUNT(*) FROM keys k JOIN connections c ON k.client_id = c.client_id WHERE c.tg_id = ?', (tg_id,)) as cursor:
|
||||
async with db.execute('SELECT COUNT(*) FROM keys WHERE tg_id = ?', (tg_id,)) as cursor:
|
||||
count = await cursor.fetchone()
|
||||
return count[0] if count else 0
|
||||
|
||||
Reference in New Issue
Block a user