feat(aurora): bunch of changes
This commit is contained in:
parent
f6a42b97d9
commit
0bcbcd6c0c
6 changed files with 122 additions and 149 deletions
|
@ -1,99 +0,0 @@
|
|||
# pylint: disable=cyclic-import
|
||||
import json
|
||||
|
||||
import aiosqlite
|
||||
from discord import Guild
|
||||
from redbot.core import data_manager
|
||||
|
||||
from .logger import logger
|
||||
|
||||
|
||||
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"
|
||||
)
|
||||
return connection
|
||||
|
||||
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}"
|
||||
) from e
|
||||
|
||||
|
||||
async def create_guild_table(guild: Guild):
|
||||
database = await connect()
|
||||
|
||||
try:
|
||||
await database.execute(f"SELECT * FROM `moderation_{guild.id}`")
|
||||
logger.verbose("SQLite Table exists for server %s (%s)", guild.name, guild.id)
|
||||
|
||||
except aiosqlite.OperationalError:
|
||||
query = f"""
|
||||
CREATE TABLE `moderation_{guild.id}` (
|
||||
moderation_id INTEGER PRIMARY KEY,
|
||||
timestamp INTEGER NOT NULL,
|
||||
moderation_type TEXT NOT NULL,
|
||||
target_type TEXT NOT NULL,
|
||||
target_id INTEGER NOT NULL,
|
||||
moderator_id INTEGER NOT NULL,
|
||||
role_id INTEGER,
|
||||
duration TEXT,
|
||||
end_timestamp INTEGER,
|
||||
reason TEXT,
|
||||
resolved INTEGER NOT NULL,
|
||||
resolved_by TEXT,
|
||||
resolve_reason TEXT,
|
||||
expired INTEGER NOT NULL,
|
||||
changes JSON NOT NULL,
|
||||
metadata JSON NOT NULL
|
||||
)
|
||||
"""
|
||||
await database.execute(query)
|
||||
|
||||
index_query_1 = f"CREATE INDEX IF NOT EXISTS idx_target_id ON moderation_{guild.id}(target_id);"
|
||||
await database.execute(index_query_1)
|
||||
|
||||
index_query_2 = f"CREATE INDEX IF NOT EXISTS idx_moderator_id ON moderation_{guild.id}(moderator_id);"
|
||||
await database.execute(index_query_2)
|
||||
|
||||
index_query_3 = f"CREATE INDEX IF NOT EXISTS idx_moderation_id ON moderation_{guild.id}(moderation_id);"
|
||||
await database.execute(index_query_3)
|
||||
|
||||
insert_query = f"""
|
||||
INSERT INTO `moderation_{guild.id}`
|
||||
(moderation_id, timestamp, moderation_type, target_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes, metadata)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
"""
|
||||
insert_values = (
|
||||
0,
|
||||
0,
|
||||
"NULL",
|
||||
"NULL",
|
||||
0,
|
||||
0,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
0,
|
||||
None,
|
||||
None,
|
||||
0,
|
||||
json.dumps([]),
|
||||
json.dumps({}),
|
||||
)
|
||||
await database.execute(insert_query, insert_values)
|
||||
|
||||
await database.commit()
|
||||
|
||||
logger.debug(
|
||||
"SQLite Table (moderation_%s) created for %s (%s)",
|
||||
guild.id,
|
||||
guild.name,
|
||||
guild.id,
|
||||
)
|
||||
|
||||
await database.close()
|
|
@ -2,6 +2,7 @@
|
|||
from datetime import datetime, timedelta
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
import aiosqlite
|
||||
from dateutil.relativedelta import relativedelta as rd
|
||||
from discord import File, Guild, Interaction, Member, SelectOption, TextChannel, User
|
||||
from discord.errors import Forbidden
|
||||
|
@ -9,6 +10,8 @@ from redbot.core import commands, data_manager
|
|||
from redbot.core.utils.chat_formatting import error
|
||||
|
||||
from ..utilities.config import config
|
||||
from ..utilities.json import dumps
|
||||
from ..utilities.logger import logger
|
||||
|
||||
|
||||
def check_permissions(
|
||||
|
@ -210,3 +213,69 @@ def get_footer_image(coginstance: commands.Cog) -> File:
|
|||
"""Returns the footer image for the embeds."""
|
||||
image_path = data_manager.bundled_data_path(coginstance) / "arrow.png"
|
||||
return File(image_path, filename="arrow.png", description="arrow")
|
||||
|
||||
async def create_guild_table(guild: Guild) -> None:
|
||||
from ..models.moderation import Moderation
|
||||
|
||||
try:
|
||||
await Moderation.execute(f"SELECT * FROM `moderation_{guild.id}`", return_obj=False)
|
||||
logger.trace("SQLite Table exists for server %s (%s)", guild.name, guild.id)
|
||||
|
||||
except aiosqlite.OperationalError:
|
||||
query = f"""
|
||||
CREATE TABLE `moderation_{guild.id}` (
|
||||
moderation_id INTEGER PRIMARY KEY,
|
||||
timestamp INTEGER NOT NULL,
|
||||
moderation_type TEXT NOT NULL,
|
||||
target_type TEXT NOT NULL,
|
||||
target_id INTEGER NOT NULL,
|
||||
moderator_id INTEGER NOT NULL,
|
||||
role_id INTEGER,
|
||||
duration TEXT,
|
||||
end_timestamp INTEGER,
|
||||
reason TEXT,
|
||||
resolved INTEGER NOT NULL,
|
||||
resolved_by TEXT,
|
||||
resolve_reason TEXT,
|
||||
expired INTEGER NOT NULL,
|
||||
changes JSON NOT NULL,
|
||||
metadata JSON NOT NULL
|
||||
)
|
||||
"""
|
||||
await Moderation.execute(query=query, return_obj=False)
|
||||
|
||||
index_query_1 = f"CREATE INDEX IF NOT EXISTS idx_target_id ON moderation_{guild.id}(target_id);"
|
||||
await Moderation.execute(query=index_query_1, return_obj=False)
|
||||
|
||||
index_query_2 = f"CREATE INDEX IF NOT EXISTS idx_moderator_id ON moderation_{guild.id}(moderator_id);"
|
||||
await Moderation.execute(query=index_query_2, return_obj=False)
|
||||
|
||||
index_query_3 = f"CREATE INDEX IF NOT EXISTS idx_moderation_id ON moderation_{guild.id}(moderation_id);"
|
||||
await Moderation.execute(query=index_query_3, return_obj=False)
|
||||
|
||||
insert_query = f"""
|
||||
INSERT INTO `moderation_{guild.id}`
|
||||
(moderation_id, timestamp, moderation_type, target_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes, metadata)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
"""
|
||||
insert_values = (
|
||||
0,
|
||||
0,
|
||||
"NULL",
|
||||
"NULL",
|
||||
0,
|
||||
0,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
0,
|
||||
None,
|
||||
None,
|
||||
0,
|
||||
dumps([]),
|
||||
dumps({}),
|
||||
)
|
||||
await Moderation.execute(query=insert_query, parameters=insert_values, return_obj=False)
|
||||
|
||||
logger.trace("SQLite Table created for server %s (%s)", guild.name, guild.id)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue