From 94f6d6c3b588cc7de63b533d1f1773106d4717df Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sat, 4 May 2024 21:15:39 -0400 Subject: [PATCH] fix(aurora): fixed the changes_factory --- aurora/models.py | 14 ++++++++++++++ aurora/utilities/factory.py | 38 ++++++++++++++----------------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/aurora/models.py b/aurora/models.py index 069bc40..a7b81a7 100644 --- a/aurora/models.py +++ b/aurora/models.py @@ -106,6 +106,10 @@ class Change(AuroraBaseModel): duration: Optional[timedelta] = None end_timestamp: Optional[datetime] = None + @property + def unix_timestamp(self) -> int: + return int(self.timestamp.timestamp()) + def __str__(self): return f"{self.type} {self.user_id} {self.reason}" @@ -114,6 +118,16 @@ class Change(AuroraBaseModel): @classmethod def from_dict(cls, bot: Red, data: dict) -> "Change": + if data["duration"] is not None: + hours, minutes, seconds = map(int, data["duration"].split(':')) + duration = timedelta(hours=hours, minutes=minutes, seconds=seconds) + else: + duration = None + data.update({ + "timestamp": datetime.fromtimestamp(data["timestamp"]), + "end_timestamp": datetime.fromtimestamp(data["end_timestamp"]) if data["end_timestamp"] else None, + "duration": duration + }) return cls(bot=bot, **data) class PartialUser(AuroraBaseModel): diff --git a/aurora/utilities/factory.py b/aurora/utilities/factory.py index 63e8f03..4a0945e 100644 --- a/aurora/utilities/factory.py +++ b/aurora/utilities/factory.py @@ -2,17 +2,13 @@ from datetime import datetime, timedelta from typing import Union -from discord import (Color, Embed, Guild, Interaction, InteractionMessage, - Member, Role, User) +from discord import Color, Embed, Guild, Interaction, InteractionMessage, Member, Role, User from redbot.core import commands -from redbot.core.utils.chat_formatting import (bold, box, error, - humanize_timedelta, warning) +from redbot.core.utils.chat_formatting import bold, box, error, humanize_timedelta, warning from aurora.models import Moderation from aurora.utilities.config import config -from aurora.utilities.utils import (fetch_channel_dict, fetch_user_dict, - get_bool_emoji, get_next_case_number, - get_pagesize_str) +from aurora.utilities.utils import fetch_channel_dict, fetch_user_dict, get_bool_emoji, get_next_case_number, get_pagesize_str async def message_factory( @@ -228,38 +224,32 @@ async def changes_factory(interaction: Interaction, moderation: Moderation) -> E if moderation.changes: for change in moderation.changes: - if change["user_id"] not in memory_dict: - memory_dict[str(change["user_id"])] = await fetch_user_dict( - interaction.client, change["user_id"] - ) + if change.user_id not in memory_dict: + memory_dict[str(change.user_id)] = await change.get_user() - user = memory_dict[str(change["user_id"])] - name = ( - user["name"] - if user["discriminator"] == "0" - else f"{user['name']}#{user['discriminator']}" - ) + user = memory_dict[str(change.user_id)] + name = user["name"] - timestamp = f" | " + timestamp = f" | " - if change["type"] == "ORIGINAL": + if change.type == "ORIGINAL": embed.add_field( name="Original", - value=f"**User:** `{name}` ({user['id']})\n**Reason:** {change['reason']}\n**Timestamp:** {timestamp}", + value=f"**User:** `{name}` ({user['id']})\n**Reason:** {change.reason}\n**Timestamp:** {timestamp}", inline=False, ) - elif change["type"] == "EDIT": + elif change.type == "EDIT": embed.add_field( name="Edit", - value=f"**User:** `{name}` ({user['id']})\n**Reason:** {change['reason']}\n**Timestamp:** {timestamp}", + value=f"**User:** `{name}` ({user['id']})\n**Reason:** {change.reason}\n**Timestamp:** {timestamp}", inline=False, ) - elif change["type"] == "RESOLVE": + elif change.type == "RESOLVE": embed.add_field( name="Resolve", - value=f"**User:** `{name}` ({user['id']})\n**Reason:** {change['reason']}\n**Timestamp:** {timestamp}", + value=f"**User:** `{name}` ({user['id']})\n**Reason:** {change.reason}\n**Timestamp:** {timestamp}", inline=False, )