2024-05-06 15:11:11 -04:00
|
|
|
import json
|
2024-05-06 14:15:05 -04:00
|
|
|
import sqlite3
|
2024-05-04 14:08:53 -04:00
|
|
|
from datetime import datetime, timedelta
|
2024-05-06 14:15:05 -04:00
|
|
|
from time import time
|
2024-06-03 00:55:09 -04:00
|
|
|
from typing import Dict, Iterable, List, Optional, Tuple, Union
|
2024-05-04 13:41:11 -04:00
|
|
|
|
2024-05-06 15:29:48 -04:00
|
|
|
import discord
|
2024-06-08 20:12:22 -04:00
|
|
|
from aiosqlite import Connection, Cursor, OperationalError, Row
|
|
|
|
from aiosqlite import connect as aiosqlite_connect
|
|
|
|
from redbot.core import data_manager
|
2024-05-04 16:54:12 -04:00
|
|
|
from redbot.core.bot import Red
|
2024-05-04 13:42:58 -04:00
|
|
|
|
2024-05-06 21:39:43 -04:00
|
|
|
from ..utilities.logger import logger
|
2024-06-04 15:22:50 -04:00
|
|
|
from ..utilities.utils import timedelta_to_string
|
2024-05-09 21:27:26 -04:00
|
|
|
from .base import AuroraGuildModel
|
|
|
|
from .change import Change
|
|
|
|
from .partials import PartialChannel, PartialRole, PartialUser
|
2024-07-12 15:22:24 -04:00
|
|
|
from .type import Type, type_registry
|
2024-05-04 13:41:11 -04:00
|
|
|
|
2024-05-04 15:37:07 -04:00
|
|
|
|
2024-05-04 18:17:21 -04:00
|
|
|
class Moderation(AuroraGuildModel):
|
2024-07-12 15:22:24 -04:00
|
|
|
"""This class represents a moderation case in the database.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
bot (Red): The bot instance.
|
|
|
|
guild (discord.Guild): The guild the case belongs to.
|
|
|
|
moderation_id (int): The ID of the moderation case.
|
|
|
|
timestamp (datetime): The timestamp of the case.
|
|
|
|
moderation_type (Type): The type of moderation case.
|
|
|
|
target_type (str): The type of target. Should be either `user` or `channel`.
|
|
|
|
target_id (int): The ID of the target.
|
|
|
|
moderator_id (int): The ID of the moderator who issued the case.
|
|
|
|
role_id (int): The ID of the role, if applicable.
|
|
|
|
duration (timedelta): The duration of the case, if applicable.
|
|
|
|
end_timestamp (datetime): The end timestamp of the case, if applicable.
|
|
|
|
reason (str): The reason for the case.
|
|
|
|
resolved (bool): Whether the case is resolved.
|
|
|
|
resolved_by (int): The ID of the user who resolved the case.
|
|
|
|
resolve_reason (str): The reason the case was resolved.
|
|
|
|
expired (bool): Whether the case is expired.
|
|
|
|
changes (List[Change]): A list of changes to the case.
|
|
|
|
metadata (Dict): A dictionary of metadata stored with the case.
|
|
|
|
|
|
|
|
Properties:
|
|
|
|
id (int): The ID of the case.
|
|
|
|
type (Type): The type of the case.
|
|
|
|
unix_timestamp (int): The timestamp of the case as a Unix timestamp.
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
get_moderator: Gets the moderator who issued the case.
|
|
|
|
get_target: Gets the target of the case.
|
|
|
|
get_resolved_by: Gets the user who resolved the case.
|
|
|
|
get_role: Gets the role, if applicable.
|
|
|
|
resolve: Resolves the case.
|
|
|
|
update: Updates the case in the database.
|
|
|
|
|
|
|
|
Class Methods:
|
|
|
|
from_dict: Creates a `Moderation` object from a dictionary.
|
|
|
|
from_result: Creates a `Moderation` object from a database result.
|
|
|
|
execute: Executes a query on the database.
|
|
|
|
get_latest: Gets the latest cases from the database.
|
|
|
|
get_next_case_number: Gets the next case number to use.
|
|
|
|
find_by_id: Finds a case by its ID.
|
|
|
|
find_by_target: Finds cases by the target.
|
|
|
|
find_by_moderator: Finds cases by the moderator.
|
|
|
|
log: Logs a moderation case in the database.
|
|
|
|
|
|
|
|
Static Methods:
|
|
|
|
connect: Connects to the SQLite database.
|
|
|
|
"""
|
|
|
|
|
2024-05-04 13:41:11 -04:00
|
|
|
moderation_id: int
|
2024-05-04 14:08:53 -04:00
|
|
|
timestamp: datetime
|
2024-07-06 12:03:49 -04:00
|
|
|
moderation_type: Type
|
2024-05-04 13:41:11 -04:00
|
|
|
target_type: str
|
|
|
|
target_id: int
|
|
|
|
moderator_id: int
|
2024-05-04 18:05:20 -04:00
|
|
|
role_id: Optional[int] = None
|
|
|
|
duration: Optional[timedelta] = None
|
|
|
|
end_timestamp: Optional[datetime] = None
|
|
|
|
reason: Optional[str] = None
|
2024-05-04 13:41:11 -04:00
|
|
|
resolved: bool
|
2024-05-04 18:05:20 -04:00
|
|
|
resolved_by: Optional[int] = None
|
|
|
|
resolve_reason: Optional[str] = None
|
2024-05-04 13:41:11 -04:00
|
|
|
expired: bool
|
2024-05-04 18:05:20 -04:00
|
|
|
changes: List["Change"]
|
2024-05-04 13:41:11 -04:00
|
|
|
metadata: Dict
|
|
|
|
|
2024-05-04 16:54:12 -04:00
|
|
|
@property
|
|
|
|
def id(self) -> int:
|
|
|
|
return self.moderation_id
|
|
|
|
|
|
|
|
@property
|
2024-07-06 12:27:59 -04:00
|
|
|
def type(self) -> Type:
|
2024-05-04 16:54:12 -04:00
|
|
|
return self.moderation_type
|
|
|
|
|
2024-05-04 21:06:29 -04:00
|
|
|
@property
|
|
|
|
def unix_timestamp(self) -> int:
|
|
|
|
return int(self.timestamp.timestamp())
|
|
|
|
|
2024-05-04 17:09:22 -04:00
|
|
|
async def get_moderator(self) -> "PartialUser":
|
2024-05-04 16:54:12 -04:00
|
|
|
return await PartialUser.from_id(self.bot, self.moderator_id)
|
|
|
|
|
2024-05-04 17:09:22 -04:00
|
|
|
async def get_target(self) -> Union["PartialUser", "PartialChannel"]:
|
2024-07-12 15:22:24 -04:00
|
|
|
if self.target_type.lower() == "user":
|
2024-05-04 16:54:12 -04:00
|
|
|
return await PartialUser.from_id(self.bot, self.target_id)
|
2024-06-05 23:13:23 -04:00
|
|
|
return await PartialChannel.from_id(self.bot, self.target_id, self.guild)
|
2024-05-04 16:54:12 -04:00
|
|
|
|
2024-05-04 17:09:22 -04:00
|
|
|
async def get_resolved_by(self) -> Optional["PartialUser"]:
|
2024-05-04 16:54:12 -04:00
|
|
|
if self.resolved_by:
|
|
|
|
return await PartialUser.from_id(self.bot, self.resolved_by)
|
|
|
|
return None
|
|
|
|
|
2024-05-04 17:31:16 -04:00
|
|
|
async def get_role(self) -> Optional["PartialRole"]:
|
|
|
|
if self.role_id:
|
2024-06-05 23:13:23 -04:00
|
|
|
return await PartialRole.from_id(self.bot, self.guild, self.role_id)
|
2024-05-04 17:31:16 -04:00
|
|
|
return None
|
|
|
|
|
2024-06-03 23:46:22 -04:00
|
|
|
def __str__(self) -> str:
|
2024-05-04 13:41:11 -04:00
|
|
|
return f"{self.moderation_type} {self.target_type} {self.target_id} {self.reason}"
|
|
|
|
|
2024-06-04 12:44:58 -04:00
|
|
|
def __int__(self) -> int:
|
|
|
|
return self.moderation_id
|
|
|
|
|
2024-08-22 15:33:36 -04:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
attrs = [
|
|
|
|
('guild_id', self.guild_id),
|
|
|
|
('moderation_id', self.moderation_id),
|
|
|
|
('timestamp', self.timestamp),
|
|
|
|
('type', self.type),
|
|
|
|
('target_type', self.target_type),
|
|
|
|
('target_id', self.target_id),
|
|
|
|
('moderator_id', self.moderator_id),
|
|
|
|
('role_id', self.role_id),
|
|
|
|
('duration', self.duration),
|
|
|
|
('end_timestamp', self.end_timestamp),
|
|
|
|
('reason', self.reason),
|
|
|
|
('resolved', self.resolved),
|
|
|
|
('resolved_by', self.resolved_by),
|
|
|
|
('resolve_reason', self.resolve_reason),
|
|
|
|
('expired', self.expired),
|
|
|
|
('changes', self.changes),
|
|
|
|
('metadata', self.metadata),
|
|
|
|
]
|
|
|
|
joined = ' '.join('%s=%r' % t for t in attrs)
|
|
|
|
return f"<{self.__class__.__name__} {joined}>"
|
|
|
|
|
2024-07-12 15:22:24 -04:00
|
|
|
async def resolve(self, resolved_by: int, reason: str) -> Tuple[bool, str]:
|
2024-05-06 15:29:48 -04:00
|
|
|
if self.resolved:
|
|
|
|
raise ValueError("Case is already resolved!")
|
|
|
|
|
|
|
|
self.resolved = True
|
|
|
|
self.resolved_by = resolved_by
|
|
|
|
self.resolve_reason = reason
|
|
|
|
|
2024-07-12 15:22:24 -04:00
|
|
|
success, msg = await self.type.resolve_handler(moderation=self, reason=reason)
|
2024-07-06 12:15:39 -04:00
|
|
|
|
2024-05-06 15:29:48 -04:00
|
|
|
if not self.changes:
|
|
|
|
self.changes.append(Change.from_dict(self.bot, {
|
|
|
|
"type": "ORIGINAL",
|
|
|
|
"timestamp": self.timestamp,
|
|
|
|
"reason": self.reason,
|
|
|
|
"user_id": self.moderator_id,
|
|
|
|
"duration": self.duration,
|
|
|
|
"end_timestamp": self.end_timestamp,
|
|
|
|
}))
|
|
|
|
self.changes.append(Change.from_dict(self.bot, {
|
|
|
|
"type": "RESOLVE",
|
|
|
|
"timestamp": datetime.now(),
|
|
|
|
"reason": reason,
|
|
|
|
"user_id": resolved_by,
|
|
|
|
}))
|
|
|
|
|
2024-06-05 00:14:43 -04:00
|
|
|
await self.update()
|
2024-07-12 15:22:24 -04:00
|
|
|
return success, msg
|
2024-05-06 15:29:48 -04:00
|
|
|
|
2024-06-05 00:14:43 -04:00
|
|
|
async def update(self) -> None:
|
2024-05-06 21:46:01 -04:00
|
|
|
from ..utilities.json import dumps
|
2024-05-04 22:01:32 -04:00
|
|
|
query = f"UPDATE moderation_{self.guild_id} SET timestamp = ?, moderation_type = ?, target_type = ?, moderator_id = ?, role_id = ?, duration = ?, end_timestamp = ?, reason = ?, resolved = ?, resolved_by = ?, resolve_reason = ?, expired = ?, changes = ?, metadata = ? WHERE moderation_id = ?;"
|
|
|
|
|
2024-06-05 23:13:23 -04:00
|
|
|
await self.execute(query, (
|
2024-06-05 00:39:56 -04:00
|
|
|
self.timestamp.timestamp(),
|
2024-07-12 16:33:00 -04:00
|
|
|
self.moderation_type.key,
|
2024-06-05 00:39:56 -04:00
|
|
|
self.target_type,
|
|
|
|
self.moderator_id,
|
|
|
|
self.role_id,
|
2024-08-14 15:26:55 -04:00
|
|
|
timedelta_to_string(self.duration) if self.duration else None,
|
2024-06-05 00:39:56 -04:00
|
|
|
self.end_timestamp.timestamp() if self.end_timestamp else None,
|
|
|
|
self.reason,
|
|
|
|
self.resolved,
|
|
|
|
self.resolved_by,
|
|
|
|
self.resolve_reason,
|
|
|
|
self.expired,
|
|
|
|
dumps(self.changes),
|
|
|
|
dumps(self.metadata),
|
|
|
|
self.moderation_id,
|
|
|
|
))
|
2024-05-04 22:01:32 -04:00
|
|
|
|
2024-06-05 23:13:23 -04:00
|
|
|
logger.verbose("Row updated in moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s",
|
2024-05-04 22:17:52 -04:00
|
|
|
self.moderation_id,
|
|
|
|
self.guild_id,
|
2024-05-06 14:55:36 -04:00
|
|
|
self.timestamp.timestamp(),
|
2024-07-12 16:33:00 -04:00
|
|
|
self.moderation_type.key,
|
2024-05-04 22:17:52 -04:00
|
|
|
self.target_type,
|
|
|
|
self.moderator_id,
|
|
|
|
self.role_id,
|
2024-08-14 15:08:37 -04:00
|
|
|
timedelta_to_string(self.duration) if self.duration else None,
|
2024-05-06 14:55:36 -04:00
|
|
|
self.end_timestamp.timestamp() if self.end_timestamp else None,
|
2024-05-04 22:17:52 -04:00
|
|
|
self.reason,
|
|
|
|
self.resolved,
|
|
|
|
self.resolved_by,
|
|
|
|
self.resolve_reason,
|
|
|
|
self.expired,
|
2024-05-06 14:55:36 -04:00
|
|
|
dumps(self.changes),
|
|
|
|
dumps(self.metadata),
|
2024-05-04 22:17:52 -04:00
|
|
|
)
|
|
|
|
|
2024-05-06 14:55:36 -04:00
|
|
|
@classmethod
|
2024-07-12 15:22:24 -04:00
|
|
|
async def from_dict(cls, bot: Red, data: dict) -> "Moderation":
|
2024-06-05 23:13:23 -04:00
|
|
|
if data.get("guild_id"):
|
|
|
|
try:
|
2024-07-12 15:22:24 -04:00
|
|
|
guild = bot.get_guild(data["guild_id"])
|
2024-06-05 23:13:23 -04:00
|
|
|
if not guild:
|
2024-07-12 15:22:24 -04:00
|
|
|
guild = await bot.fetch_guild(data["guild_id"])
|
2024-06-05 23:13:23 -04:00
|
|
|
except (discord.Forbidden, discord.HTTPException):
|
|
|
|
guild = None
|
|
|
|
data.update({"guild": guild})
|
2024-05-06 14:55:36 -04:00
|
|
|
return cls(bot=bot, **data)
|
|
|
|
|
2024-05-06 16:34:08 -04:00
|
|
|
@classmethod
|
2024-07-12 15:22:24 -04:00
|
|
|
async def from_result(cls, bot: Red, result: Iterable, guild_id: int) -> "Moderation":
|
2024-06-04 16:52:19 -04:00
|
|
|
if result[7] is not None and result[7] != "NULL":
|
2024-08-14 14:37:32 -04:00
|
|
|
try:
|
|
|
|
hours, minutes, seconds = map(int, result[7].split(':'))
|
|
|
|
duration = timedelta(hours=hours, minutes=minutes, seconds=seconds)
|
|
|
|
except ValueError as e:
|
|
|
|
logger.error("Error parsing duration for case %s: %s", result[0], result[7])
|
|
|
|
raise e
|
2024-05-06 16:34:08 -04:00
|
|
|
else:
|
|
|
|
duration = None
|
|
|
|
|
|
|
|
if result[14] is not None:
|
|
|
|
changes = json.loads(result[14])
|
|
|
|
change_obj_list = []
|
2024-05-24 03:53:38 -04:00
|
|
|
if changes:
|
|
|
|
for change in changes:
|
|
|
|
change_obj_list.append(Change.from_dict(bot=bot, data=change))
|
2024-05-06 16:34:08 -04:00
|
|
|
|
2024-05-24 03:57:54 -04:00
|
|
|
if result[15] is not None:
|
|
|
|
metadata = json.loads(result[15])
|
2024-05-24 04:01:15 -04:00
|
|
|
else:
|
|
|
|
metadata = {}
|
2024-05-24 03:57:54 -04:00
|
|
|
|
2024-07-06 12:03:49 -04:00
|
|
|
moderation_type = str.lower(result[2])
|
|
|
|
if moderation_type in type_registry:
|
|
|
|
moderation_type = type_registry[moderation_type]
|
2024-07-06 13:12:47 -04:00
|
|
|
else:
|
|
|
|
logger.error("Unknown moderation type in case %s: %s", result[0], result[2])
|
2024-07-06 12:03:49 -04:00
|
|
|
|
2024-05-06 16:34:08 -04:00
|
|
|
case = {
|
|
|
|
"moderation_id": int(result[0]),
|
|
|
|
"guild_id": int(guild_id),
|
|
|
|
"timestamp": datetime.fromtimestamp(result[1]),
|
2024-07-06 12:03:49 -04:00
|
|
|
"moderation_type": moderation_type,
|
2024-05-06 16:34:08 -04:00
|
|
|
"target_type": str(result[3]),
|
|
|
|
"target_id": int(result[4]),
|
|
|
|
"moderator_id": int(result[5]),
|
|
|
|
"role_id": int(result[6]) if result[6] is not None else None,
|
|
|
|
"duration": duration,
|
|
|
|
"end_timestamp": datetime.fromtimestamp(result[8]) if result[8] is not None else None,
|
|
|
|
"reason": result[9],
|
|
|
|
"resolved": bool(result[10]),
|
|
|
|
"resolved_by": result[11],
|
|
|
|
"resolve_reason": result[12],
|
|
|
|
"expired": bool(result[13]),
|
2024-05-24 04:01:15 -04:00
|
|
|
"changes": change_obj_list,
|
2024-05-24 04:04:21 -04:00
|
|
|
"metadata": metadata if metadata else {},
|
2024-05-06 16:34:08 -04:00
|
|
|
}
|
2024-07-12 15:22:24 -04:00
|
|
|
return await cls.from_dict(bot=bot, data=case)
|
2024-05-06 16:34:08 -04:00
|
|
|
|
2024-06-08 20:12:22 -04:00
|
|
|
@staticmethod
|
|
|
|
async def connect() -> 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 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
|
|
|
|
|
2024-05-04 13:47:07 -04:00
|
|
|
@classmethod
|
2024-06-08 20:12:22 -04:00
|
|
|
async def execute(cls, query: str, parameters: tuple | None = None, bot: Red | None = None, guild_id: int | None = None, cursor: Cursor | None = None, return_obj: bool = True) -> Union[Tuple["Moderation"], Iterable[Row]]:
|
2024-07-12 15:22:24 -04:00
|
|
|
"""Executes a query on the database.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
query (str): The query to execute.
|
|
|
|
parameters (tuple): The parameters to pass to the query.
|
|
|
|
bot (Red): The bot instance.
|
|
|
|
guild_id (int): The ID of the guild to execute the query on.
|
|
|
|
cursor (Cursor): The cursor to use for the query.
|
|
|
|
return_obj (bool): Whether to return the case object(s). Defaults to `True`. If `False`, returns a `Iterable` of `aiosqlite.Row` objects.
|
|
|
|
Returns: The result of the query, either as a `Tuple` of `Moderation` objects or an `Iterable` of `aiosqlite.Row` objects.
|
|
|
|
"""
|
2024-06-08 20:12:22 -04:00
|
|
|
logger.trace("Executing query: \"%s\" with parameters \"%s\"", query, parameters)
|
2024-06-03 00:47:56 -04:00
|
|
|
if not parameters:
|
|
|
|
parameters = ()
|
2024-06-03 23:48:21 -04:00
|
|
|
if not cursor:
|
|
|
|
no_cursor = True
|
2024-06-08 20:12:22 -04:00
|
|
|
database = await cls.connect()
|
2024-06-05 00:14:43 -04:00
|
|
|
cursor = await database.cursor()
|
2024-06-03 23:48:21 -04:00
|
|
|
else:
|
|
|
|
no_cursor = False
|
2024-06-02 23:53:19 -04:00
|
|
|
|
2024-08-10 13:32:37 -04:00
|
|
|
try:
|
|
|
|
await cursor.execute(query, parameters)
|
|
|
|
except OperationalError as e:
|
|
|
|
logger.error("Error executing query: \"%s\" with parameters \"%s\"\nError:\n%s",
|
|
|
|
query, parameters, e)
|
|
|
|
raise OperationalError(f"Error executing query: \"{query}\" with parameters \"{parameters}\"") from e
|
2024-06-05 00:14:43 -04:00
|
|
|
results = await cursor.fetchall()
|
2024-06-08 20:12:22 -04:00
|
|
|
await database.commit()
|
2024-06-03 23:48:21 -04:00
|
|
|
if no_cursor:
|
2024-06-05 00:14:43 -04:00
|
|
|
await cursor.close()
|
|
|
|
await database.close()
|
2024-06-03 23:48:21 -04:00
|
|
|
|
2024-06-08 20:12:22 -04:00
|
|
|
if results and return_obj and bot and guild_id:
|
2024-06-03 23:48:21 -04:00
|
|
|
cases = []
|
|
|
|
for result in results:
|
2024-07-06 13:15:06 -04:00
|
|
|
if result[0] == 0:
|
|
|
|
continue
|
2024-07-12 15:22:24 -04:00
|
|
|
case = await cls.from_result(bot=bot, result=result, guild_id=guild_id)
|
2024-07-06 13:15:06 -04:00
|
|
|
cases.append(case)
|
2024-06-03 23:48:21 -04:00
|
|
|
return tuple(cases)
|
2024-06-08 20:12:22 -04:00
|
|
|
return results
|
2024-06-02 23:53:19 -04:00
|
|
|
|
2024-06-03 00:47:56 -04:00
|
|
|
@classmethod
|
2024-08-13 14:25:03 -04:00
|
|
|
async def get_latest(cls, bot: Red, guild_id: int, before: datetime | None = None, after: datetime | None = None, limit: int | None = None, offset: int = 0, types: Iterable[Type] | None = None, expired: bool | None = None, cursor: Cursor | None = None) -> Tuple["Moderation"]:
|
2024-06-04 12:29:24 -04:00
|
|
|
params = []
|
2024-08-10 13:44:18 -04:00
|
|
|
query = f"SELECT * FROM moderation_{guild_id}"
|
2024-08-10 13:42:15 -04:00
|
|
|
conditions = []
|
|
|
|
|
2024-06-04 00:04:11 -04:00
|
|
|
if types:
|
2024-08-10 13:42:15 -04:00
|
|
|
conditions.append(f"moderation_type IN ({', '.join(['?' for _ in types])})")
|
2024-08-13 16:31:00 -04:00
|
|
|
params.extend([t.key for t in types])
|
2024-08-10 13:17:16 -04:00
|
|
|
if before:
|
2024-08-10 13:42:15 -04:00
|
|
|
conditions.append("timestamp < ?")
|
2024-08-10 13:34:15 -04:00
|
|
|
params.append(int(before.timestamp()))
|
2024-08-10 13:17:16 -04:00
|
|
|
if after:
|
2024-08-10 13:42:15 -04:00
|
|
|
conditions.append("timestamp > ?")
|
2024-08-10 13:34:15 -04:00
|
|
|
params.append(int(after.timestamp()))
|
2024-08-13 14:25:03 -04:00
|
|
|
if expired is not None:
|
|
|
|
conditions.append("expired = ?")
|
|
|
|
params.append(int(expired))
|
2024-08-10 13:42:15 -04:00
|
|
|
|
|
|
|
if conditions:
|
|
|
|
query += " WHERE " + " AND ".join(conditions)
|
|
|
|
|
2024-08-12 18:50:07 -04:00
|
|
|
query += " ORDER BY moderation_id DESC"
|
|
|
|
|
2024-06-04 12:29:24 -04:00
|
|
|
if limit:
|
|
|
|
query += " LIMIT ? OFFSET ?"
|
|
|
|
params.extend((limit, offset))
|
2024-08-12 18:50:07 -04:00
|
|
|
query += ";"
|
2024-06-05 00:14:43 -04:00
|
|
|
return await cls.execute(bot=bot, guild_id=guild_id, query=query, parameters=tuple(params) if params else (), cursor=cursor)
|
2024-06-03 00:47:56 -04:00
|
|
|
|
|
|
|
@classmethod
|
2024-06-05 00:14:43 -04:00
|
|
|
async def get_next_case_number(cls, bot: Red, guild_id: int, cursor: Cursor | None = None) -> int:
|
|
|
|
result = await cls.get_latest(bot=bot, guild_id=guild_id, cursor=cursor, limit=1)
|
2024-06-04 00:04:11 -04:00
|
|
|
return (result[0].moderation_id + 1) if result else 1
|
|
|
|
|
|
|
|
@classmethod
|
2024-06-05 00:14:43 -04:00
|
|
|
async def find_by_id(cls, bot: Red, moderation_id: int, guild_id: int, cursor: Cursor | None = None) -> "Moderation":
|
2024-06-03 00:47:56 -04:00
|
|
|
query = f"SELECT * FROM moderation_{guild_id} WHERE moderation_id = ?;"
|
2024-06-05 00:14:43 -04:00
|
|
|
case = await cls.execute(bot=bot, guild_id=guild_id, query=query, parameters=(moderation_id,), cursor=cursor)
|
2024-06-03 00:47:56 -04:00
|
|
|
if case:
|
|
|
|
return case[0]
|
|
|
|
raise ValueError(f"Case {moderation_id} not found in moderation_{guild_id}!")
|
|
|
|
|
|
|
|
@classmethod
|
2024-08-13 14:25:03 -04:00
|
|
|
async def find_by_target(cls, bot: Red, guild_id: int, target: int, before: datetime = None, after: datetime = None, types: Iterable[Type] | None = None, expired: bool | None = None, cursor: Cursor | None = None) -> Tuple["Moderation"]:
|
2024-06-03 00:47:56 -04:00
|
|
|
query = f"SELECT * FROM moderation_{guild_id} WHERE target_id = ?"
|
2024-08-10 13:17:16 -04:00
|
|
|
params = [target]
|
2024-06-03 00:47:56 -04:00
|
|
|
if types:
|
|
|
|
query += f" AND moderation_type IN ({', '.join(['?' for _ in types])})"
|
2024-08-10 13:17:16 -04:00
|
|
|
for t in types:
|
|
|
|
params.append(t.key)
|
|
|
|
if before:
|
|
|
|
query += " AND timestamp < ?"
|
2024-08-10 13:34:15 -04:00
|
|
|
params.append(int(before.timestamp()))
|
2024-08-10 13:17:16 -04:00
|
|
|
if after:
|
|
|
|
query += " AND timestamp > ?"
|
2024-08-10 13:34:15 -04:00
|
|
|
params.append(int(after.timestamp()))
|
2024-08-13 14:25:03 -04:00
|
|
|
if expired is not None:
|
|
|
|
query += " AND expired = ?"
|
|
|
|
params.append(int(expired))
|
|
|
|
|
2024-06-03 00:47:56 -04:00
|
|
|
query += " ORDER BY moderation_id DESC;"
|
|
|
|
|
2024-08-10 13:17:16 -04:00
|
|
|
return await cls.execute(bot=bot, guild_id=guild_id, query=query, parameters=params, cursor=cursor)
|
2024-06-03 00:47:56 -04:00
|
|
|
|
|
|
|
@classmethod
|
2024-08-13 14:25:03 -04:00
|
|
|
async def find_by_moderator(cls, bot: Red, guild_id: int, moderator: int, before: datetime = None, after: datetime = None, types: Iterable[Type] | None = None, expired: bool | None = None, cursor: Cursor | None = None) -> Tuple["Moderation"]:
|
2024-06-03 00:47:56 -04:00
|
|
|
query = f"SELECT * FROM moderation_{guild_id} WHERE moderator_id = ?"
|
2024-08-10 13:17:16 -04:00
|
|
|
params = [moderator]
|
2024-06-03 00:47:56 -04:00
|
|
|
if types:
|
|
|
|
query += f" AND moderation_type IN ({', '.join(['?' for _ in types])})"
|
2024-08-10 13:17:16 -04:00
|
|
|
for t in types:
|
|
|
|
params.append(t.key)
|
|
|
|
if before:
|
|
|
|
query += " AND timestamp < ?"
|
2024-08-10 13:34:15 -04:00
|
|
|
params.append(int(before.timestamp()))
|
2024-08-10 13:17:16 -04:00
|
|
|
if after:
|
|
|
|
query += " AND timestamp > ?"
|
2024-08-10 13:34:15 -04:00
|
|
|
params.append(int(after.timestamp()))
|
2024-08-13 14:25:03 -04:00
|
|
|
if expired is not None:
|
|
|
|
query += " AND expired = ?"
|
|
|
|
params.append(int(expired))
|
|
|
|
|
2024-06-03 00:47:56 -04:00
|
|
|
query += " ORDER BY moderation_id DESC;"
|
|
|
|
|
2024-08-10 13:17:16 -04:00
|
|
|
return await cls.execute(bot=bot, guild_id=guild_id, query=query, parameters=params, cursor=cursor)
|
2024-06-03 00:47:56 -04:00
|
|
|
|
2024-05-06 14:15:05 -04:00
|
|
|
@classmethod
|
2024-06-05 00:14:43 -04:00
|
|
|
async def log(
|
2024-05-06 14:15:05 -04:00
|
|
|
cls,
|
|
|
|
bot: Red,
|
|
|
|
guild_id: int,
|
2024-05-06 14:40:14 -04:00
|
|
|
moderator_id: int,
|
2024-07-06 12:23:06 -04:00
|
|
|
moderation_type: Type,
|
2024-05-06 14:15:05 -04:00
|
|
|
target_type: str,
|
|
|
|
target_id: int,
|
2024-05-24 03:46:20 -04:00
|
|
|
role_id: int | None = None,
|
2024-05-06 21:04:08 -04:00
|
|
|
duration: timedelta | None = None,
|
|
|
|
reason: str | None = None,
|
|
|
|
database: sqlite3.Connection | None = None,
|
|
|
|
timestamp: datetime | None = None,
|
2024-05-06 14:15:05 -04:00
|
|
|
resolved: bool = False,
|
2024-05-06 21:04:08 -04:00
|
|
|
resolved_by: int | None = None,
|
|
|
|
resolved_reason: str | None = None,
|
|
|
|
expired: bool | None = None,
|
|
|
|
changes: list | None = None,
|
|
|
|
metadata: dict | None = None,
|
2024-06-04 16:35:18 -04:00
|
|
|
return_obj: bool = True,
|
2024-06-04 23:55:55 -04:00
|
|
|
) -> Union["Moderation", int]:
|
2024-07-12 15:22:24 -04:00
|
|
|
"""Logs a moderation case in the database.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
bot (Red): The bot instance.
|
|
|
|
guild_id (int): The ID of the guild to log the case in.
|
|
|
|
moderator_id (int): The ID of the moderator who issued the case.
|
|
|
|
moderation_type (Type): The type of moderation case. See `aurora.models.moderation_types` for the built-in options.
|
|
|
|
target_type (str): The type of target. Should be either `user` or `channel`.
|
|
|
|
target_id (int): The ID of the target.
|
|
|
|
role_id (int): The ID of the role, if applicable.
|
|
|
|
duration (timedelta): The duration of the case, if applicable.
|
|
|
|
reason (str): The reason for the case.
|
|
|
|
database (sqlite3.Connection): The database connection to use to log the case. A connection will be automatically created if not provided.
|
|
|
|
timestamp (datetime): The timestamp of the case. Will be automatically generated if not provided.
|
|
|
|
resolved (bool): Whether the case is resolved.
|
|
|
|
resolved_by (int): The ID of the user who resolved the case.
|
|
|
|
resolved_reason (str): The reason the case was resolved.
|
|
|
|
expired (bool): Whether the case is expired.
|
|
|
|
changes (list): A list of changes to log. You usually shouldn't pass this, as it's automatically generated by the `/edit` and `/resolve` commands.
|
|
|
|
metadata (dict): A dictionary of metadata to store with the case.
|
|
|
|
return_obj (bool): Whether to return the case object. Defaults to `True`. If `False`, returns the case ID.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Union[Moderation, int]: The `Moderation` object if `return_obj` is `True`, otherwise the case ID.
|
|
|
|
"""
|
2024-05-06 21:46:01 -04:00
|
|
|
from ..utilities.json import dumps
|
2024-05-06 14:15:05 -04:00
|
|
|
if not timestamp:
|
|
|
|
timestamp = datetime.fromtimestamp(time())
|
|
|
|
elif not isinstance(timestamp, datetime):
|
|
|
|
timestamp = datetime.fromtimestamp(timestamp)
|
|
|
|
|
2024-05-09 21:27:26 -04:00
|
|
|
if duration == "NULL":
|
|
|
|
duration = None
|
|
|
|
|
|
|
|
if duration is not None:
|
2024-05-06 14:44:06 -04:00
|
|
|
end_timestamp = timestamp + duration
|
2024-05-06 14:15:05 -04:00
|
|
|
else:
|
|
|
|
duration = None
|
|
|
|
end_timestamp = None
|
|
|
|
|
|
|
|
if not expired:
|
2024-05-24 03:51:43 -04:00
|
|
|
if end_timestamp:
|
|
|
|
expired = bool(timestamp > end_timestamp)
|
|
|
|
else:
|
|
|
|
expired = False
|
2024-05-06 14:15:05 -04:00
|
|
|
|
|
|
|
if reason == "NULL":
|
|
|
|
reason = None
|
|
|
|
|
2024-06-04 23:55:55 -04:00
|
|
|
if resolved_by in ["NULL", "?"]:
|
2024-05-06 14:15:05 -04:00
|
|
|
resolved_by = None
|
|
|
|
|
|
|
|
if resolved_reason == "NULL":
|
|
|
|
resolved_reason = None
|
|
|
|
|
|
|
|
if role_id == 0:
|
|
|
|
role_id = None
|
|
|
|
|
|
|
|
if not database:
|
2024-06-08 20:12:22 -04:00
|
|
|
database = await cls.connect()
|
2024-05-06 14:15:05 -04:00
|
|
|
close_db = True
|
|
|
|
else:
|
|
|
|
close_db = False
|
|
|
|
|
2024-06-05 00:41:41 -04:00
|
|
|
moderation_id = await cls.get_next_case_number(bot=bot, guild_id=guild_id)
|
2024-05-06 14:15:05 -04:00
|
|
|
|
|
|
|
case = {
|
|
|
|
"moderation_id": moderation_id,
|
2024-05-06 14:55:36 -04:00
|
|
|
"timestamp": timestamp.timestamp(),
|
2024-07-06 12:23:06 -04:00
|
|
|
"moderation_type": moderation_type.key,
|
2024-05-06 14:15:05 -04:00
|
|
|
"target_type": target_type,
|
|
|
|
"target_id": target_id,
|
2024-05-06 14:40:14 -04:00
|
|
|
"moderator_id": moderator_id,
|
2024-05-06 14:15:05 -04:00
|
|
|
"role_id": role_id,
|
2024-06-04 15:22:50 -04:00
|
|
|
"duration": timedelta_to_string(duration) if duration else None,
|
2024-05-06 14:55:36 -04:00
|
|
|
"end_timestamp": end_timestamp.timestamp() if end_timestamp else None,
|
2024-05-06 14:15:05 -04:00
|
|
|
"reason": reason,
|
|
|
|
"resolved": resolved,
|
|
|
|
"resolved_by": resolved_by,
|
|
|
|
"resolve_reason": resolved_reason,
|
|
|
|
"expired": expired,
|
2024-05-06 14:55:36 -04:00
|
|
|
"changes": dumps(changes),
|
|
|
|
"metadata": dumps(metadata)
|
2024-05-06 14:15:05 -04: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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.execute(sql, tuple(case.values()))
|
2024-05-06 14:15:05 -04:00
|
|
|
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.commit()
|
2024-05-06 14:15:05 -04:00
|
|
|
if close_db:
|
2024-06-05 00:14:43 -04:00
|
|
|
await database.close()
|
2024-05-06 14:15:05 -04:00
|
|
|
|
2024-06-05 01:12:49 -04:00
|
|
|
logger.verbose(
|
2024-05-06 14:15:05 -04:00
|
|
|
"Row inserted into moderation_%s!\n%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s",
|
2024-05-06 14:40:14 -04:00
|
|
|
guild_id,
|
2024-05-06 14:55:36 -04:00
|
|
|
case["moderation_id"],
|
|
|
|
case["timestamp"],
|
|
|
|
case["moderation_type"],
|
|
|
|
case["target_type"],
|
|
|
|
case["target_id"],
|
|
|
|
case["moderator_id"],
|
|
|
|
case["role_id"],
|
|
|
|
case["duration"],
|
|
|
|
case["end_timestamp"],
|
|
|
|
case["reason"],
|
|
|
|
case["resolved"],
|
|
|
|
case["resolved_by"],
|
|
|
|
case["resolve_reason"],
|
|
|
|
case["expired"],
|
|
|
|
case["changes"],
|
|
|
|
case["metadata"],
|
2024-05-06 14:15:05 -04:00
|
|
|
)
|
|
|
|
|
2024-06-04 16:35:18 -04:00
|
|
|
if return_obj:
|
2024-06-05 00:14:43 -04:00
|
|
|
return await cls.find_by_id(bot=bot, moderation_id=moderation_id, guild_id=guild_id)
|
2024-06-04 23:55:55 -04:00
|
|
|
return moderation_id
|