"""Text chunking utilities for sending long content within Discord's limit.""" from __future__ import annotations def split_message(text: str, limit: int = 1900) -> list[str]: """Split ``text`` into chunks of at most ``limit`` characters. Splits on newlines first so a line is never broken mid-text when avoidable; a single line longer than ``limit`` is hard-split by plain code-point slicing (Python ``str`` slicing is code-point safe, so no lone surrogates are produced). Multi-codepoint grapheme clusters (e.g. some emoji) are not protected — this matches the previous behavior and keeps the dependency footprint at zero. Guarantees ``"".join(split_message(text, limit)) == text``. """ if not text: return [] if limit <= 0: raise ValueError("limit must be a positive integer") chunks: list[str] = [] current = "" for raw_line in text.splitlines(keepends=True): line = raw_line while len(line) > limit: if current: chunks.append(current) current = "" chunks.append(line[:limit]) line = line[limit:] if len(current) + len(line) <= limit: current += line else: if current: chunks.append(current) current = line if current: chunks.append(current) return chunks