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

Add EmbedField object to allow for easier embed class instance creation #1181

Merged
merged 29 commits into from
Apr 17, 2022
Merged
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
cfb8083
add EmbedField object to allow for easier embed class instance creation
krittick Mar 18, 2022
4bcca9b
Merge branch 'master' into embed-fields
krittick Mar 20, 2022
772d0e6
add TypeError if `fields` setter is passed a list containing anything…
krittick Mar 20, 2022
53ba029
initial pass at removing proxy object reliance from embed fields
krittick Mar 20, 2022
a16c316
Merge branch 'Pycord-Development:master' into embed-fields
krittick Apr 1, 2022
c4ed8eb
Merge branch 'Pycord-Development:master' into embed-fields
krittick Apr 8, 2022
4b6c23a
Merge branch 'master' into embed-fields
krittick Apr 8, 2022
22e1702
Merge branch 'master' into embed-fields
krittick Apr 9, 2022
ab18423
add self._fields to Embed.__init__ (prevents AttributeError)
krittick Apr 9, 2022
a1d8b19
add EmbedField to __all__
krittick Apr 9, 2022
70a7942
fix init loop
krittick Apr 9, 2022
b508b46
fix init loop
krittick Apr 9, 2022
aadebba
fix to_dict for _fields attr
krittick Apr 9, 2022
b4081a6
remove now-unused _EmbedFieldProxy class
krittick Apr 9, 2022
571a650
add from_dict classmethod to EmbedField for better handling with Embe…
krittick Apr 9, 2022
50d3e35
update EmbedField.from_dict to more closely match the behavior of Emb…
krittick Apr 9, 2022
c1c0d18
add Embed.append_field option to allow directly adding EmbedField obj…
krittick Apr 9, 2022
068c86d
add EmbedField to docs
krittick Apr 9, 2022
dacdfe0
doc fix
krittick Apr 9, 2022
fb9a701
add docstring for EmbedField.to_dict
krittick Apr 9, 2022
0c097cf
fix for embeds with no initial fields
krittick Apr 12, 2022
870d82b
revert
krittick Apr 12, 2022
1e53637
fix for copying embeds with no fields
krittick Apr 12, 2022
408e7bd
Merge branch 'master' into embed-fields
krittick Apr 13, 2022
85bacab
Merge branch 'master' into embed-fields
krittick Apr 15, 2022
bcc106c
Merge branch 'master' into embed-fields
krittick Apr 16, 2022
1f4ec44
Merge branch 'master' into embed-fields
krittick Apr 17, 2022
6c113fd
add versionadded to docstrings
krittick Apr 17, 2022
0fda011
Merge branch 'master' into embed-fields
krittick Apr 17, 2022
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
59 changes: 55 additions & 4 deletions discord/embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
Final,
List,
Mapping,
Optional,
Protocol,
Type,
TypeVar,
Expand Down Expand Up @@ -114,6 +115,32 @@ class _EmbedAuthorProxy(Protocol):
proxy_icon_url: MaybeEmpty[str]


class EmbedField:
"""Represents a field on the :class:`Embed` object.

Attributes
----------
name: :class:`str`
The name of the field.
value: :class:`str`
The value of the field.
inline: :class:`bool`
Whether the field should be displayed inline.
"""

def __init__(self, name: str, value: str, inline: Optional[bool] = False):
self.name = name
self.value = value
self.inline = inline

def to_dict(self) -> Dict[str, Union[str, bool]]:
return {
"name": self.name,
"value": self.value,
"inline": self.inline,
}


class Embed:
"""Represents a Discord embed.

Expand Down Expand Up @@ -195,6 +222,7 @@ def __init__(
url: MaybeEmpty[Any] = EmptyEmbed,
description: MaybeEmpty[Any] = EmptyEmbed,
timestamp: datetime.datetime = None,
fields: List[EmbedField] = None,
):

self.colour = colour if colour is not EmptyEmbed else color
Expand All @@ -215,6 +243,10 @@ def __init__(
if timestamp:
self.timestamp = timestamp

if fields:
for field in fields:
self.add_field(name=field.name, value=field.value, inline=field.inline)

@classmethod
def from_dict(cls: Type[E], data: Mapping[str, Any]) -> E:
"""Converts a :class:`dict` to a :class:`Embed` provided it is in the
Expand Down Expand Up @@ -606,14 +638,33 @@ def remove_author(self: E) -> E:
return self

@property
def fields(self) -> List[_EmbedFieldProxy]:
"""List[Union[``EmbedProxy``, :attr:`Empty`]]: Returns a :class:`list` of ``EmbedProxy`` denoting the field contents.
def fields(self) -> Optional[List[Dict[str, Union[bool, str]]]]:
"""Returns a :class:`list` of :class:`EmbedField` objects denoting the field contents.

See :meth:`add_field` for possible values you can access.

If the attribute has no value then :attr:`Empty` is returned.
If the attribute has no value then ``None`` is returned.
"""
if self._fields:
return self._fields
else:
return None

@fields.setter
def fields(self, value: List[EmbedField]) -> None:
"""Sets the fields for the embed. This overwrites any existing fields.

Parameters
----------
value: List[:class:`EmbedField`]
The list of :class:`EmbedField` objects to include in the embed.
"""
return [EmbedProxy(d) for d in getattr(self, "_fields", [])] # type: ignore
if not all(isinstance(x, EmbedField) for x in value):
raise TypeError("Expected a list of EmbedField objects.")

self.clear_fields()
for field in value:
self.add_field(name=field.name, value=field.value, inline=field.inline)

def add_field(self: E, *, name: Any, value: Any, inline: bool = True) -> E:
"""Adds a field to the embed object.
Expand Down