Skip to content

Commit

Permalink
Make embed flags required and add them to all media fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfies authored Mar 4, 2025
1 parent de5720e commit cab4732
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
64 changes: 33 additions & 31 deletions discord/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ class _EmbedMediaProxy(Protocol):
proxy_url: Optional[str]
height: Optional[int]
width: Optional[int]
flags: Optional[AttachmentFlags]

class _EmbedVideoProxy(Protocol):
url: Optional[str]
height: Optional[int]
width: Optional[int]
flags: AttachmentFlags

class _EmbedProviderProxy(Protocol):
name: Optional[str]
Expand Down Expand Up @@ -148,10 +143,6 @@ class Embed:
colour: Optional[Union[:class:`Colour`, :class:`int`]]
The colour code of the embed. Aliased to ``color`` as well.
This can be set during initialisation.
flags: Optional[:class:`EmbedFlags`]
The flags of this embed.
.. versionadded:: 2.5
"""

__slots__ = (
Expand All @@ -168,7 +159,7 @@ class Embed:
'_author',
'_fields',
'description',
'flags',
'_flags',
)

def __init__(
Expand All @@ -188,7 +179,7 @@ def __init__(
self.type: EmbedType = type
self.url: Optional[str] = url
self.description: Optional[str] = description
self.flags: Optional[EmbedFlags] = None
self._flags: int = 0

if self.title is not None:
self.title = str(self.title)
Expand Down Expand Up @@ -223,6 +214,7 @@ def from_dict(cls, data: Mapping[str, Any]) -> Self:
self.type = data.get('type', None)
self.description = data.get('description', None)
self.url = data.get('url', None)
self._flags = data.get('flags', 0)

if self.title is not None:
self.title = str(self.title)
Expand Down Expand Up @@ -253,11 +245,6 @@ def from_dict(cls, data: Mapping[str, Any]) -> Self:
else:
setattr(self, '_' + attr, value)

try:
self.flags = EmbedFlags._from_value(data['flags'])
except KeyError:
pass

return self

def copy(self) -> Self:
Expand Down Expand Up @@ -318,8 +305,17 @@ def __eq__(self, other: Embed) -> bool:
and self.image == other.image
and self.provider == other.provider
and self.video == other.video
and self._flags == other._flags
)

@property
def flags(self) -> EmbedFlags:
""":class:`EmbedFlags`: The flags of this embed.
.. versionadded:: 2.5
"""
return EmbedFlags._from_value(self._flags or 0)

@property
def colour(self) -> Optional[Colour]:
return getattr(self, '_colour', None)
Expand Down Expand Up @@ -408,18 +404,17 @@ def image(self) -> _EmbedMediaProxy:
Possible attributes you can access are:
- ``url``
- ``proxy_url``
- ``width``
- ``height``
- ``flags``
- ``url`` for the image URL.
- ``proxy_url`` for the proxied image URL.
- ``width`` for the image width.
- ``height`` for the image height.
- ``flags`` for the image's attachment flags.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
data = getattr(self, '_image', {})
if 'flags' in data:
data['flags'] = AttachmentFlags._from_value(data['flags'])
data['flags'] = AttachmentFlags._from_value(data.get('flags', 0))
return EmbedProxy(data) # type: ignore

def set_image(self, *, url: Optional[Any]) -> Self:
Expand Down Expand Up @@ -454,15 +449,18 @@ def thumbnail(self) -> _EmbedMediaProxy:
Possible attributes you can access are:
- ``url``
- ``proxy_url``
- ``width``
- ``height``
- ``url`` for the thumbnail URL.
- ``proxy_url`` for the proxied thumbnail URL.
- ``width`` for the thumbnail width.
- ``height`` for the thumbnail height.
- ``flags`` for the thumbnail's attachment flags.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
return EmbedProxy(getattr(self, '_thumbnail', {})) # type: ignore
data = getattr(self, '_thumbnail', {})
data['flags'] = AttachmentFlags._from_value(data.get('flags', 0))
return EmbedProxy(data) # type: ignore

def set_thumbnail(self, *, url: Optional[Any]) -> Self:
"""Sets the thumbnail for the embed content.
Expand Down Expand Up @@ -491,19 +489,23 @@ def set_thumbnail(self, *, url: Optional[Any]) -> Self:
return self

@property
def video(self) -> _EmbedVideoProxy:
def video(self) -> _EmbedMediaProxy:
"""Returns an ``EmbedProxy`` denoting the video contents.
Possible attributes include:
- ``url`` for the video URL.
- ``proxy_url`` for the proxied video URL.
- ``height`` for the video height.
- ``width`` for the video width.
- ``flags`` for the video's attachment flags.
If the attribute has no value then ``None`` is returned.
"""
# Lying to the type checker for better developer UX.
return EmbedProxy(getattr(self, '_video', {})) # type: ignore
data = getattr(self, '_video', {})
data['flags'] = AttachmentFlags._from_value(data.get('flags', 0))
return EmbedProxy(data) # type: ignore

@property
def provider(self) -> _EmbedProviderProxy:
Expand Down
22 changes: 4 additions & 18 deletions discord/types/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,14 @@ class EmbedField(TypedDict):
inline: NotRequired[bool]


class EmbedThumbnail(TypedDict, total=False):
class EmbedMedia(TypedDict, total=False):
url: Required[str]
proxy_url: str
height: int
width: int


class EmbedVideo(TypedDict, total=False):
url: str
proxy_url: str
height: int
width: int
flags: int


class EmbedImage(TypedDict, total=False):
url: Required[str]
proxy_url: str
height: int
width: int


class EmbedProvider(TypedDict, total=False):
name: str
url: str
Expand All @@ -83,9 +69,9 @@ class Embed(TypedDict, total=False):
timestamp: str
color: int
footer: EmbedFooter
image: EmbedImage
thumbnail: EmbedThumbnail
video: EmbedVideo
image: EmbedMedia
thumbnail: EmbedMedia
video: EmbedMedia
provider: EmbedProvider
author: EmbedAuthor
fields: List[EmbedField]
Expand Down

0 comments on commit cab4732

Please sign in to comment.