Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add created_at to more models #1095

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1095.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``created_at`` property to :attr:`AutoModRule <AutoModRule.created_at>`, :attr:`ForumTag <ForumTag.created_at>`, :attr:`Integration <Integration.created_at>`, :attr:`StageInstance <StageInstance.created_at>`, and :attr:`Team <Team.created_at>`.
16 changes: 12 additions & 4 deletions disnake/automod.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from datetime import timedelta
import datetime
from typing import (
TYPE_CHECKING,
Dict,
Expand All @@ -25,7 +25,7 @@
try_enum_to_int,
)
from .flags import AutoModKeywordPresets
from .utils import MISSING, _get_as_snowflake
from .utils import MISSING, _get_as_snowflake, snowflake_time

if TYPE_CHECKING:
from typing_extensions import Self
Expand Down Expand Up @@ -207,10 +207,10 @@ class AutoModTimeoutAction(AutoModAction):

_metadata: AutoModTimeoutActionMetadata

def __init__(self, duration: Union[int, timedelta]) -> None:
def __init__(self, duration: Union[int, datetime.timedelta]) -> None:
super().__init__(type=AutoModActionType.timeout)

if isinstance(duration, timedelta):
if isinstance(duration, datetime.timedelta):
duration = int(duration.total_seconds())
self._metadata["duration_seconds"] = duration

Expand Down Expand Up @@ -492,6 +492,14 @@ def __init__(self, *, data: AutoModRulePayload, guild: Guild) -> None:
else frozenset()
)

@property
def created_at(self) -> datetime.datetime:
""":class:`datetime.datetime`: Returns the rule's creation time in UTC.

.. versionadded:: 2.10
"""
return snowflake_time(self.id)

@property
def actions(self) -> List[AutoModAction]:
"""List[Union[:class:`AutoModBlockMessageAction`, :class:`AutoModSendAlertAction`, :class:`AutoModTimeoutAction`, :class:`AutoModAction`]]:
Expand Down
18 changes: 17 additions & 1 deletion disnake/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

from .enums import ExpireBehaviour, try_enum
from .user import User
from .utils import MISSING, _get_as_snowflake, deprecated, parse_time, warn_deprecated
from .utils import (
MISSING,
_get_as_snowflake,
deprecated,
parse_time,
snowflake_time,
warn_deprecated,
)

__all__ = (
"IntegrationAccount",
Expand Down Expand Up @@ -99,6 +106,15 @@ def _from_data(self, data: PartialIntegrationPayload) -> None:
self.account: IntegrationAccount = IntegrationAccount(data["account"])
self.application_id: Optional[int] = _get_as_snowflake(data, "application_id")

@property
def created_at(self) -> datetime.datetime:
""":class:`datetime.datetime`: Returns the integration's
(*not* the associated application's) creation time in UTC.

.. versionadded:: 2.10
"""
return snowflake_time(self.id)


class Integration(PartialIntegration):
"""Represents a guild integration.
Expand Down
11 changes: 10 additions & 1 deletion disnake/stage_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from __future__ import annotations

import datetime
from typing import TYPE_CHECKING, Optional

from .enums import StagePrivacyLevel, try_enum
from .mixins import Hashable
from .utils import MISSING, _get_as_snowflake, cached_slot_property, warn_deprecated
from .utils import MISSING, _get_as_snowflake, cached_slot_property, snowflake_time, warn_deprecated

__all__ = ("StageInstance",)

Expand Down Expand Up @@ -84,6 +85,14 @@ def _update(self, data: StageInstancePayload) -> None:
def __repr__(self) -> str:
return f"<StageInstance id={self.id} guild={self.guild!r} channel_id={self.channel_id} topic={self.topic!r}>"

@property
def created_at(self) -> datetime.datetime:
""":class:`datetime.datetime`: Returns the stage instance's creation time in UTC.

.. versionadded:: 2.10
"""
return snowflake_time(self.id)

@cached_slot_property("_cs_channel")
def channel(self) -> Optional[StageChannel]:
"""Optional[:class:`StageChannel`]: The channel that stage instance is running in."""
Expand Down
9 changes: 9 additions & 0 deletions disnake/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import datetime
from typing import TYPE_CHECKING, List, Optional

from . import utils
Expand Down Expand Up @@ -52,6 +53,14 @@ def __init__(self, state: ConnectionState, data: TeamPayload) -> None:
def __repr__(self) -> str:
return f"<{self.__class__.__name__} id={self.id} name={self.name}>"

@property
def created_at(self) -> datetime.datetime:
""":class:`datetime.datetime`: Returns the team's creation time in UTC.

.. versionadded:: 2.10
"""
return utils.snowflake_time(self.id)

@property
def icon(self) -> Optional[Asset]:
"""Optional[:class:`.Asset`]: Retrieves the team's icon asset, if any."""
Expand Down
10 changes: 9 additions & 1 deletion disnake/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ def created_at(self) -> datetime.datetime:
""":class:`datetime.datetime`: Returns the thread's creation time in UTC.

.. versionchanged:: 2.4
If create_timestamp is provided by discord, that will be used instead of the time in the ID.
If ``create_timestamp`` is provided by Discord, that will be used instead of the time in the ID.
"""
return self.create_timestamp or snowflake_time(self.id)

Expand Down Expand Up @@ -1183,6 +1183,14 @@ def __repr__(self) -> str:
f" moderated={self.moderated!r} emoji={self.emoji!r}>"
)

@property
def created_at(self) -> datetime.datetime:
""":class:`datetime.datetime`: Returns the tag's creation time in UTC.

.. versionadded:: 2.10
"""
return snowflake_time(self.id)

def to_dict(self) -> PartialForumTagPayload:
emoji_name, emoji_id = PartialEmoji._emoji_to_name_id(self.emoji)
data: PartialForumTagPayload = {
Expand Down