Add Embed.__len__ to query total character size of an embed.

This commit is contained in:
Rapptz 2019-03-13 06:07:57 -04:00
parent 73aca4d4d3
commit 35a330c5d3

View File

@ -36,6 +36,9 @@ class _EmptyEmbed:
def __repr__(self): def __repr__(self):
return 'Embed.Empty' return 'Embed.Empty'
def __len__(self):
return 0
EmptyEmbed = _EmptyEmbed() EmptyEmbed = _EmptyEmbed()
class EmbedProxy: class EmbedProxy:
@ -54,6 +57,13 @@ class EmbedProxy:
class Embed: class Embed:
"""Represents a Discord embed. """Represents a Discord embed.
.. container:: operations
.. describe:: len(x)
Returns the total size of the embed.
Useful for checking if it's within the 6000 character limit.
The following attributes can be set during creation The following attributes can be set during creation
of the object: of the object:
@ -159,6 +169,27 @@ class Embed:
return self return self
def __len__(self):
total = len(self.title) + len(self.description)
for field in getattr(self, '_fields', []):
total += len(field['name']) + len(field['value'])
try:
footer = self._footer
except AttributeError:
pass
else:
total += len(footer['text'])
try:
author = self._author
except AttributeError:
pass
else:
total += len(author['name'])
return total
@property @property
def colour(self): def colour(self):
return getattr(self, '_colour', EmptyEmbed) return getattr(self, '_colour', EmptyEmbed)