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(message): add the new ex query param to Attachment with some util property #1118

Closed
wants to merge 2 commits into from
Closed
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/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`.
27 changes: 27 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,33 @@ 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).get("ex")
self._flags: int = data.get("flags", 0)

def expires_at(self) -> Optional[datetime.datetime]:
"""The date when this attachment will expire.
``None`` if the ``ex`` param is not present in :attr:`.Attachment.url`.

:return type: Optional[:class:`datetime.datetime`]
"""
if not self._ex:
return

timestamp = int(self._ex[0], 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`
"""
ex = self.expires_at()
if not ex:
return False

return utils.utcnow() >= ex

def is_spoiler(self) -> bool:
"""Whether this attachment contains a spoiler.

Expand Down
Loading