db fix
This commit is contained in:
1 parent
d53fd4790b
commit
887fdbf25c
3 files changed
+59
-30
No files matched your search
+57
-21
@@ -6,27 +6,9 @@ DB_PATH = "SQLite/FemBot.db"
|
||||
class Database:
|
||||
def __init__(self):
|
||||
self.conn = None
|
||||
self._lock = 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:
|
||||
async def _ensure_schema(self):
|
||||
await self.conn.executescript("""
|
||||
CREATE TABLE IF NOT EXISTS test (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -57,13 +39,67 @@ class Database:
|
||||
""")
|
||||
|
||||
await self.conn.commit()
|
||||
print("Table check/creation completed successfully.")
|
||||
|
||||
async def connect(self):
|
||||
if self._lock is None:
|
||||
import asyncio
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
async with self._lock:
|
||||
if self.conn is not None:
|
||||
try:
|
||||
await self.conn.execute("SELECT 1")
|
||||
return self.conn
|
||||
except Exception:
|
||||
try:
|
||||
await self.conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.conn = 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._ensure_schema()
|
||||
print("Table check/creation completed successfully.")
|
||||
except aiosqlite.Error as e:
|
||||
print(f"Failed to check/creation table: {e}")
|
||||
|
||||
return self.conn
|
||||
|
||||
async def get_conn(self):
|
||||
if self.conn is not None:
|
||||
try:
|
||||
await self.conn.execute("SELECT 1")
|
||||
return self.conn
|
||||
except Exception:
|
||||
try:
|
||||
await self.conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.conn = None
|
||||
await self.connect()
|
||||
return self.conn
|
||||
|
||||
async def close(self):
|
||||
if self.conn:
|
||||
try:
|
||||
await self.conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.conn = None
|
||||
@@ -46,14 +46,7 @@ class Counting(commands.Cog):
|
||||
if message.channel.id != COUNTING_CHANNEL_ID:
|
||||
return
|
||||
|
||||
db = self.bot.database.conn
|
||||
if db is None:
|
||||
try:
|
||||
await message.delete()
|
||||
except discord.NotFound:
|
||||
pass
|
||||
await message.channel.send(f"DB is Down :c <@1073620716152434830>")
|
||||
return
|
||||
db = await self.bot.database.get_conn()
|
||||
|
||||
try:
|
||||
async with db.execute(
|
||||
|
||||
@@ -165,7 +165,7 @@ async def CreateTicketChannel(interaction: discord.Interaction, chosen_language,
|
||||
print("Error: Category not found")
|
||||
return None
|
||||
|
||||
db = bot.database.conn
|
||||
db = await bot.database.get_conn()
|
||||
|
||||
cursor = await db.execute(
|
||||
"INSERT INTO tickets (user_id, language, reason) VALUES (?, ?, ?)",
|
||||
|
||||
Reference in new issue
Block a user