From 05c608878a0f75aacff9421f4729c35e7fa5602a Mon Sep 17 00:00:00 2001 From: "C. Eric Mathey" Date: Tue, 12 Sep 2023 17:11:29 -0600 Subject: [PATCH] Add __len__ support to embeds --- discord_webhook/webhook.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/discord_webhook/webhook.py b/discord_webhook/webhook.py index 25a4ae0..1737d98 100644 --- a/discord_webhook/webhook.py +++ b/discord_webhook/webhook.py @@ -66,6 +66,26 @@ def __init__( if timestamp := kwargs.get("timestamp"): self.set_timestamp(timestamp) + def __len__(self) -> int: + """Return the total length in characters of the embed + + https://discord.com/developers/docs/resources/channel#embed-limits + Discord imposes a 6,000 character limit on embeds + """ + + total_length = 0 + + if self.fields: + for field, value in self.fields: + total_length += len(field) + len(value) + + total_length += len(self.title or "") + total_length += len(self.description or "") + total_length += len(self.footer or "") + total_length += len(self.author or "") + + return total_length + def set_title(self, title: str) -> None: """ Set the title of the embed.