2023-12-17 03:11:51 -05:00
|
|
|
# pylint: disable=cyclic-import
|
|
|
|
|
2023-12-17 02:16:44 -05:00
|
|
|
import json
|
|
|
|
import time
|
2023-12-28 04:23:55 -05:00
|
|
|
import sqlite3
|
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
|
2023-12-28 04:39:57 -05:00
|
|
|
from .utils import 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:
|
2023-12-28 04:45:50 -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:09:12 -05:00
|
|
|
cursor.execute(f"SELECT * FROM `{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:09:12 -05:00
|
|
|
CREATE TABLE `{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:12:58 -05:00
|
|
|
index_query_1 = "CREATE INDEX idx_target_id ON {}(target_id);"
|
|
|
|
cursor.execute(index_query_1.format(guild.id))
|
2023-12-28 04:23:55 -05:00
|
|
|
|
2023-12-28 05:12:58 -05:00
|
|
|
index_query_2 = "CREATE INDEX idx_moderator_id ON {}(moderator_id);"
|
|
|
|
cursor.execute(index_query_2.format(guild.id))
|
2023-12-28 04:23:55 -05:00
|
|
|
|
2023-12-28 05:12:58 -05:00
|
|
|
index_query_3 = "CREATE INDEX idx_moderation_id ON {}(moderation_id);"
|
|
|
|
cursor.execute(index_query_3.format(guild.id))
|
2023-12-17 02:16:44 -05:00
|
|
|
|
|
|
|
insert_query = f"""
|
2023-12-28 05:09:12 -05:00
|
|
|
INSERT INTO `{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,
|
2023-12-17 02:36:18 -05:00
|
|
|
json.dumps([]), # pylint: disable=dangerous-default-value
|
|
|
|
json.dumps({}), # pylint: disable=dangerous-default-value
|
2023-12-17 02:16:44 -05:00
|
|
|
)
|
|
|
|
cursor.execute(insert_query, insert_values)
|
|
|
|
|
|
|
|
database.commit()
|
|
|
|
|
|
|
|
logger.debug(
|
2023-12-28 05:09:12 -05:00
|
|
|
"SQLite Table (%s) created for %s (%s)",
|
2023-12-17 02:16:44 -05:00
|
|
|
guild.id,
|
|
|
|
guild.name,
|
|
|
|
guild.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
database.close()
|
|
|
|
|
|
|
|
|
2023-12-28 04:45:50 -05:00
|
|
|
# pylint: disable=dangerous-default-value
|
2023-12-17 02:16:44 -05:00
|
|
|
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,
|
2023-12-28 04:45:50 -05:00
|
|
|
changes: list = [],
|
|
|
|
metadata: dict = {},
|
2023-12-28 04:39:57 -05:00
|
|
|
) -> 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
|
|
|
|
|
|
|
total_seconds = int(duration.total_seconds())
|
|
|
|
hours = total_seconds // 3600
|
|
|
|
minutes = (total_seconds % 3600) // 60
|
|
|
|
seconds = total_seconds % 60
|
|
|
|
duration = f"{hours}:{minutes}:{seconds}"
|
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:09:12 -05:00
|
|
|
sql = f"INSERT INTO `{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,
|
|
|
|
json.dumps(changes),
|
|
|
|
json.dumps(metadata),
|
|
|
|
)
|
|
|
|
cursor.execute(sql, val)
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
database.commit()
|
|
|
|
if close_db:
|
|
|
|
database.close()
|
|
|
|
|
|
|
|
logger.debug(
|
2023-12-28 05:09:12 -05:00
|
|
|
"Row inserted into %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:09:12 -05:00
|
|
|
query = f"SELECT * FROM {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)
|