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 __len__ support to embeds #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions discord_webhook/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down