feat(aurora): bunch of changes
This commit is contained in:
parent
f6a42b97d9
commit
0bcbcd6c0c
6 changed files with 122 additions and 149 deletions
|
@ -5,8 +5,10 @@ from time import time
|
|||
from typing import Dict, Iterable, List, Optional, Tuple, Union
|
||||
|
||||
import discord
|
||||
from aiosqlite import Cursor
|
||||
from aiosqlite import Connection, Cursor, OperationalError, Row
|
||||
from aiosqlite import connect as aiosqlite_connect
|
||||
from discord import NotFound
|
||||
from redbot.core import data_manager
|
||||
from redbot.core.bot import Red
|
||||
|
||||
from ..utilities.logger import logger
|
||||
|
@ -211,34 +213,48 @@ class Moderation(AuroraGuildModel):
|
|||
}
|
||||
return cls.from_dict(bot=bot, data=case)
|
||||
|
||||
@staticmethod
|
||||
async def connect() -> Connection:
|
||||
"""Connects to the SQLite database, and returns a connection object."""
|
||||
try:
|
||||
connection = await aiosqlite_connect(
|
||||
database=data_manager.cog_data_path(raw_name="Aurora") / "aurora.db"
|
||||
)
|
||||
return connection
|
||||
|
||||
except OperationalError as e:
|
||||
logger.error("Unable to access the SQLite database!\nError:\n%s", e.msg)
|
||||
raise ConnectionRefusedError(
|
||||
f"Unable to access the SQLite Database!\n{e.msg}"
|
||||
) from e
|
||||
|
||||
@classmethod
|
||||
async def execute(cls, bot: Red, guild_id: int, query: str, parameters: tuple | None = None, cursor: Cursor | None = None, return_obj: bool = True) -> Tuple["Moderation"]:
|
||||
from ..utilities.database import connect
|
||||
logger.trace("Executing query: %s", query)
|
||||
logger.trace("With parameters: %s", parameters)
|
||||
async def execute(cls, query: str, parameters: tuple | None = None, bot: Red | None = None, guild_id: int | None = None, cursor: Cursor | None = None, return_obj: bool = True) -> Union[Tuple["Moderation"], Iterable[Row]]:
|
||||
logger.trace("Executing query: \"%s\" with parameters \"%s\"", query, parameters)
|
||||
if not parameters:
|
||||
parameters = ()
|
||||
if not cursor:
|
||||
no_cursor = True
|
||||
database = await connect()
|
||||
database = await cls.connect()
|
||||
cursor = await database.cursor()
|
||||
else:
|
||||
no_cursor = False
|
||||
|
||||
await cursor.execute(query, parameters)
|
||||
results = await cursor.fetchall()
|
||||
await database.commit()
|
||||
if no_cursor:
|
||||
await cursor.close()
|
||||
await database.close()
|
||||
|
||||
if results and return_obj:
|
||||
if results and return_obj and bot and guild_id:
|
||||
cases = []
|
||||
for result in results:
|
||||
case = cls.from_result(bot=bot, result=result, guild_id=guild_id)
|
||||
if case.moderation_id != 0:
|
||||
cases.append(case)
|
||||
return tuple(cases)
|
||||
return ()
|
||||
return results
|
||||
|
||||
@classmethod
|
||||
async def get_latest(cls, bot: Red, guild_id: int, limit: int | None = None, offset: int = 0, types: Iterable | None = None, cursor: Cursor | None = None) -> Tuple["Moderation"]:
|
||||
|
@ -306,7 +322,6 @@ class Moderation(AuroraGuildModel):
|
|||
metadata: dict | None = None,
|
||||
return_obj: bool = True,
|
||||
) -> Union["Moderation", int]:
|
||||
from ..utilities.database import connect
|
||||
from ..utilities.json import dumps
|
||||
if not timestamp:
|
||||
timestamp = datetime.fromtimestamp(time())
|
||||
|
@ -341,7 +356,7 @@ class Moderation(AuroraGuildModel):
|
|||
role_id = None
|
||||
|
||||
if not database:
|
||||
database = await connect()
|
||||
database = await cls.connect()
|
||||
close_db = True
|
||||
else:
|
||||
close_db = False
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue