WIP: Moderation type registry #26
2 changed files with 17 additions and 10 deletions
|
@ -11,13 +11,20 @@ from aurora.utilities.utils import generate_dict
|
||||||
class AuroraBaseModel(BaseModel):
|
class AuroraBaseModel(BaseModel):
|
||||||
"""Base class for all models in Aurora."""
|
"""Base class for all models in Aurora."""
|
||||||
bot: ClassVar[Red]
|
bot: ClassVar[Red]
|
||||||
|
|
||||||
|
def to_json(self, indent: int = None, file: Any = None, **kwargs):
|
||||||
|
from aurora.utilities.json import dump, dumps
|
||||||
|
return dump(self.model_dump(exclude={"bot"}), file, indent=indent, **kwargs) if file else dumps(self.model_dump(exclude={"bot"}), indent=indent, **kwargs)
|
||||||
|
|
||||||
|
class AuroraGuildModel(AuroraBaseModel):
|
||||||
|
"""Subclass of AuroraBaseModel that includes a guild_id attribute, and a modified to_json() method to match."""
|
||||||
guild_id: int
|
guild_id: int
|
||||||
|
|
||||||
def to_json(self, indent: int = None, file: Any = None, **kwargs):
|
def to_json(self, indent: int = None, file: Any = None, **kwargs):
|
||||||
from aurora.utilities.json import dump, dumps
|
from aurora.utilities.json import dump, dumps
|
||||||
return dump(self.model_dump(exclude={"bot", "guild_id"}), file, indent=indent, **kwargs) if file else dumps(self.model_dump(exclude={"bot", "guild_id"}), indent=indent, **kwargs)
|
return dump(self.model_dump(exclude={"bot", "guild_id"}), file, indent=indent, **kwargs) if file else dumps(self.model_dump(exclude={"bot", "guild_id"}), indent=indent, **kwargs)
|
||||||
|
|
||||||
class Moderation(AuroraBaseModel):
|
class Moderation(AuroraGuildModel):
|
||||||
moderation_id: int
|
moderation_id: int
|
||||||
timestamp: datetime
|
timestamp: datetime
|
||||||
moderation_type: str
|
moderation_type: str
|
||||||
|
@ -101,8 +108,8 @@ class Change(AuroraBaseModel):
|
||||||
return await PartialUser.from_id(self.bot, self.user_id)
|
return await PartialUser.from_id(self.bot, self.user_id)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_dict(cls, bot: Red, guild_id: int, data: dict) -> "Change":
|
def from_dict(cls, bot: Red, data: dict) -> "Change":
|
||||||
return cls(bot=bot, guild_id=guild_id, **data)
|
return cls(bot=bot **data)
|
||||||
|
|
||||||
class PartialUser(AuroraBaseModel):
|
class PartialUser(AuroraBaseModel):
|
||||||
id: int
|
id: int
|
||||||
|
@ -122,12 +129,12 @@ class PartialUser(AuroraBaseModel):
|
||||||
if not user:
|
if not user:
|
||||||
try:
|
try:
|
||||||
user = await bot.fetch_user(user_id)
|
user = await bot.fetch_user(user_id)
|
||||||
return cls(bot=bot, guild_id=0, id=user.id, username=user.name, discriminator=user.discriminator)
|
return cls(bot=bot, id=user.id, username=user.name, discriminator=user.discriminator)
|
||||||
except NotFound:
|
except NotFound:
|
||||||
return cls(bot=bot, guild_id=0, id=user_id, username="Deleted User", discriminator=0)
|
return cls(bot=bot, id=user_id, username="Deleted User", discriminator=0)
|
||||||
|
|
||||||
|
|
||||||
class PartialChannel(AuroraBaseModel):
|
class PartialChannel(AuroraGuildModel):
|
||||||
id: int
|
id: int
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
@ -149,10 +156,10 @@ class PartialChannel(AuroraBaseModel):
|
||||||
return cls(bot=bot, guild_id=channel.guild.id, id=channel.id, username=channel.name)
|
return cls(bot=bot, guild_id=channel.guild.id, id=channel.id, username=channel.name)
|
||||||
except (NotFound, InvalidData, HTTPException, Forbidden) as e:
|
except (NotFound, InvalidData, HTTPException, Forbidden) as e:
|
||||||
if e == Forbidden:
|
if e == Forbidden:
|
||||||
return cls(bot=bot, guild_id=0, id=channel_id, name="Forbidden Channel")
|
return cls(bot=bot, guild_id=None, id=channel_id, name="Forbidden Channel")
|
||||||
return cls(bot=bot, guild_id=0, id=channel_id, name="Deleted Channel")
|
return cls(bot=bot, guild_id=None, id=channel_id, name="Deleted Channel")
|
||||||
|
|
||||||
class PartialRole(AuroraBaseModel):
|
class PartialRole(AuroraGuildModel):
|
||||||
id: int
|
id: int
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
|
|
@ -137,7 +137,7 @@ def generate_dict(bot: Red, result: dict, guild_id: int) -> dict:
|
||||||
changes = json.loads(result[14].strip('"').replace('\\"', '"'))
|
changes = json.loads(result[14].strip('"').replace('\\"', '"'))
|
||||||
change_obj_list = []
|
change_obj_list = []
|
||||||
for change in changes:
|
for change in changes:
|
||||||
change_obj_list.append(Change.from_dict(bot=bot, guild_id=guild_id, data=change))
|
change_obj_list.append(Change.from_dict(bot=bot, data=change))
|
||||||
|
|
||||||
case = {
|
case = {
|
||||||
"moderation_id": int(result[0]),
|
"moderation_id": int(result[0]),
|
||||||
|
|
Loading…
Reference in a new issue