splitMessage
function in meocord/common Since 4.3.0
splitMessage(text: string, options?: { max?: number }): string[]Splits text into messages Discord takes, each at most max characters, keeping code and characters whole.
A chunk ends at the last line end that fits; a line longer than a chunk breaks at its last whitespace that fits,
else at the last character that fits, never inside an emoji, a flag or a letter and its accent. The line end or
whitespace at a cut is dropped, with a run of blank lines there. Code is read as Discord's client reads it: a block
is three backticks to the next three, wherever they fall, with a language when a word of letters, digits and
_ + - . # ends the opening line and text follows it, and inline code is a run of backticks to the next run as long;
CR LF and a lone CR end a line as LF does. Code cut across chunks is closed at the end of one and
reopened at the start of the next: a block with its language, or its backticks alone when the language leaves no
room, and inline code with its own backticks; they count toward max. Each chunk reads as its part of the text: no
cut parts an escape from what it escapes, pairs backticks the text leaves unpaired, or leaves a block with no text
where the text has some. A character is cut between its code points only when no chunk can hold it whole. No chunk
is empty or only whitespace, so an empty text gives []. Lengths are string.length, as Discord's builders count
them.
Parameters
| Name | Type |
|---|---|
text | string |
The text to send. | |
options? | { max?: number } |
| |
options.max? | number |
Returns
string[]The chunks, in order.
Throws
RangeError when
maxisn't a whole number of at least 1, or is too small for code's backticks and one character between them; the message names the smallestmaxthe text needs.
Examples
declare const channel: SendableChannels
declare const log: string
for (const content of splitMessage(log)) await channel.send({ content })