2024-02-28 10:58:57 -05:00
|
|
|
# pylint: disable=cyclic-import
|
2023-12-17 02:16:44 -05:00
|
|
|
import json
|
2023-12-28 04:23:55 -05:00
|
|
|
import sqlite3
|
2024-02-02 11:22:08 -05:00
|
|
|
import time
|
2023-12-28 05:01:08 -05:00
|
|
|
from datetime import datetime, timedelta
|
2023-12-18 17:24:40 -05:00
|
|
|
|
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
|
|
|
|
2023-12-17 02:16:44 -05:00
|
|
|
from .logger import logger
|
2024-02-28 11:01:33 -05:00
|
|
|
from .utils import (convert_timedelta_to_str, generate_dict,
|
|
|
|
get_next_case_number)
|
2023-12-17 02:16:44 -05:00
|
|
|
|
|
|
|
|
2023-12-28 04:23:55 -05:00
|
|
|
def connect() -> sqlite3.Connection:
|
|
|
|
"""Connects to the SQLite database, and returns a connection object."""
|
2023-12-17 02:16:44 -05:00
|
|
|
try:
|
2024-02-02 11:22:08 -05:00
|
|
|
connection = sqlite3.connect(
|
|
|
|
database=data_manager.cog_data_path(raw_name="Aurora") / "aurora.db"
|
|
|
|
)
|
2023-12-17 02:16:44 -05:00
|
|
|
return connection
|
|
|
|
|
2023-12-28 04:47:16 -05:00
|
|
|
except sqlite3.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):
|
2023-12-28 04:23:55 -05:00
|
|
|
database = connect()
|
2023-12-17 02:16:44 -05:00
|
|
|
cursor = database.cursor()
|
|
|
|
|
|
|
|
try:
|
2023-12-28 05:13:13 -05:00
|
|
|
cursor.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
|
|
|
|
2023-12-28 04:47:16 -05:00
|
|
|
except sqlite3.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,
|
|
|
|
target_id TEXT NOT NULL,
|
|
|
|
moderator_id TEXT NOT NULL,
|
|
|
|
role_id TEXT,
|
|
|
|
duration TEXT,
|
|
|
|
end_timestamp INTEGER,
|
|
|
|
reason TEXT,
|
|
|
|
resolved INTEGER NOT NULL,
|
|
|
|
resolved_by TEXT,
|
|
|
|
resolve_reason TEXT,
|
|
|
|
expired INTEGER NOT NULL,
|
|
|
|
changes TEXT NOT NULL,
|
|
|
|
metadata TEXT NOT NULL
|
2023-12-17 02:16:44 -05:00
|
|
|
)
|
|
|
|
"""
|
|
|
|
cursor.execute(query)
|
|
|
|
|
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);"
|
2023-12-28 05:25:45 -05:00
|
|
|
cursor.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);"
|
2023-12-28 05:25:45 -05:00
|
|
|
cursor.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);"
|
2023-12-28 05:25:45 -05:00
|
|
|
cursor.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,
|
|
|
|
0,
|
|
|
|
"NULL",
|
|
|
|
0,
|
|
|
|
"NULL",
|
|
|
|
0,
|
|
|
|
"NULL",
|
|
|
|
"NULL",
|
|
|
|
0,
|
2024-02-14 11:04:26 -05:00
|
|
|
json.dumps([]),
|
|
|
|
json.dumps({}),
|
2023-12-17 02:16:44 -05:00
|
|
|
)
|
|
|
|
cursor.execute(insert_query, insert_values)
|
|
|
|
|
|
|
|
database.commit()
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
database.close()
|
|
|
|
|
|
|
|
|
|
|
|
async def mysql_log(
|
|
|
|
guild_id: str,
|
|
|
|
author_id: str,
|
|
|
|
moderation_type: str,
|
2023-12-17 12:54:41 -05:00
|
|
|
target_type: str,
|
2023-12-17 02:16:44 -05:00
|
|
|
target_id: int,
|
|
|
|
role_id: int,
|
2023-12-28 05:01:08 -05:00
|
|
|
duration: timedelta,
|
2023-12-17 02:16:44 -05:00
|
|
|
reason: str,
|
2023-12-28 04:23:55 -05:00
|
|
|
database: sqlite3.Connection = None,
|
2023-12-17 02:16:44 -05:00
|
|
|
timestamp: int = None,
|
|
|
|
resolved: bool = False,
|
|
|
|
resolved_by: str = None,
|
|
|
|
resolved_reason: str = None,
|
|
|
|
expired: bool = None,
|
2024-02-28 11:01:33 -05:00
|
|
|
changes: list = None,
|
|
|
|
metadata: dict = None,
|
|
|
|
) -> int:
|
2023-12-17 02:16:44 -05:00
|
|
|
if not timestamp:
|
|
|
|
timestamp = int(time.time())
|
|
|
|
|
|
|
|
if duration != "NULL":
|
|
|
|
end_timedelta = datetime.fromtimestamp(timestamp) + duration
|
|
|
|
end_timestamp = int(end_timedelta.timestamp())
|
2023-12-28 05:01:08 -05:00
|
|
|
|
2023-12-30 04:10:25 -05:00
|
|
|
duration = convert_timedelta_to_str(duration)
|
2023-12-17 02:16:44 -05:00
|
|
|
else:
|
|
|
|
end_timestamp = 0
|
|
|
|
|
|
|
|
if not expired:
|
|
|
|
if int(time.time()) > end_timestamp:
|
|
|
|
expired = 1
|
|
|
|
else:
|
|
|
|
expired = 0
|
|
|
|
|
|
|
|
if resolved_by is None:
|
|
|
|
resolved_by = "NULL"
|
|
|
|
|
|
|
|
if resolved_reason is None:
|
|
|
|
resolved_reason = "NULL"
|
|
|
|
|
|
|
|
if not database:
|
2023-12-28 04:23:55 -05:00
|
|
|
database = connect()
|
2023-12-17 02:16:44 -05:00
|
|
|
close_db = True
|
|
|
|
else:
|
|
|
|
close_db = False
|
|
|
|
cursor = database.cursor()
|
|
|
|
|
|
|
|
moderation_id = await get_next_case_number(guild_id=guild_id, cursor=cursor)
|
|
|
|
|
2023-12-28 05:13:13 -05:00
|
|
|
sql = 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
2023-12-17 02:16:44 -05:00
|
|
|
val = (
|
|
|
|
moderation_id,
|
|
|
|
timestamp,
|
|
|
|
moderation_type,
|
2023-12-17 12:54:41 -05:00
|
|
|
target_type,
|
2023-12-17 02:16:44 -05:00
|
|
|
target_id,
|
|
|
|
author_id,
|
|
|
|
role_id,
|
|
|
|
duration,
|
|
|
|
end_timestamp,
|
|
|
|
reason,
|
|
|
|
int(resolved),
|
|
|
|
resolved_by,
|
|
|
|
resolved_reason,
|
|
|
|
expired,
|
2024-02-28 11:01:33 -05:00
|
|
|
json.dumps(changes if changes else []),
|
|
|
|
json.dumps(metadata if metadata else {}),
|
2023-12-17 02:16:44 -05:00
|
|
|
)
|
|
|
|
cursor.execute(sql, val)
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
database.commit()
|
|
|
|
if close_db:
|
|
|
|
database.close()
|
|
|
|
|
|
|
|
logger.debug(
|
2023-12-28 05:13:13 -05:00
|
|
|
"Row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s",
|
2023-12-17 02:16:44 -05:00
|
|
|
guild_id,
|
|
|
|
moderation_id,
|
|
|
|
timestamp,
|
|
|
|
moderation_type,
|
2023-12-17 12:54:41 -05:00
|
|
|
target_type,
|
2023-12-17 02:16:44 -05:00
|
|
|
target_id,
|
|
|
|
author_id,
|
|
|
|
role_id,
|
|
|
|
duration,
|
|
|
|
end_timestamp,
|
|
|
|
reason,
|
|
|
|
int(resolved),
|
|
|
|
resolved_by,
|
|
|
|
resolved_reason,
|
|
|
|
expired,
|
|
|
|
changes,
|
|
|
|
metadata,
|
|
|
|
)
|
|
|
|
|
|
|
|
return moderation_id
|
|
|
|
|
|
|
|
|
2023-12-28 04:23:55 -05:00
|
|
|
async def fetch_case(moderation_id: int, guild_id: str) -> dict:
|
2023-12-17 02:16:44 -05:00
|
|
|
"""This method fetches a case from the database and returns the case's dictionary."""
|
2023-12-28 04:23:55 -05:00
|
|
|
database = connect()
|
2023-12-17 02:16:44 -05:00
|
|
|
cursor = database.cursor()
|
|
|
|
|
2023-12-28 05:13:13 -05:00
|
|
|
query = f"SELECT * FROM moderation_{guild_id} WHERE moderation_id = ?;"
|
2023-12-28 04:39:57 -05:00
|
|
|
cursor.execute(query, (moderation_id,))
|
2023-12-17 02:16:44 -05:00
|
|
|
result = cursor.fetchone()
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
database.close()
|
|
|
|
|
|
|
|
return generate_dict(result)
|