2024-02-28 10:58:57 -05:00
|
|
|
# pylint: disable=cyclic-import
|
2023-12-17 02:16:44 -05:00
|
|
|
import json
|
2023-12-18 17:24:40 -05:00
|
|
|
|
2024-06-05 00:14:43 -04:00
|
|
|
import aiosqlite
|
2023-12-17 02:16:44 -05:00
|
|
|
from discord import Guild
|
2023-12-28 04:23:55 -05:00
|
|
|
from redbot.core import data_manager
|
2023-12-18 17:24:40 -05:00
|
|
|
|
2024-05-06 21:39:43 -04:00
|
|
|
from .logger import logger
|
2023-12-17 02:16:44 -05:00
|
|
|
|
|
|
|
|
2024-06-05 00:39:56 -04:00
|
|
|
async def connect() -> aiosqlite.Connection:
|
2023-12-28 04:23:55 -05:00
|
|
|
"""Connects to the SQLite database, and returns a connection object."""
|
2023-12-17 02:16:44 -05:00
|
|
|
try:
|
2024-06-05 00:14:43 -04:00
|
|
|
connection = await aiosqlite.connect(
|
2024-02-02 11:22:08 -05:00
|
|
|
database=data_manager.cog_data_path(raw_name="Aurora") / "aurora.db"
|
|
|
|
)
|
2024-06-05 00:39:56 -04:00
|
|
|
return connection
|
2023-12-17 02:16:44 -05:00
|
|
|
|
2024-06-05 00:14:43 -04:00
|
|
|
except aiosqlite.OperationalError as e:
|
2023-12-28 04:23:55 -05:00
|
|
|
logger.error("Unable to access the SQLite database!\nError:\n%s", e.msg)
|
2023-12-17 02:16:44 -05:00
|
|
|
raise ConnectionRefusedError(
|
2023-12-28 04:23:55 -05:00
|
|
|
f"Unable to access the SQLite Database!\n{e.msg}"
|
2023-12-17 02:16:44 -05:00
|
|
|
) from e
|
|
|
|
|
|
|
|
|
|
|
|
async def create_guild_table(guild: Guild):
|
2024-06-05 00:14:43 -04:00
|
|
|
database = await connect()
|
2023-12-17 02:16:44 -05:00
|
|
|
|
|
|
|
try:
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.execute(f"SELECT * FROM `moderation_{guild.id}`")
|
2023-12-28 04:23:55 -05:00
|
|
|
logger.debug("SQLite Table exists for server %s (%s)", guild.name, guild.id)
|
2023-12-17 02:16:44 -05:00
|
|
|
|
2024-06-05 00:14:43 -04:00
|
|
|
except aiosqlite.OperationalError:
|
2023-12-17 02:16:44 -05:00
|
|
|
query = f"""
|
2023-12-28 05:13:13 -05:00
|
|
|
CREATE TABLE `moderation_{guild.id}` (
|
2023-12-28 04:39:57 -05:00
|
|
|
moderation_id INTEGER PRIMARY KEY,
|
|
|
|
timestamp INTEGER NOT NULL,
|
|
|
|
moderation_type TEXT NOT NULL,
|
|
|
|
target_type TEXT NOT NULL,
|
2024-05-04 14:49:07 -04:00
|
|
|
target_id INTEGER NOT NULL,
|
|
|
|
moderator_id INTEGER NOT NULL,
|
|
|
|
role_id INTEGER,
|
2023-12-28 04:39:57 -05:00
|
|
|
duration TEXT,
|
|
|
|
end_timestamp INTEGER,
|
|
|
|
reason TEXT,
|
|
|
|
resolved INTEGER NOT NULL,
|
|
|
|
resolved_by TEXT,
|
|
|
|
resolve_reason TEXT,
|
|
|
|
expired INTEGER NOT NULL,
|
2024-05-04 14:49:07 -04:00
|
|
|
changes JSON NOT NULL,
|
|
|
|
metadata JSON NOT NULL
|
2023-12-17 02:16:44 -05:00
|
|
|
)
|
|
|
|
"""
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.execute(query)
|
2023-12-17 02:16:44 -05:00
|
|
|
|
2023-12-28 05:29:47 -05:00
|
|
|
index_query_1 = f"CREATE INDEX IF NOT EXISTS idx_target_id ON moderation_{guild.id}(target_id);"
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.execute(index_query_1)
|
2023-12-28 04:23:55 -05:00
|
|
|
|
2023-12-28 05:29:47 -05:00
|
|
|
index_query_2 = f"CREATE INDEX IF NOT EXISTS idx_moderator_id ON moderation_{guild.id}(moderator_id);"
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.execute(index_query_2)
|
2023-12-28 04:23:55 -05:00
|
|
|
|
2023-12-28 05:29:47 -05:00
|
|
|
index_query_3 = f"CREATE INDEX IF NOT EXISTS idx_moderation_id ON moderation_{guild.id}(moderation_id);"
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.execute(index_query_3)
|
2023-12-17 02:16:44 -05:00
|
|
|
|
|
|
|
insert_query = f"""
|
2023-12-28 05:13:13 -05:00
|
|
|
INSERT INTO `moderation_{guild.id}`
|
2023-12-17 12:54:41 -05:00
|
|
|
(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)
|
2023-12-28 04:39:57 -05:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2023-12-17 02:16:44 -05:00
|
|
|
"""
|
|
|
|
insert_values = (
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"NULL",
|
2023-12-17 12:54:41 -05:00
|
|
|
"NULL",
|
2023-12-17 02:16:44 -05:00
|
|
|
0,
|
|
|
|
0,
|
2024-05-06 16:34:08 -04:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
None,
|
2023-12-17 02:16:44 -05:00
|
|
|
0,
|
2024-05-06 16:34:08 -04:00
|
|
|
None,
|
|
|
|
None,
|
2023-12-17 02:16:44 -05:00
|
|
|
0,
|
2024-02-14 11:04:26 -05:00
|
|
|
json.dumps([]),
|
|
|
|
json.dumps({}),
|
2023-12-17 02:16:44 -05:00
|
|
|
)
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.execute(insert_query, insert_values)
|
2023-12-17 02:16:44 -05:00
|
|
|
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.commit()
|
2023-12-17 02:16:44 -05:00
|
|
|
|
|
|
|
logger.debug(
|
2023-12-28 05:13:13 -05:00
|
|
|
"SQLite Table (moderation_%s) created for %s (%s)",
|
2023-12-17 02:16:44 -05:00
|
|
|
guild.id,
|
|
|
|
guild.name,
|
|
|
|
guild.id,
|
|
|
|
)
|
|
|
|
|
2024-06-05 01:05:11 -04:00
|
|
|
await database.close()
|