feat(aurora): finished migrating case_factory to the new model

This commit is contained in:
Seaswimmer 2024-05-04 17:31:16 -04:00
parent 2c336ff70c
commit eea66607d9
Signed by untrusted user: cswimr
GPG key ID: 5D671B5D03D65A7F
2 changed files with 58 additions and 56 deletions

View file

@ -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)

View file

@ -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:** <t:{case_dict['timestamp']}> | <t:{case_dict['timestamp']}:R>"
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:** <t:{moderation.timestamp}> | <t:{moderation.timestamp}:R>"
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)} | <t:{case_dict['end_timestamp']}:R>"
if bool(case_dict["expired"]) is False
else str(humanize_timedelta(timedelta=td))
f"{humanize_timedelta(timedelta=moderation.duration)} | <t:{moderation.timestamp}:R>"
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:** <t:{moderation.metadata['imported_timestamp']}> | <t:{moderation.metadata['imported_timestamp']}:R>"
)
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,
)