Skip to content

Commit

Permalink
Add support for attachment flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Soheab authored Jul 15, 2023
1 parent 00ea67d commit 14faa9b
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
74 changes: 74 additions & 0 deletions discord/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
'ChannelFlags',
'AutoModPresets',
'MemberFlags',
'AttachmentFlags',
)

BF = TypeVar('BF', bound='BaseFlags')
Expand Down Expand Up @@ -1824,3 +1825,76 @@ def bypasses_verification(self):
def started_onboarding(self):
""":class:`bool`: Returns ``True`` if the member has started onboarding."""
return 1 << 3


@fill_with_flags()
class AttachmentFlags(BaseFlags):
r"""Wraps up the Discord Attachment flags
.. versionadded:: 2.4
.. container:: operations
.. describe:: x == y
Checks if two AttachmentFlags are equal.
.. describe:: x != y
Checks if two AttachmentFlags are not equal.
.. describe:: x | y, x |= y
Returns a AttachmentFlags instance with all enabled flags from
both x and y.
.. describe:: x & y, x &= y
Returns a AttachmentFlags instance with only flags enabled on
both x and y.
.. describe:: x ^ y, x ^= y
Returns a AttachmentFlags instance with only flags enabled on
only one of x or y, not on both.
.. describe:: ~x
Returns a AttachmentFlags instance with all flags inverted from x.
.. 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.
Note that aliases are not shown.
.. describe:: bool(b)
Returns whether any flag is set to ``True``.
Attributes
-----------
value: :class:`int`
The raw value. You should query flags via the properties
rather than using this raw value.
"""

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

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

@flag_value
def remix(self):
""":class:`bool`: Returns ``True`` if the attachment has been edited using the remix feature."""
return 1 << 2
10 changes: 9 additions & 1 deletion discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
from .components import _component_factory
from .embeds import Embed
from .member import Member
from .flags import MessageFlags
from .flags import MessageFlags, AttachmentFlags
from .file import File
from .utils import escape_mentions, MISSING
from .http import handle_message_parameters
Expand Down Expand Up @@ -207,6 +207,7 @@ class Attachment(Hashable):
'ephemeral',
'duration',
'waveform',
'_flags',
)

def __init__(self, *, data: AttachmentPayload, state: ConnectionState):
Expand All @@ -226,6 +227,13 @@ def __init__(self, *, data: AttachmentPayload, state: ConnectionState):
waveform = data.get('waveform')
self.waveform: Optional[bytes] = utils._base64_to_bytes(waveform) if waveform is not None else None

self._flags: int = data.get('flags', 0)

@property
def flags(self) -> AttachmentFlags:
""":class:`AttachmentFlags`: The attachment's flags."""
return AttachmentFlags._from_value(self._flags)

def is_spoiler(self) -> bool:
""":class:`bool`: Whether this attachment contains a spoiler."""
return self.filename.startswith('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 @@ -70,6 +70,7 @@ class Attachment(TypedDict):
ephemeral: NotRequired[bool]
duration_secs: NotRequired[float]
waveform: NotRequired[str]
flags: NotRequired[int]


MessageActivityType = Literal[1, 2, 3, 5]
Expand Down
8 changes: 8 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4982,6 +4982,14 @@ MemberFlags
.. autoclass:: MemberFlags
:members:

AttachmentFlags
~~~~~~~~~~~~~~~~

.. attributetable:: AttachmentFlags

.. autoclass:: AttachmentFlags
:members:

ForumTag
~~~~~~~~~

Expand Down

0 comments on commit 14faa9b

Please sign in to comment.