Source code for sidequest_common.schemas.moderation

 1"""Moderation case schemas."""
 2
 3from datetime import datetime  # pydantic needs it at runtime
 4from enum import StrEnum
 5from typing import Literal
 6
 7from pydantic import BaseModel, ConfigDict, Field, model_validator
 8
 9
[docs] 10class CaseAction(StrEnum): 11 """Kinds of moderation action recorded as cases.""" 12 13 WARN = "warn" 14 TIMEOUT = "timeout" 15 UNTIMEOUT = "untimeout" 16 KICK = "kick" 17 BAN = "ban" 18 UNBAN = "unban" 19 AUTOMOD = "automod" 20 REPORT = "report"
21 22
[docs] 23class ModCaseCreate(BaseModel): 24 """Body for ``POST /cases``; the API assigns ``case_number``. 25 26 A ``report`` needs ``reporter_id``; every other action needs ``user_id`` and ``moderator_id``. 27 """ 28 29 action: CaseAction 30 user_id: int | None = None 31 moderator_id: int | None = None 32 reporter_id: int | None = None 33 thread_id: int | None = None 34 report_number: int | None = Field(default=None, ge=1) 35 reason: str | None = Field(default=None, max_length=1000) 36 duration_seconds: int | None = Field(default=None, ge=0) 37 38 @model_validator(mode="after") 39 def _validate_required_fields(self) -> ModCaseCreate: 40 if self.action == CaseAction.REPORT: 41 if self.reporter_id is None: 42 msg = "A report needs reporter_id" 43 raise ValueError(msg) 44 elif self.user_id is None or self.moderator_id is None: 45 msg = "user_id and moderator_id are required for this action" 46 raise ValueError(msg) 47 return self
48 49
[docs] 50class ModCaseUpdate(BaseModel): 51 """Body for ``PATCH /cases/{case_number}``. 52 53 ``status``, ``resolution``, and ``thread_id`` only apply to report cases; setting them on any other case is a 54 400 from the API (it knows the case's action, so that check lives there, not here). 55 """ 56 57 reason: str | None = Field(default=None, max_length=1000) 58 moderator_id: int | None = None 59 status: Literal["open", "closed"] | None = None 60 resolution: str | None = Field(default=None, max_length=1000) 61 thread_id: int | None = None
62 63
[docs] 64class ModCaseSchema(BaseModel): 65 """A recorded case.""" 66 67 model_config = ConfigDict(from_attributes=True) 68 69 guild_id: int 70 case_number: int 71 action: CaseAction 72 user_id: int | None = None 73 moderator_id: int | None = None 74 reason: str | None 75 duration_seconds: int | None 76 created_at: datetime 77 reporter_id: int | None = None 78 status: Literal["open", "closed"] | None = None 79 thread_id: int | None = None 80 resolution: str | None = None 81 report_number: int | None = None