aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2019-03-13 06:07:57 -0400
committerRapptz <[email protected]>2019-03-13 06:07:57 -0400
commit35a330c5d3acfc5a6d7348ea916f4fe59bbcb98c (patch)
treebcadd563677ff2572cce38cb4ec010f356f3e27e
parentExpose Embed.from_data as Embed.from_dict (diff)
downloaddiscord.py-35a330c5d3acfc5a6d7348ea916f4fe59bbcb98c.tar.xz
discord.py-35a330c5d3acfc5a6d7348ea916f4fe59bbcb98c.zip
Add Embed.__len__ to query total character size of an embed.
-rw-r--r--discord/embeds.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/discord/embeds.py b/discord/embeds.py
index 1b0c70d0..663e9ea0 100644
--- a/discord/embeds.py
+++ b/discord/embeds.py
@@ -36,6 +36,9 @@ class _EmptyEmbed:
def __repr__(self):
return 'Embed.Empty'
+ def __len__(self):
+ return 0
+
EmptyEmbed = _EmptyEmbed()
class EmbedProxy:
@@ -54,6 +57,13 @@ class EmbedProxy:
class 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
of the object:
@@ -159,6 +169,27 @@ class Embed:
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
def colour(self):
return getattr(self, '_colour', EmptyEmbed)