From eea66607d9479315a742f7111fc825b7015f4836 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sat, 4 May 2024 17:31:16 -0400 Subject: [PATCH] feat(aurora): finished migrating case_factory to the new model --- aurora/models.py | 32 ++++++++++++++- aurora/utilities/factory.py | 82 +++++++++++++------------------------ 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/aurora/models.py b/aurora/models.py index e9f0ca0..2561267 100644 --- a/aurora/models.py +++ b/aurora/models.py @@ -52,6 +52,11 @@ class Moderation(AuroraBaseModel): return await PartialUser.from_id(self.bot, self.resolved_by) return None + async def get_role(self) -> Optional["PartialRole"]: + if self.role_id: + return await PartialRole.from_id(self.bot, self.guild_id, self.role_id) + return None + def __str__(self): return f"{self.moderation_type} {self.target_type} {self.target_id} {self.reason}" @@ -76,9 +81,9 @@ class Moderation(AuroraBaseModel): def from_dict(cls, bot: Red, data: dict) -> "Moderation": return cls(bot=bot, **data) - def to_json(self, indent: int = None, file: Any = None): + 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) if file else dumps(self.model_dump(exclude={"bot", "guild_id"}), indent=indent) + 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 PartialUser(AuroraBaseModel): @@ -127,3 +132,26 @@ class PartialChannel(AuroraBaseModel): if e == Forbidden: return cls(id=channel_id, name="Forbidden Channel") return cls(id=channel_id, name="Deleted Channel") + +class PartialRole(AuroraBaseModel): + id: int + guild_id: int + name: str + + @property + def mention(self): + return f"<@&{self.id}>" + + def __str__(self): + return self.mention + + @classmethod + async def from_id(cls, bot: Red, guild_id: int, role_id: int) -> "PartialRole": + try: + guild = await bot.fetch_guild(guild_id, with_counts=False) + except (Forbidden, HTTPException): + return cls(id=role_id, guild_id=guild_id, name="Forbidden Role") + role = guild.get_role(role_id) + if not role: + return cls(id=role_id, guild_id=guild_id, name="Deleted Role") + return cls(id=role.id, guild_id=guild_id, name=role.name) diff --git a/aurora/utilities/factory.py b/aurora/utilities/factory.py index c6506ec..09d9dd3 100644 --- a/aurora/utilities/factory.py +++ b/aurora/utilities/factory.py @@ -155,83 +155,57 @@ async def log_factory( return embed -async def case_factory(interaction: Interaction, case_dict: dict) -> Embed: +async def case_factory(interaction: Interaction, moderation: Moderation) -> Embed: """This function creates a case embed from set parameters. Args: - interaction (Interaction): The interaction object. - case_dict (dict): The case dictionary. + interaction (discord.Interaction): The interaction object. + moderation (aurora.models.Moderation): The moderation object. """ - if case_dict["target_type"] == "USER": - target_user = await fetch_user_dict(interaction.client, case_dict["target_id"]) - target_name = ( - f"`{target_user['name']}`" - if target_user["discriminator"] == "0" - else f"`{target_user['name']}#{target_user['discriminator']}`" - ) - elif case_dict["target_type"] == "CHANNEL": - target_user = await fetch_channel_dict(interaction.guild, case_dict["target_id"]) - if target_user["mention"]: - target_name = f"{target_user['mention']}" - else: - target_name = f"`{target_user['name']}`" - - moderator_user = await fetch_user_dict(interaction.client, case_dict["moderator_id"]) - moderator_name = ( - f"`{moderator_user['name']}`" - if moderator_user["discriminator"] == "0" - else f"`{moderator_user['name']}#{moderator_user['discriminator']}`" - ) + target = await moderation.get_target() + moderator = await moderation.get_moderator() embed = Embed( - title=f"📕 Case #{case_dict['moderation_id']:,}", + title=f"📕 Case #{moderation.id:,}", color=await interaction.client.get_embed_color(interaction.channel), ) - embed.description = f"**Type:** {str.title(case_dict['moderation_type'])}\n**Target:** {target_name} ({target_user['id']})\n**Moderator:** {moderator_name} ({moderator_user['id']})\n**Resolved:** {bool(case_dict['resolved'])}\n**Timestamp:** | " + embed.description = f"**Type:** {str.title(moderation.type)}\n**Target:** {target.name} ({target.id})\n**Moderator:** {moderator.name} ({moderator.id})\n**Resolved:** {moderation.resolved}\n**Timestamp:** | " - if case_dict["duration"] != "NULL": - td = timedelta( - **{ - unit: int(val) - for unit, val in zip( - ["hours", "minutes", "seconds"], case_dict["duration"].split(":") - ) - } - ) + if moderation.duration: duration_embed = ( - f"{humanize_timedelta(timedelta=td)} | " - if bool(case_dict["expired"]) is False - else str(humanize_timedelta(timedelta=td)) + f"{humanize_timedelta(timedelta=moderation.duration)} | " + if moderation.expired is False + else str(humanize_timedelta(timedelta=moderation.duration)) ) - embed.description += f"\n**Duration:** {duration_embed}\n**Expired:** {bool(case_dict['expired'])}" + embed.description += f"\n**Duration:** {duration_embed}\n**Expired:** {moderation.expired}" embed.description += ( - f"\n**Changes:** {len(case_dict['changes']) - 1}" - if case_dict["changes"] + f"\n**Changes:** {len(moderation.changes) - 1}" + if moderation.changes else "\n**Changes:** 0" ) - if case_dict["role_id"]: - embed.description += f"\n**Role:** <@&{case_dict['role_id']}>" + if moderation.role_id: + role = await moderation.get_role() + embed.description += f"\n**Role:** {role.name}" - if case_dict["metadata"]: - if case_dict["metadata"]["imported_from"]: + if moderation.metadata: + if moderation.metadata["imported_from"]: embed.description += ( - f"\n**Imported From:** {case_dict['metadata']['imported_from']}" + f"\n**Imported From:** {moderation.metadata['imported_from']}" + ) + if moderation.metadata["imported_timestamp"]: + embed.description += ( + f"\n**Imported Timestamp:** | " ) - embed.add_field(name="Reason", value=box(case_dict["reason"]), inline=False) + embed.add_field(name="Reason", value=box(moderation.reason), inline=False) - if case_dict["resolved"] == 1: - resolved_user = await fetch_user_dict(interaction.client, case_dict["resolved_by"]) - resolved_name = ( - f"`{resolved_user['name']}`" - if resolved_user["discriminator"] == "0" - else f"`{resolved_user['name']}#{resolved_user['discriminator']}`" - ) + if moderation.resolved: + resolved_user = await moderation.get_resolved_by() embed.add_field( name="Resolve Reason", - value=f"Resolved by {resolved_name} ({resolved_user['id']}) for:\n{box(case_dict['resolve_reason'])}", + value=f"Resolved by {resolved_user.name} ({resolved_user.id}) for:\n{box(moderation.resolve_reason)}", inline=False, )