From fa27d12de5bcc324aaaa5075e23466a10651fb3f Mon Sep 17 00:00:00 2001 From: cswimr Date: Thu, 22 Aug 2024 15:33:36 -0400 Subject: [PATCH] feat(aurora): make all models compatible with `repr` --- aurora/models/base.py | 3 +++ aurora/models/change.py | 12 ++++++++++++ aurora/models/moderation.py | 23 +++++++++++++++++++++++ aurora/models/partials.py | 9 +++++++++ aurora/models/type.py | 8 ++++++++ 5 files changed, 55 insertions(+) diff --git a/aurora/models/base.py b/aurora/models/base.py index 43c9d89..d94b635 100644 --- a/aurora/models/base.py +++ b/aurora/models/base.py @@ -25,3 +25,6 @@ class AuroraGuildModel(AuroraBaseModel): def dump(self) -> dict: return self.model_dump(exclude={"bot", "guild_id", "guild"}) + + def __repr__(self) -> str: + return f"<{self.__class__.__name__} guild_id={self.guild_id}>" diff --git a/aurora/models/change.py b/aurora/models/change.py index ff95a57..f5e5f64 100644 --- a/aurora/models/change.py +++ b/aurora/models/change.py @@ -30,6 +30,18 @@ class Change(AuroraBaseModel): def __str__(self): return f"{self.type} {self.user_id} {self.reason}" + def __repr__(self) -> str: + attrs = [ + ('type', self.type), + ('timestamp', self.timestamp), + ('user_id', self.user_id), + ('reason', self.reason), + ('duration', self.duration), + ('end_timestamp', self.end_timestamp), + ] + joined = ' '.join('%s=%r' % t for t in attrs) + return f"<{self.__class__.__name__} {joined}>" + async def get_user(self) -> "PartialUser": return await PartialUser.from_id(self.bot, self.user_id) diff --git a/aurora/models/moderation.py b/aurora/models/moderation.py index 18dffa2..616c426 100644 --- a/aurora/models/moderation.py +++ b/aurora/models/moderation.py @@ -122,6 +122,29 @@ class Moderation(AuroraGuildModel): def __int__(self) -> int: return self.moderation_id + 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}>" + async def resolve(self, resolved_by: int, reason: str) -> Tuple[bool, str]: if self.resolved: raise ValueError("Case is already resolved!") diff --git a/aurora/models/partials.py b/aurora/models/partials.py index 05c3cc8..0aaa246 100644 --- a/aurora/models/partials.py +++ b/aurora/models/partials.py @@ -18,6 +18,9 @@ class PartialUser(AuroraBaseModel): def __str__(self): return self.name + def __repr__(self): + return f"<{self.__class__.__name__} id={self.id}>" + @classmethod async def from_id(cls, bot: Red, user_id: int) -> "PartialUser": user = bot.get_user(user_id) @@ -45,6 +48,9 @@ class PartialChannel(AuroraGuildModel): def __str__(self): return self.mention + def __repr__(self): + return f"<{self.__class__.__name__} id={self.id} guild_id={self.guild_id}>" + @classmethod async def from_id(cls, bot: Red, channel_id: int, guild: Guild) -> "PartialChannel": channel = bot.get_channel(channel_id) @@ -72,6 +78,9 @@ class PartialRole(AuroraGuildModel): def __str__(self): return self.mention + def __repr__(self) -> str: + return f"<{self.__class__.__name__} id={self.id} guild_id={self.guild_id}>" + @classmethod async def from_id(cls, bot: Red, guild: Guild, role_id: int) -> "PartialRole": role = guild.get_role(role_id) diff --git a/aurora/models/type.py b/aurora/models/type.py index 6cc8092..ba806a5 100644 --- a/aurora/models/type.py +++ b/aurora/models/type.py @@ -43,6 +43,14 @@ class Type(metaclass=AutoRegister(type_registry)): def __str__(self) -> str: return self.string + def __repr__(self) -> str: + attrs = [ + ('key', self.key), + ('channel', self.channel), + ] + joined = ' '.join('%s=%r' % t for t in attrs) + return f"<{self.__class__.__name__} {joined}>" + @classmethod async def handler(cls, ctx: commands.Context, target: Member | User | Messageable, silent: bool, **kwargs) -> 'Type': # pylint: disable=unused-argument """This method should be overridden by any child classes, but should retain the same starting keyword arguments.