case ¶
Moderation case management controller.
This controller manages moderation cases (bans, kicks, timeouts, etc.) with automatic case numbering, status tracking, and audit logging for Discord guilds.
Classes:
-
CaseController–Clean Case controller using the new BaseController pattern.
Classes¶
CaseController ¶
CaseController(db: DatabaseService | None = None)
Bases: BaseController[Case]
Clean Case controller using the new BaseController pattern.
Initialize the case controller.
Parameters:
-
db(DatabaseService | None, default:None) –The database service instance. If None, uses the default service.
Methods:
-
get_case_by_id–Get a case by its ID.
-
get_cases_by_user–Get all cases for a specific user in a guild.
-
get_active_cases_by_user–Get all active cases for a specific user in a guild.
-
create_case–Create a new case with auto-generated case number.
-
create–Create a new record.
-
get_by_id–Get a record by ID.
-
update_by_id–Update a record by ID.
-
update_case–Update a case by ID.
-
delete_by_id–Delete a record by ID.
-
update_mod_log_message_id–Update the mod log message ID for a case.
-
exists–Check if a record exists.
-
close_case–Close a case by setting its status to False.
-
find_one–Find one record.
-
delete_case–Delete a case by ID.
-
find_all–Find all records with performance optimizations.
-
get_cases_by_guild–Get all cases for a guild, optionally limited.
-
get_cases_by_type–Get all cases of a specific type in a guild.
-
find_all_with_options–Find all records with relationship loading options.
-
get_recent_cases–Get cases created within the last N hours.
-
count–Count records.
-
get_case_count_by_guild–Get the total number of cases in a guild.
-
get_all–Get all records (alias for find_all without pagination).
-
is_user_under_restriction–Check if a user is under any active restriction in a guild.
-
execute_query–Execute a custom query.
-
find_with_json_query–Find records using JSON column queries.
-
get_case_by_number–Get a case by its case number in a guild.
-
find_with_array_contains–Find records where array column contains value.
-
get_cases_by_options–Get cases by various filter options.
-
find_with_full_text_search–Find records using full-text search.
-
update_case_by_number–Update a case by guild ID and case number.
-
paginate–Paginate records with metadata.
-
get_all_cases–Get all cases in a guild.
-
find_paginated–Find paginated records with relationship loading.
-
get_latest_case_by_user–Get the most recent case for a user in a guild.
-
bulk_create–Create multiple records in bulk.
-
set_tempban_expired–Mark a tempban case as processed after the user has been unbanned.
-
bulk_update–Update multiple records in bulk.
-
bulk_delete–Delete multiple records in bulk.
-
get_expired_tempbans–Get tempban cases that have expired but haven't been processed yet.
-
update_where–Update records matching filters.
-
delete_where–Delete records matching filters.
-
bulk_upsert_with_conflict_resolution–Bulk upsert with conflict resolution.
-
get_case_count_by_user–Get the total number of cases for a specific user in a guild.
-
with_session–Execute operation within a session context.
-
get_cases_by_moderator–Get all cases moderated by a specific user in a guild.
-
with_transaction–Execute operation within a transaction context.
-
get_expired_cases–Get all expired cases (any type) that haven't been processed yet.
-
execute_transaction–Execute a callback within a transaction.
-
get_table_statistics–Get comprehensive table statistics.
-
explain_query_performance–Explain query performance with optional analysis.
-
upsert_by_field–Upsert a record by a specific field.
-
upsert_by_id–Upsert a record by ID.
-
get_or_create_by_field–Get existing record or create new one by field.
-
get_or_create–Get existing record or create new one.
-
upsert–Upsert a record.
Attributes:
-
db_service(DatabaseService) –Database service property for test compatibility.
-
model_class(type[ModelT]) –Model class property for test compatibility.
Attributes¶
db_service property ¶
db_service: DatabaseService
Database service property for test compatibility.
Returns:
-
DatabaseService–The database service instance.
Functions¶
get_case_by_id async ¶
get_cases_by_user async ¶
get_active_cases_by_user async ¶
create_case async ¶
create_case(
case_type: str,
case_user_id: int,
case_moderator_id: int,
guild_id: int,
case_reason: str | None = None,
case_status: bool = True,
**kwargs: Any,
) -> Case
Create a new case with auto-generated case number.
Uses SELECT FOR UPDATE to prevent race conditions when generating case numbers.
Parameters:
-
case_type(str) –The type of case (from CaseType enum value)
-
case_user_id(int) –Discord ID of the user being moderated
-
case_moderator_id(int) –Discord ID of the moderator
-
guild_id(int) –Discord guild ID
-
case_reason(str | None, default:None) –Reason for the moderation action
-
case_status(bool, default:True) –Whether the case is active (default True)
-
**kwargs(Any, default:{}) –Additional case fields (e.g., case_expires_at, case_metadata, mod_log_message_id)
Returns:
-
Case–The newly created case with auto-generated case number.
Notes
- For expiring cases, use
case_expires_at(datetime) in kwargs - Do NOT pass
duration- convert tocase_expires_atbefore calling this method - Case numbers are auto-generated per guild using SELECT FOR UPDATE locking
_get_pagination ¶
_get_pagination() -> PaginationController[ModelT]
Get or create pagination controller.
Returns:
-
PaginationController[ModelT]–The pagination controller instance.
_get_bulk ¶
_get_bulk() -> BulkOperationsController[ModelT]
Get or create bulk operations controller.
Returns:
-
BulkOperationsController[ModelT]–The bulk operations controller instance.
_get_transaction ¶
_get_transaction() -> TransactionController[ModelT]
Get or create transaction controller.
Returns:
-
TransactionController[ModelT]–The transaction controller instance.
_get_performance ¶
_get_performance() -> PerformanceController[ModelT]
Get or create performance controller.
Returns:
-
PerformanceController[ModelT]–The performance controller instance.
_get_upsert ¶
_get_upsert() -> UpsertController[ModelT]
Get or create upsert controller.
Returns:
-
UpsertController[ModelT]–The upsert controller instance.
create async ¶
create(**kwargs: Any) -> ModelT
Create a new record.
Returns:
-
ModelT–The newly created record.
get_by_id async ¶
get_by_id(record_id: Any) -> ModelT | None
Get a record by ID.
Returns:
-
ModelT | None–The record if found, None otherwise.
update_by_id async ¶
Update a record by ID.
Returns:
-
ModelT | None–The updated record, or None if not found.
update_case async ¶
delete_by_id async ¶
update_mod_log_message_id async ¶
exists async ¶
close_case async ¶
Close a case by setting its status to False.
Returns:
-
Case | None–The updated case, or None if not found.
find_one async ¶
Find one record.
Returns:
-
ModelT | None–The found record, or None if not found.
delete_case async ¶
find_all async ¶
get_cases_by_guild async ¶
get_cases_by_type async ¶
find_all_with_options async ¶
find_all_with_options(
filters: Any | None = None,
order_by: Any | None = None,
limit: int | None = None,
offset: int | None = None,
load_relationships: list[str] | None = None,
) -> list[ModelT]
Find all records with relationship loading options.
Returns:
-
list[ModelT]–List of found records with loaded relationships.
get_recent_cases async ¶
count async ¶
get_case_count_by_guild async ¶
get_all async ¶
Get all records (alias for find_all without pagination).
Returns:
-
list[ModelT]–List of all matching records.
is_user_under_restriction async ¶
is_user_under_restriction(
user_id: int | None = None, guild_id: int | None = None, **kwargs: Any
) -> bool
Check if a user is under any active restriction in a guild.
Returns:
-
bool–True if user is under restriction, False otherwise.
execute_query async ¶
find_with_json_query async ¶
find_with_json_query(
json_column: str, json_path: str, value: Any, filters: Any | None = None
) -> list[ModelT]
Find records using JSON column queries.
Returns:
-
list[ModelT]–List of records matching the JSON query.
get_case_by_number async ¶
find_with_array_contains async ¶
find_with_array_contains(
array_column: str, value: Any, filters: Any | None = None
) -> list[ModelT]
Find records where array column contains value.
Returns:
-
list[ModelT]–List of records with matching array values.
get_cases_by_options async ¶
find_with_full_text_search async ¶
find_with_full_text_search(
search_columns: list[str], search_term: str, filters: Any | None = None
) -> list[ModelT]
Find records using full-text search.
Returns:
-
list[ModelT]–List of records matching the search term.
update_case_by_number async ¶
Update a case by guild ID and case number.
Returns:
-
Case | None–The updated case, or None if not found.
paginate async ¶
paginate(
page: int = 1,
per_page: int = 20,
filters: Any | None = None,
order_by: Any | None = None,
) -> PaginationResult[ModelT]
Paginate records with metadata.
Returns:
-
PaginationResult[ModelT]–Pagination result with items, total, and page info.
get_all_cases async ¶
find_paginated async ¶
find_paginated(
page: int = 1,
per_page: int = 20,
filters: Any | None = None,
order_by: Any | None = None,
load_relationships: list[str] | None = None,
) -> PaginationResult[ModelT]
Find paginated records with relationship loading.
Returns:
-
PaginationResult[ModelT]–Pagination result with items and relationships loaded.
get_latest_case_by_user async ¶
Get the most recent case for a user in a guild.
Returns:
-
Case | None–The most recent case if found, None otherwise.
bulk_create async ¶
set_tempban_expired async ¶
Mark a tempban case as processed after the user has been unbanned.
This sets case_processed=True to indicate the expiration has been handled. The case_status remains True (the case is still valid, just completed). The case_expires_at field remains unchanged as a historical record.
Parameters:
-
case_id(int) –The ID of the case to mark as processed
-
guild_id(int | None, default:None) –Deprecated parameter kept for backward compatibility (unused)
Returns:
-
bool–True if the case was updated, False if not found
bulk_update async ¶
bulk_delete async ¶
get_expired_tempbans async ¶
update_where async ¶
delete_where async ¶
bulk_upsert_with_conflict_resolution async ¶
get_case_count_by_user async ¶
Get the total number of cases for a specific user in a guild.
Returns:
-
int–The total count of cases for the user.
with_session async ¶
Execute operation within a session context.
Returns:
-
R–The result of the operation.
get_cases_by_moderator async ¶
with_transaction async ¶
Execute operation within a transaction context.
Returns:
-
R–The result of the operation.