From 32f3ce596a0c8f9c8e82f124171b76296bddcb0f Mon Sep 17 00:00:00 2001 From: Snipy7374 <100313469+Snipy7374@users.noreply.github.com> Date: Wed, 11 Oct 2023 23:20:22 +0200 Subject: [PATCH] feat: add new properties to disnake.Attachment --- changelog/1118.feature.rst | 1 + disnake/message.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 changelog/1118.feature.rst diff --git a/changelog/1118.feature.rst b/changelog/1118.feature.rst new file mode 100644 index 0000000000..ce4dcb3a05 --- /dev/null +++ b/changelog/1118.feature.rst @@ -0,0 +1 @@ +Add :attr:`.Attachment.expires_at` and :attr:`.Attachment.is_expired` properties to :class:`.Attachment`. diff --git a/disnake/message.py b/disnake/message.py index 21f59e269e..ebb470f76e 100644 --- a/disnake/message.py +++ b/disnake/message.py @@ -21,6 +21,7 @@ cast, overload, ) +from urllib.parse import parse_qs, urlparse from . import utils from .components import ActionRow, MessageComponent, _component_factory @@ -302,6 +303,7 @@ class Attachment(Hashable): "description", "duration", "waveform", + "_ex", "_flags", ) @@ -321,8 +323,25 @@ def __init__(self, *, data: AttachmentPayload, state: ConnectionState) -> None: self.waveform: Optional[bytes] = ( b64decode(waveform_data) if (waveform_data := data.get("waveform")) else None ) + _params = urlparse(self.url) + self._ex = parse_qs(_params.query)["ex"][0] self._flags: int = data.get("flags", 0) + def expires_at(self) -> datetime.datetime: + """The date when this attachment will expire. + + :return type: :class:`datetime.datetime` + """ + timestamp = int(self._ex, 16) + return datetime.datetime.fromtimestamp(timestamp, tz=datetime.timezone.utc) + + def is_expired(self) -> bool: + """Whether this attachment expired or not. + + :return type: :class:`bool` + """ + return utils.utcnow() >= self.expires_at() + def is_spoiler(self) -> bool: """Whether this attachment contains a spoiler.