diff --git a/src/dpy_commons/embeds.py b/src/dpy_commons/embeds.py index 82109d1..f42dd70 100644 --- a/src/dpy_commons/embeds.py +++ b/src/dpy_commons/embeds.py @@ -135,6 +135,22 @@ def _normalize_colour(value: object) -> "discord.Colour | None": def split_embeds(embeds: "list[discord.Embed]") -> "list[list[discord.Embed]]": - """chunk a list of embeds into groups of <= 10 (MSG_MAX_EMBEDS) so each group sends in one - message""" - return [embeds[i:i + MSG_MAX_EMBEDS] for i in range(0, len(embeds), MSG_MAX_EMBEDS)] + """chunk embeds into sendable groups bounded by BOTH the 10-per-message count and the + 6000-char combined total (EMBED_TOTAL); a new group starts before either cap is exceeded + + each embed is assumed already within its own limits (run fit_embed first); a single + embed over EMBED_TOTAL still lands alone in its own group""" + groups: "list[list[discord.Embed]]" = [] + current: "list[discord.Embed]" = [] + running = 0 + for embed in embeds: + size = len(embed) + if current and (len(current) >= MSG_MAX_EMBEDS or running + size > EMBED_TOTAL): + groups.append(current) + current = [] + running = 0 + current.append(embed) + running += size + if current: + groups.append(current) + return groups diff --git a/src/dpy_commons/text.py b/src/dpy_commons/text.py index 218ce63..f54f06b 100644 --- a/src/dpy_commons/text.py +++ b/src/dpy_commons/text.py @@ -54,7 +54,7 @@ def chunk_text(text: str, limit: int = MSG_LIMIT) -> list[str]: if limit < 1: raise ValueError(f"limit must be >= 1, got {limit}") if len(text) <= limit: - return [text] if text else [] + return [text] if text.strip() else [] chunks: list[str] = [] remaining = text @@ -65,11 +65,13 @@ def chunk_text(text: str, limit: int = MSG_LIMIT) -> list[str]: split = window.rfind(" ") if split <= 0: split = limit - chunks.append(remaining[:split]) + piece = remaining[:split] + if piece.strip(): + chunks.append(piece) remaining = remaining[split:] if remaining.startswith(("\n", " ")): remaining = remaining[1:] - if remaining: + if remaining.strip(): chunks.append(remaining) return chunks