first commit
This commit is contained in:
50
bot/db/_db.py
Normal file
50
bot/db/_db.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import aiosqlite
|
||||
import asyncio
|
||||
import random
|
||||
from typing import Optional
|
||||
|
||||
class Database:
|
||||
_instance: Optional['Database'] = None
|
||||
_lock: asyncio.Lock
|
||||
db_path: str
|
||||
db: Optional[aiosqlite.Connection]
|
||||
|
||||
def __new__(cls, db_path='db/anti.db'):
|
||||
if cls._instance is None:
|
||||
cls._instance = super().__new__(cls)
|
||||
cls._instance.db_path = db_path
|
||||
cls._instance.db = None
|
||||
cls._instance._lock = asyncio.Lock()
|
||||
return cls._instance
|
||||
|
||||
async def connect(self, timeout=30):
|
||||
async with self._lock:
|
||||
if self.db is None:
|
||||
self.db = await aiosqlite.connect(self.db_path, timeout=timeout)
|
||||
await self.db.execute('PRAGMA journal_mode=WAL;')
|
||||
await self.db.commit()
|
||||
return self.db
|
||||
|
||||
async def ensure_connection(self):
|
||||
if self.db is None or not self.db.is_open:
|
||||
await self.connect()
|
||||
return self.db
|
||||
|
||||
async def execute_with_retries(self, func, retries=5, delay=1):
|
||||
for attempt in range(retries):
|
||||
try:
|
||||
return await func()
|
||||
except aiosqlite.OperationalError as e:
|
||||
if 'database is locked' in str(e).lower():
|
||||
sleep_time = delay * (2 ** attempt) + random.uniform(0, 1)
|
||||
print(f"Database is locked. Retryng in {sleep_time:.2f} seconds.....")
|
||||
await asyncio.sleep(sleep_time)
|
||||
else:
|
||||
raise
|
||||
raise Exception("Max retrys.")
|
||||
|
||||
async def close(self):
|
||||
async with self._lock:
|
||||
if self.db is not None:
|
||||
await self.db.close()
|
||||
self.db = None
|
||||
BIN
bot/db/admin_config.db
Normal file
BIN
bot/db/admin_config.db
Normal file
Binary file not shown.
BIN
bot/db/afk.db
Normal file
BIN
bot/db/afk.db
Normal file
Binary file not shown.
BIN
bot/db/ai_data.db
Normal file
BIN
bot/db/ai_data.db
Normal file
Binary file not shown.
BIN
bot/db/anti.db
Normal file
BIN
bot/db/anti.db
Normal file
Binary file not shown.
BIN
bot/db/automod.db
Normal file
BIN
bot/db/automod.db
Normal file
Binary file not shown.
BIN
bot/db/autoreact.db
Normal file
BIN
bot/db/autoreact.db
Normal file
Binary file not shown.
BIN
bot/db/autoresponder.db
Normal file
BIN
bot/db/autoresponder.db
Normal file
Binary file not shown.
BIN
bot/db/autorole.db
Normal file
BIN
bot/db/autorole.db
Normal file
Binary file not shown.
BIN
bot/db/badges.db
Normal file
BIN
bot/db/badges.db
Normal file
Binary file not shown.
BIN
bot/db/block.db
Normal file
BIN
bot/db/block.db
Normal file
Binary file not shown.
BIN
bot/db/blword.db
Normal file
BIN
bot/db/blword.db
Normal file
Binary file not shown.
BIN
bot/db/boost.db
Normal file
BIN
bot/db/boost.db
Normal file
Binary file not shown.
8
bot/db/counting.json
Normal file
8
bot/db/counting.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"1419729237480575070": {
|
||||
"enabled": false,
|
||||
"channel": 1421538951323193467,
|
||||
"count": 4,
|
||||
"reset_on_fail": false
|
||||
}
|
||||
}
|
||||
BIN
bot/db/customrole.db
Normal file
BIN
bot/db/customrole.db
Normal file
Binary file not shown.
BIN
bot/db/emergency.db
Normal file
BIN
bot/db/emergency.db
Normal file
Binary file not shown.
BIN
bot/db/fastgreet.db
Normal file
BIN
bot/db/fastgreet.db
Normal file
Binary file not shown.
BIN
bot/db/giveaways.db
Normal file
BIN
bot/db/giveaways.db
Normal file
Binary file not shown.
BIN
bot/db/ignore.db
Normal file
BIN
bot/db/ignore.db
Normal file
Binary file not shown.
BIN
bot/db/invc.db
Normal file
BIN
bot/db/invc.db
Normal file
Binary file not shown.
BIN
bot/db/invite.db
Normal file
BIN
bot/db/invite.db
Normal file
Binary file not shown.
BIN
bot/db/jail.db
Normal file
BIN
bot/db/jail.db
Normal file
Binary file not shown.
BIN
bot/db/leveling.db
Normal file
BIN
bot/db/leveling.db
Normal file
Binary file not shown.
BIN
bot/db/media.db
Normal file
BIN
bot/db/media.db
Normal file
Binary file not shown.
BIN
bot/db/messages.db
Normal file
BIN
bot/db/messages.db
Normal file
Binary file not shown.
BIN
bot/db/minecraft.db
Normal file
BIN
bot/db/minecraft.db
Normal file
Binary file not shown.
BIN
bot/db/notify.db
Normal file
BIN
bot/db/notify.db
Normal file
Binary file not shown.
BIN
bot/db/np.db
Normal file
BIN
bot/db/np.db
Normal file
Binary file not shown.
BIN
bot/db/prefix.db
Normal file
BIN
bot/db/prefix.db
Normal file
Binary file not shown.
BIN
bot/db/stats.db
Normal file
BIN
bot/db/stats.db
Normal file
Binary file not shown.
BIN
bot/db/stickymessages.db
Normal file
BIN
bot/db/stickymessages.db
Normal file
Binary file not shown.
BIN
bot/db/ticket.db
Normal file
BIN
bot/db/ticket.db
Normal file
Binary file not shown.
BIN
bot/db/ticket.db-journal
Normal file
BIN
bot/db/ticket.db-journal
Normal file
Binary file not shown.
BIN
bot/db/topcheck.db
Normal file
BIN
bot/db/topcheck.db
Normal file
Binary file not shown.
BIN
bot/db/vanity.db
Normal file
BIN
bot/db/vanity.db
Normal file
Binary file not shown.
BIN
bot/db/verification.db
Normal file
BIN
bot/db/verification.db
Normal file
Binary file not shown.
BIN
bot/db/warn.db
Normal file
BIN
bot/db/warn.db
Normal file
Binary file not shown.
BIN
bot/db/welcome.db
Normal file
BIN
bot/db/welcome.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user