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: Implement Attachmentflags and Attachment expiry attributes #2342

Merged
merged 7 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
66 changes: 66 additions & 0 deletions discord/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
__all__ = (
"SystemChannelFlags",
"MessageFlags",
"AttachmentFlags",
"PublicUserFlags",
"Intents",
"MemberCacheFlags",
Expand Down Expand Up @@ -1485,3 +1486,68 @@ def require_tag(self):
.. versionadded:: 2.2
"""
return 1 << 4


@fill_with_flags()
class AttachmentFlags(BaseFlags):
r"""Wraps up the Discord Attachment flags.

See :class:`SystemChannelFlags`.

.. container:: operations

.. describe:: x == y

Checks if two flags are equal.
.. describe:: x != y

Checks if two flags are not equal.
.. describe:: x + y

Adds two flags together. Equivalent to ``x | y``.
.. describe:: x - y

Subtracts two flags from each other.
.. describe:: x | y

Returns the union of two flags. Equivalent to ``x + y``.
.. describe:: x & y

Returns the intersection of two flags.
.. describe:: ~x

Returns the inverse of a flag.
.. describe:: hash(x)

Return the flag's hash.
.. describe:: iter(x)

Returns an iterator of ``(name, value)`` pairs. This allows it
to be, for example, constructed as a dict or a list of pairs.

.. versionadded:: 2.5

Attributes
-----------
value: :class:`int`
The raw value. This value is a bit array field of a 53-bit integer
representing the currently available flags. You should query
flags via the properties rather than using this raw value.
"""

__slots__ = ()

@flag_value
def is_clip(self):
""":class:`bool`: Returns ``True`` if the attachment is a clip."""
return 1 << 0

@flag_value
def is_thumbnail(self):
""":class:`bool`: Returns ``True`` if the attachment is a thumbnail."""
return 1 << 1

@flag_value
def is_remix(self):
""":class:`bool`: Returns ``True`` if the attachment has been remixed."""
return 1 << 2
43 changes: 42 additions & 1 deletion discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
Union,
overload,
)
from urllib.parse import parse_qs, urlparse

from . import utils
from .components import _component_factory
Expand All @@ -47,7 +48,7 @@
from .enums import ChannelType, MessageType, try_enum
from .errors import InvalidArgument
from .file import File
from .flags import MessageFlags
from .flags import AttachmentFlags, MessageFlags
from .guild import Guild
from .member import Member
from .mixins import Hashable
Expand Down Expand Up @@ -178,6 +179,16 @@ class Attachment(Hashable):
waveform: Optional[:class:`str`]
The base64 encoded bytearray representing a sampled waveform (currently for voice messages).

.. versionadded:: 2.5

flags: :class:`AttachmentFlags`
Extra attributes of the attachment.

.. versionadded:: 2.5

hm: :class:`str`
The unique signature of this attachment's instance.

.. versionadded:: 2.5
"""

Expand All @@ -195,6 +206,10 @@ class Attachment(Hashable):
"description",
"duration_secs",
"waveform",
"flags",
"_ex",
"_is",
"hm",
)

def __init__(self, *, data: AttachmentPayload, state: ConnectionState):
Expand All @@ -211,6 +226,32 @@ def __init__(self, *, data: AttachmentPayload, state: ConnectionState):
self.description: str | None = data.get("description")
self.duration_secs: float | None = data.get("duration_secs")
self.waveform: str | None = data.get("waveform")
self.flags: AttachmentFlags = AttachmentFlags._from_value(data.get("flags", 0))
self._ex: str | None = None
self._is: str | None = None
self.hm: str | None = None

q = urlparse(self.url).query
extras = ["_ex", "_is", "hm"]
if qs := parse_qs(q):
for attr in extras:
v = "".join(qs.get(attr.replace("_", ""), []))
if v:
setattr(self, attr, v)
Dorukyum marked this conversation as resolved.
Show resolved Hide resolved

@property
def expires_at(self) -> datetime.datetime:
"""This attachment URL's expiry time in UTC."""
if not self._ex:
return None
return datetime.datetime.utcfromtimestamp(int(self._ex, 16))

@property
def issued_at(self) -> datetime.datetime:
"""The attachment URL's issue time in UTC."""
if not self._is:
return None
return datetime.datetime.utcfromtimestamp(int(self._is, 16))

def is_spoiler(self) -> bool:
"""Whether this attachment contains a spoiler."""
Expand Down
1 change: 1 addition & 0 deletions discord/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Attachment(TypedDict):
proxy_url: str
duration_secs: NotRequired[float]
waveform: NotRequired[str]
flags: NotRequired[int]


MessageActivityType = Literal[1, 2, 3, 5]
Expand Down
5 changes: 5 additions & 0 deletions docs/api/data_classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ Flags
.. autoclass:: MessageFlags()
:members:

.. attributetable:: AttachmentFlags

.. autoclass:: AttachmentFlags()
:members:

.. attributetable:: PublicUserFlags

.. autoclass:: PublicUserFlags()
Expand Down
Loading