2024-05-06 17:23:59 -04:00
|
|
|
import json
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from typing import Literal, Optional
|
|
|
|
|
|
|
|
from redbot.core.bot import Red
|
|
|
|
|
2024-06-04 23:52:35 -04:00
|
|
|
from ..utilities.utils import timedelta_from_string
|
2024-05-09 21:27:26 -04:00
|
|
|
from .base import AuroraBaseModel
|
|
|
|
from .partials import PartialUser
|
2024-05-06 17:23:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Change(AuroraBaseModel):
|
|
|
|
type: Literal["ORIGINAL", "RESOLVE", "EDIT"]
|
|
|
|
timestamp: datetime
|
|
|
|
user_id: int
|
2024-08-19 17:19:01 -04:00
|
|
|
reason: Optional[str] = None
|
2024-05-06 17:23:59 -04:00
|
|
|
duration: Optional[timedelta] = None
|
|
|
|
end_timestamp: Optional[datetime] = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unix_timestamp(self) -> int:
|
|
|
|
return int(self.timestamp.timestamp())
|
|
|
|
|
2024-08-19 17:50:52 -04:00
|
|
|
@property
|
|
|
|
def unix_end_timestamp(self) -> Optional[int]:
|
|
|
|
if self.end_timestamp:
|
|
|
|
return int(self.end_timestamp.timestamp())
|
|
|
|
return None
|
|
|
|
|
2024-05-06 17:23:59 -04:00
|
|
|
def __str__(self):
|
|
|
|
return f"{self.type} {self.user_id} {self.reason}"
|
|
|
|
|
2024-08-22 15:33:36 -04:00
|
|
|
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}>"
|
|
|
|
|
2024-05-06 17:23:59 -04:00
|
|
|
async def get_user(self) -> "PartialUser":
|
|
|
|
return await PartialUser.from_id(self.bot, self.user_id)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_dict(cls, bot: Red, data: dict) -> "Change":
|
|
|
|
if isinstance(data, str):
|
|
|
|
data = json.loads(data)
|
2024-08-21 15:15:50 -04:00
|
|
|
if data.get('duration') and not isinstance(data["duration"], timedelta) and not data["duration"] == "NULL":
|
2024-06-04 23:52:35 -04:00
|
|
|
duration = timedelta_from_string(data["duration"])
|
2024-08-21 15:15:50 -04:00
|
|
|
elif data.get('duration') and isinstance(data["duration"], timedelta):
|
2024-05-06 17:23:59 -04:00
|
|
|
duration = data["duration"]
|
|
|
|
else:
|
|
|
|
duration = None
|
|
|
|
|
2024-08-21 15:15:50 -04:00
|
|
|
if data.get('end_timestamp') and not isinstance(data["end_timestamp"], datetime):
|
2024-05-06 17:23:59 -04:00
|
|
|
end_timestamp = datetime.fromtimestamp(data["end_timestamp"])
|
2024-08-21 15:15:50 -04:00
|
|
|
elif data.get('end_timestamp') and isinstance(data["end_timestamp"], datetime):
|
2024-05-06 17:23:59 -04:00
|
|
|
end_timestamp = data["end_timestamp"]
|
|
|
|
else:
|
|
|
|
end_timestamp = None
|
|
|
|
|
|
|
|
if not isinstance(data["timestamp"], datetime):
|
|
|
|
timestamp = datetime.fromtimestamp(data["timestamp"])
|
|
|
|
else:
|
|
|
|
timestamp = data["timestamp"]
|
|
|
|
|
2024-06-04 15:32:59 -04:00
|
|
|
try:
|
|
|
|
data["user_id"] = int(data["user_id"])
|
|
|
|
except ValueError:
|
|
|
|
data["user_id"] = 0
|
|
|
|
|
2024-05-06 17:23:59 -04:00
|
|
|
data.update({
|
|
|
|
"timestamp": timestamp,
|
|
|
|
"end_timestamp": end_timestamp,
|
2024-06-04 15:32:59 -04:00
|
|
|
"duration": duration
|
2024-05-06 17:23:59 -04:00
|
|
|
})
|
2024-08-21 15:16:40 -04:00
|
|
|
if "bot" in data:
|
|
|
|
del data["bot"]
|
2024-05-06 17:23:59 -04:00
|
|
|
return cls(bot=bot, **data)
|