69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
import os
|
|
import aiosqlite
|
|
|
|
DB_PATH = "SQLite/FemBot.db"
|
|
|
|
class Database:
|
|
def __init__(self):
|
|
self.conn = None
|
|
|
|
async def connect(self):
|
|
if self.conn is None:
|
|
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
|
|
|
db_existed = os.path.exists(DB_PATH)
|
|
if db_existed:
|
|
print(f"Using existing database file at {DB_PATH}")
|
|
else:
|
|
print(f"Database file not found at {DB_PATH}, creating a new one...")
|
|
|
|
try:
|
|
self.conn = await aiosqlite.connect(DB_PATH)
|
|
except aiosqlite.Error as e:
|
|
print(f"Failed to open database at {DB_PATH}: {e}")
|
|
raise
|
|
|
|
if not db_existed and not os.path.exists(DB_PATH):
|
|
print(f"Warning: database file was expected at {DB_PATH} but still missing after connect.")
|
|
|
|
try:
|
|
await self.conn.executescript("""
|
|
CREATE TABLE IF NOT EXISTS test (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
message TEXT NOT NULL
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS tickets (
|
|
ticket_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
ticket_channel_id TEXT DEFAULT NULL,
|
|
|
|
user_id TEXT NOT NULL,
|
|
language TEXT NOT NULL,
|
|
reason TEXT NOT NULL,
|
|
|
|
opened_at TEXT DEFAULT CURRENT_TIMESTAMP,
|
|
closed_at TEXT DEFAULT NULL,
|
|
is_open INTEGER DEFAULT 1 CHECK (is_open IN (0, 1)),
|
|
|
|
claimed_user_id TEXT DEFAULT NULL,
|
|
claimed_at TEXT DEFAULT NULL,
|
|
is_claimed INTEGER DEFAULT 0 CHECK (is_claimed IN (0, 1))
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS count (
|
|
count INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id TEXT NOT NULL
|
|
);
|
|
""")
|
|
|
|
await self.conn.commit()
|
|
print("Table check/creation completed successfully.")
|
|
|
|
except aiosqlite.Error as e:
|
|
print(f"Failed to check/creation table: {e}")
|
|
|
|
return self.conn
|
|
|
|
async def close(self):
|
|
if self.conn:
|
|
await self.conn.close() |