feat(aurora): migrated to aiosqlite
This commit is contained in:
parent
027144f35d
commit
5cbf4e7e47
9 changed files with 127 additions and 116 deletions
|
@ -1,22 +1,22 @@
|
|||
# pylint: disable=cyclic-import
|
||||
import json
|
||||
import sqlite3
|
||||
|
||||
import aiosqlite
|
||||
from discord import Guild
|
||||
from redbot.core import data_manager
|
||||
|
||||
from .logger import logger
|
||||
|
||||
|
||||
def connect() -> sqlite3.Connection:
|
||||
async def connect() -> aiosqlite.Connection:
|
||||
"""Connects to the SQLite database, and returns a connection object."""
|
||||
try:
|
||||
connection = sqlite3.connect(
|
||||
connection = await aiosqlite.connect(
|
||||
database=data_manager.cog_data_path(raw_name="Aurora") / "aurora.db"
|
||||
)
|
||||
return connection
|
||||
|
||||
except sqlite3.OperationalError as e:
|
||||
except aiosqlite.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}"
|
||||
|
@ -24,14 +24,13 @@ def connect() -> sqlite3.Connection:
|
|||
|
||||
|
||||
async def create_guild_table(guild: Guild):
|
||||
database = connect()
|
||||
cursor = database.cursor()
|
||||
database = await connect()
|
||||
|
||||
try:
|
||||
cursor.execute(f"SELECT * FROM `moderation_{guild.id}`")
|
||||
await database.execute(f"SELECT * FROM `moderation_{guild.id}`")
|
||||
logger.debug("SQLite Table exists for server %s (%s)", guild.name, guild.id)
|
||||
|
||||
except sqlite3.OperationalError:
|
||||
except aiosqlite.OperationalError:
|
||||
query = f"""
|
||||
CREATE TABLE `moderation_{guild.id}` (
|
||||
moderation_id INTEGER PRIMARY KEY,
|
||||
|
@ -52,16 +51,16 @@ async def create_guild_table(guild: Guild):
|
|||
metadata JSON NOT NULL
|
||||
)
|
||||
"""
|
||||
cursor.execute(query)
|
||||
await database.execute(query)
|
||||
|
||||
index_query_1 = f"CREATE INDEX IF NOT EXISTS idx_target_id ON moderation_{guild.id}(target_id);"
|
||||
cursor.execute(index_query_1)
|
||||
await database.execute(index_query_1)
|
||||
|
||||
index_query_2 = f"CREATE INDEX IF NOT EXISTS idx_moderator_id ON moderation_{guild.id}(moderator_id);"
|
||||
cursor.execute(index_query_2)
|
||||
await database.execute(index_query_2)
|
||||
|
||||
index_query_3 = f"CREATE INDEX IF NOT EXISTS idx_moderation_id ON moderation_{guild.id}(moderation_id);"
|
||||
cursor.execute(index_query_3)
|
||||
await database.execute(index_query_3)
|
||||
|
||||
insert_query = f"""
|
||||
INSERT INTO `moderation_{guild.id}`
|
||||
|
@ -86,9 +85,9 @@ async def create_guild_table(guild: Guild):
|
|||
json.dumps([]),
|
||||
json.dumps({}),
|
||||
)
|
||||
cursor.execute(insert_query, insert_values)
|
||||
await database.execute(insert_query, insert_values)
|
||||
|
||||
database.commit()
|
||||
await database.commit()
|
||||
|
||||
logger.debug(
|
||||
"SQLite Table (moderation_%s) created for %s (%s)",
|
||||
|
|
|
@ -120,7 +120,7 @@ async def log(interaction: Interaction, moderation_id: int, resolved: bool = Fal
|
|||
logging_channel = interaction.guild.get_channel(logging_channel_id)
|
||||
|
||||
try:
|
||||
moderation = Moderation.find_by_id(interaction.client, moderation_id, interaction.guild_id)
|
||||
moderation = await Moderation.find_by_id(interaction.client, moderation_id, interaction.guild_id)
|
||||
embed = await log_factory(
|
||||
interaction=interaction, moderation=moderation, resolved=resolved
|
||||
)
|
||||
|
@ -145,7 +145,7 @@ async def send_evidenceformat(interaction: Interaction, moderation_id: int) -> N
|
|||
if send_evidence_bool is False:
|
||||
return
|
||||
|
||||
moderation = Moderation.find_by_id(interaction.client, moderation_id, interaction.guild.id)
|
||||
moderation = await Moderation.find_by_id(interaction.client, moderation_id, interaction.guild.id)
|
||||
content = await evidenceformat_factory(moderation=moderation)
|
||||
await interaction.followup.send(content=content, ephemeral=True)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue