Skip to content

Commit

Permalink
feat: add new properties to disnake.Attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
Snipy7374 committed Oct 11, 2023
1 parent 2b6aead commit 32f3ce5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/1118.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :attr:`.Attachment.expires_at` and :attr:`.Attachment.is_expired` properties to :class:`.Attachment`.
19 changes: 19 additions & 0 deletions disnake/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
cast,
overload,
)
from urllib.parse import parse_qs, urlparse

from . import utils
from .components import ActionRow, MessageComponent, _component_factory
Expand Down Expand Up @@ -302,6 +303,7 @@ class Attachment(Hashable):
"description",
"duration",
"waveform",
"_ex",
"_flags",
)

Expand All @@ -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.
Expand Down

0 comments on commit 32f3ce5

Please sign in to comment.