WIP: Refactor Aurora (3.0.0) #29

Draft
cswimr wants to merge 347 commits from aurora-pydantic into main
4 changed files with 34 additions and 38 deletions
Showing only changes of commit 3383e84221 - Show all commits

View file

@ -128,13 +128,13 @@ class Aurora(commands.Cog):
async def addrole_on_member_join(self, member: discord.Member): async def addrole_on_member_join(self, member: discord.Member):
"""This method automatically adds roles to users when they join the server.""" """This method automatically adds roles to users when they join the server."""
if not await self.bot.cog_disabled_in_guild(self, member.guild): 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;""" 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 with database.execute(query, (member.id,)) as cursor:
async for row in cursor: async for row in cursor:
role = member.guild.get_role(row[1]) role = member.guild.get_role(row[1])
reason = row[2] reason = row[2]
await member.add_roles(role, reason=f"Role automatically added on member rejoin for: {reason} (Case #{row[0]:,})") await member.add_roles(role, reason=f"Role automatically added on member rejoin for: {reason} (Case #{row[0]:,})")
@commands.Cog.listener("on_audit_log_entry_create") @commands.Cog.listener("on_audit_log_entry_create")
async def autologger(self, entry: discord.AuditLogEntry): async def autologger(self, entry: discord.AuditLogEntry):

View file

@ -27,10 +27,11 @@ class ImportAuroraView(ui.View):
"Deleting original table...", ephemeral=True "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};" query = f"DROP TABLE IF EXISTS moderation_{self.ctx.guild.id};"
database.execute(query) database.execute(query)
database.commit() database.commit()
database.close()
await interaction.edit_original_response(content="Creating new table...") await interaction.edit_original_response(content="Creating new table...")

View file

@ -122,24 +122,26 @@ class Moderation(AuroraGuildModel):
from ..utilities.json import dumps 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 = ?;" 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, ( await database.execute(query, (
self.timestamp.timestamp(), self.timestamp.timestamp(),
self.moderation_type, self.moderation_type,
self.target_type, self.target_type,
self.moderator_id, self.moderator_id,
self.role_id, self.role_id,
str(self.duration) if self.duration else None, str(self.duration) if self.duration else None,
self.end_timestamp.timestamp() if self.end_timestamp else None, self.end_timestamp.timestamp() if self.end_timestamp else None,
self.reason, self.reason,
self.resolved, self.resolved,
self.resolved_by, self.resolved_by,
self.resolve_reason, self.resolve_reason,
self.expired, self.expired,
dumps(self.changes), dumps(self.changes),
dumps(self.metadata), dumps(self.metadata),
self.moderation_id, 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", 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, self.moderation_id,

View file

@ -1,7 +1,5 @@
# pylint: disable=cyclic-import # pylint: disable=cyclic-import
import json import json
from contextlib import asynccontextmanager
from typing import Any, AsyncGenerator
import aiosqlite import aiosqlite
from discord import Guild from discord import Guild
@ -10,14 +8,13 @@ from redbot.core import data_manager
from .logger import logger from .logger import logger
@asynccontextmanager async def connect() -> aiosqlite.Connection:
async def connect() -> AsyncGenerator[aiosqlite.Connection, Any, None]:
"""Connects to the SQLite database, and returns a connection object.""" """Connects to the SQLite database, and returns a connection object."""
try: try:
connection = await aiosqlite.connect( connection = await aiosqlite.connect(
database=data_manager.cog_data_path(raw_name="Aurora") / "aurora.db" database=data_manager.cog_data_path(raw_name="Aurora") / "aurora.db"
) )
yield connection return connection
except aiosqlite.OperationalError as e: except aiosqlite.OperationalError as e:
logger.error("Unable to access the SQLite database!\nError:\n%s", e.msg) 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}" f"Unable to access the SQLite Database!\n{e.msg}"
) from e ) from e
finally:
if connection:
await connection.close()
async def create_guild_table(guild: Guild): async def create_guild_table(guild: Guild):
database = await connect() database = await connect()