fix(aurora): fixed some issues with aiosqlite
This commit is contained in:
parent
56a2f96a2d
commit
3383e84221
4 changed files with 34 additions and 38 deletions
|
@ -128,7 +128,7 @@ class Aurora(commands.Cog):
|
|||
async def addrole_on_member_join(self, member: discord.Member):
|
||||
"""This method automatically adds roles to users when they join the server."""
|
||||
if not await self.bot.cog_disabled_in_guild(self, member.guild):
|
||||
async with connect() as database:
|
||||
database = await connect()
|
||||
query = f"""SELECT moderation_id, role_id, reason FROM moderation_{member.guild.id} WHERE target_id = ? AND moderation_type = 'ADDROLE' AND expired = 0 AND resolved = 0;"""
|
||||
async with database.execute(query, (member.id,)) as cursor:
|
||||
async for row in cursor:
|
||||
|
|
|
@ -27,10 +27,11 @@ class ImportAuroraView(ui.View):
|
|||
"Deleting original table...", ephemeral=True
|
||||
)
|
||||
|
||||
async with await connect() as database:
|
||||
database = await connect()
|
||||
query = f"DROP TABLE IF EXISTS moderation_{self.ctx.guild.id};"
|
||||
database.execute(query)
|
||||
database.commit()
|
||||
database.close()
|
||||
|
||||
await interaction.edit_original_response(content="Creating new table...")
|
||||
|
||||
|
|
|
@ -122,7 +122,7 @@ class Moderation(AuroraGuildModel):
|
|||
from ..utilities.json import dumps
|
||||
query = f"UPDATE moderation_{self.guild_id} SET timestamp = ?, moderation_type = ?, target_type = ?, moderator_id = ?, role_id = ?, duration = ?, end_timestamp = ?, reason = ?, resolved = ?, resolved_by = ?, resolve_reason = ?, expired = ?, changes = ?, metadata = ? WHERE moderation_id = ?;"
|
||||
|
||||
async with connect() as database:
|
||||
database = await connect()
|
||||
await database.execute(query, (
|
||||
self.timestamp.timestamp(),
|
||||
self.moderation_type,
|
||||
|
@ -140,6 +140,8 @@ class Moderation(AuroraGuildModel):
|
|||
dumps(self.metadata),
|
||||
self.moderation_id,
|
||||
))
|
||||
await database.commit()
|
||||
await database.close()
|
||||
|
||||
logger.debug("Row updated in moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s",
|
||||
self.moderation_id,
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
# pylint: disable=cyclic-import
|
||||
import json
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, AsyncGenerator
|
||||
|
||||
import aiosqlite
|
||||
from discord import Guild
|
||||
|
@ -10,14 +8,13 @@ from redbot.core import data_manager
|
|||
from .logger import logger
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def connect() -> AsyncGenerator[aiosqlite.Connection, Any, None]:
|
||||
async def connect() -> aiosqlite.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"
|
||||
)
|
||||
yield connection
|
||||
return connection
|
||||
|
||||
except aiosqlite.OperationalError as e:
|
||||
logger.error("Unable to access the SQLite database!\nError:\n%s", e.msg)
|
||||
|
@ -25,10 +22,6 @@ async def connect() -> AsyncGenerator[aiosqlite.Connection, Any, None]:
|
|||
f"Unable to access the SQLite Database!\n{e.msg}"
|
||||
) from e
|
||||
|
||||
finally:
|
||||
if connection:
|
||||
await connection.close()
|
||||
|
||||
|
||||
async def create_guild_table(guild: Guild):
|
||||
database = await connect()
|
||||
|
|
Loading…
Reference in a new issue