WIP: Refactor Aurora (3.0.0) #29
2 changed files with 17 additions and 10 deletions
|
@ -11,13 +11,20 @@ from aurora.utilities.utils import generate_dict
|
|||
class AuroraBaseModel(BaseModel):
|
||||
"""Base class for all models in Aurora."""
|
||||
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
|
||||
|
||||
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", "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
|
||||
timestamp: datetime
|
||||
moderation_type: str
|
||||
|
@ -101,8 +108,8 @@ class Change(AuroraBaseModel):
|
|||
return await PartialUser.from_id(self.bot, self.user_id)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, bot: Red, guild_id: int, data: dict) -> "Change":
|
||||
return cls(bot=bot, guild_id=guild_id, **data)
|
||||
def from_dict(cls, bot: Red, data: dict) -> "Change":
|
||||
return cls(bot=bot **data)
|
||||
|
||||
class PartialUser(AuroraBaseModel):
|
||||
id: int
|
||||
|
@ -122,12 +129,12 @@ class PartialUser(AuroraBaseModel):
|
|||
if not user:
|
||||
try:
|
||||
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:
|
||||
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
|
||||
name: str
|
||||
|
||||
|
@ -149,10 +156,10 @@ class PartialChannel(AuroraBaseModel):
|
|||
return cls(bot=bot, guild_id=channel.guild.id, id=channel.id, username=channel.name)
|
||||
except (NotFound, InvalidData, HTTPException, Forbidden) as e:
|
||||
if e == Forbidden:
|
||||
return cls(bot=bot, guild_id=0, 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="Forbidden Channel")
|
||||
return cls(bot=bot, guild_id=None, id=channel_id, name="Deleted Channel")
|
||||
|
||||
class PartialRole(AuroraBaseModel):
|
||||
class PartialRole(AuroraGuildModel):
|
||||
id: int
|
||||
name: str
|
||||
|
||||
|
|
|
@ -137,7 +137,7 @@ def generate_dict(bot: Red, result: dict, guild_id: int) -> dict:
|
|||
changes = json.loads(result[14].strip('"').replace('\\"', '"'))
|
||||
change_obj_list = []
|
||||
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 = {
|
||||
"moderation_id": int(result[0]),
|
||||
|
|
Loading…
Reference in a new issue