Skip to content

Commit

Permalink
Merge branch 'master' into onboarding
Browse files Browse the repository at this point in the history
Signed-off-by: Lala Sabathil <lala@pycord.dev>
  • Loading branch information
Lulalaby committed Jan 31, 2024
2 parents 47e89cd + 90f9023 commit d01ffc9
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2178](https://github.com/Pycord-Development/pycord/pull/2178))
- Added `applied_tags` parameter to `Webhook.send()` method.
([#2322](https://github.com/Pycord-Development/pycord/pull/2322))
- Added `User.avatar_decoration`.
([#2131](https://github.com/Pycord-Development/pycord/pull/2131))
- Added support for guild onboarding related features.
([#2127](https://github.com/Pycord-Development/pycord/pull/2127))

Expand Down
17 changes: 17 additions & 0 deletions discord/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ def _from_avatar(cls, state, user_id: int, avatar: str) -> Asset:
animated=animated,
)

@classmethod
def _from_avatar_decoration(
cls, state, user_id: int, avatar_decoration: str
) -> Asset:
animated = avatar_decoration.startswith("a_")
endpoint = (
"avatar-decoration-presets"
# if avatar_decoration.startswith(("v3", "v2"))
# else f"avatar-decorations/{user_id}"
)
return cls(
state,
url=f"{cls.BASE}/{endpoint}/{avatar_decoration}.png?size=1024",
key=avatar_decoration,
animated=animated,
)

@classmethod
def _from_guild_avatar(
cls, state, guild_id: int, member_id: int, avatar: str
Expand Down
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

query = urlparse(self.url).query
extras = ["_ex", "_is", "hm"]
if query_params := parse_qs(query):
for attr in extras:
value = "".join(query_params.get(attr.replace("_", ""), []))
if value:
setattr(self, attr, value)

@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
16 changes: 16 additions & 0 deletions discord/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class BaseUser(_UserTag):
"bot",
"system",
"_public_flags",
"_avatar_decoration",
"_state",
)

Expand All @@ -84,6 +85,7 @@ class BaseUser(_UserTag):
_avatar: str | None
_banner: str | None
_accent_colour: int | None
_avatar_decoration: dict | None
_public_flags: int

def __init__(self, *, state: ConnectionState, data: UserPayload) -> None:
Expand Down Expand Up @@ -134,6 +136,7 @@ def _update(self, data: UserPayload) -> None:
self._avatar = data["avatar"]
self._banner = data.get("banner", None)
self._accent_colour = data.get("accent_color", None)
self._avatar_decoration = data.get("avatar_decoration_data", None)
self._public_flags = data.get("public_flags", 0)
self.bot = data.get("bot", False)
self.system = data.get("system", False)
Expand All @@ -149,6 +152,7 @@ def _copy(cls: type[BU], user: BU) -> BU:
self._avatar = user._avatar
self._banner = user._banner
self._accent_colour = user._accent_colour
self._avatar_decoration = user._avatar_decoration
self.bot = user.bot
self._state = user._state
self._public_flags = user._public_flags
Expand Down Expand Up @@ -221,6 +225,18 @@ def banner(self) -> Asset | None:
return None
return Asset._from_user_banner(self._state, self.id, self._banner)

@property
def avatar_decoration(self) -> Asset | None:
"""Returns the user's avatar decoration, if available.
.. versionadded:: 2.5
"""
if self._avatar_decoration is None:
return None
return Asset._from_avatar_decoration(
self._state, self.id, self._avatar_decoration.get("asset")
)

@property
def accent_colour(self) -> Colour | None:
"""Returns the user's accent colour, if applicable.
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

0 comments on commit d01ffc9

Please sign in to comment.