WIP: Moderation type registry #26

Closed
cswimr wants to merge 146 commits from aurora-3rd-party into main
2 changed files with 58 additions and 56 deletions
Showing only changes of commit eea66607d9 - Show all commits

View file

@ -52,6 +52,11 @@ class Moderation(AuroraBaseModel):
return await PartialUser.from_id(self.bot, self.resolved_by) return await PartialUser.from_id(self.bot, self.resolved_by)
return None 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): def __str__(self):
return f"{self.moderation_type} {self.target_type} {self.target_id} {self.reason}" 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": def from_dict(cls, bot: Red, data: dict) -> "Moderation":
return cls(bot=bot, **data) 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 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): class PartialUser(AuroraBaseModel):
@ -127,3 +132,26 @@ class PartialChannel(AuroraBaseModel):
if e == Forbidden: if e == Forbidden:
return cls(id=channel_id, name="Forbidden Channel") return cls(id=channel_id, name="Forbidden Channel")
return cls(id=channel_id, name="Deleted 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 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. """This function creates a case embed from set parameters.
Args: Args:
interaction (Interaction): The interaction object. interaction (discord.Interaction): The interaction object.
case_dict (dict): The case dictionary. moderation (aurora.models.Moderation): The moderation object.
""" """
if case_dict["target_type"] == "USER": target = await moderation.get_target()
target_user = await fetch_user_dict(interaction.client, case_dict["target_id"]) moderator = await moderation.get_moderator()
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']}`"
)
embed = Embed( embed = Embed(
title=f"📕 Case #{case_dict['moderation_id']:,}", title=f"📕 Case #{moderation.id:,}",
color=await interaction.client.get_embed_color(interaction.channel), 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": if moderation.duration:
td = timedelta(
**{
unit: int(val)
for unit, val in zip(
["hours", "minutes", "seconds"], case_dict["duration"].split(":")
)
}
)
duration_embed = ( duration_embed = (
f"{humanize_timedelta(timedelta=td)} | <t:{case_dict['end_timestamp']}:R>" f"{humanize_timedelta(timedelta=moderation.duration)} | <t:{moderation.timestamp}:R>"
if bool(case_dict["expired"]) is False if moderation.expired is False
else str(humanize_timedelta(timedelta=td)) 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 += ( embed.description += (
f"\n**Changes:** {len(case_dict['changes']) - 1}" f"\n**Changes:** {len(moderation.changes) - 1}"
if case_dict["changes"] if moderation.changes
else "\n**Changes:** 0" else "\n**Changes:** 0"
) )
if case_dict["role_id"]: if moderation.role_id:
embed.description += f"\n**Role:** <@&{case_dict['role_id']}>" role = await moderation.get_role()
embed.description += f"\n**Role:** {role.name}"
if case_dict["metadata"]: if moderation.metadata:
if case_dict["metadata"]["imported_from"]: if moderation.metadata["imported_from"]:
embed.description += ( 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: if moderation.resolved:
resolved_user = await fetch_user_dict(interaction.client, case_dict["resolved_by"]) resolved_user = await moderation.get_resolved_by()
resolved_name = (
f"`{resolved_user['name']}`"
if resolved_user["discriminator"] == "0"
else f"`{resolved_user['name']}#{resolved_user['discriminator']}`"
)
embed.add_field( embed.add_field(
name="Resolve Reason", 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, inline=False,
) )