-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
34 lines (30 loc) · 1.16 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import aiosqlite
from cryptography.fernet import Fernet
from config import ENCRYPTION_KEY
cipher_suite = Fernet(ENCRYPTION_KEY.encode())
async def encrypt_password(password: str) -> str:
return cipher_suite.encrypt(password.encode()).decode()
async def decrypt_password(encrypted_password: str) -> str:
return cipher_suite.decrypt(encrypted_password.encode()).decode()
async def init_db() -> None:
try:
async with aiosqlite.connect('bot.db') as db:
await db.execute('''
CREATE TABLE IF NOT EXISTS servers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
ip TEXT,
port INTEGER,
login TEXT,
password TEXT
)
''')
await db.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
telegram_id INTEGER UNIQUE
)
''')
await db.commit()
except Exception as e:
print(f"Failed to initialize the database: {e}")