fix: split_embeds bounds groups by the 6000-char total too; chunk_text drops whitespace-only chunks
split_embeds chunked only by the 10-per-message count, so several individually-fitted embeds whose combined length exceeded 6000 still 400'd (BASE_TYPE_MAX_LENGTH), breaking the 'never fails on a discord limit' contract; it now starts a new group before either the count OR the 6000 total is exceeded. chunk_text could emit a whitespace-only piece (e.g. '\n') when the only split boundary sat at index 1, which safe_send then sent as empty content discord rejects (50006); whitespace-only pieces are now skipped. (live 400/50006 need discord; the split/chunk logic is verified offline.) Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user