mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Make Util#splitMessage handle edge cases properly (#2212)
* Make Util#splitMessage handle edge cases properly * Restart Travis * Set maxLength to 2000 + small tweak
This commit is contained in:
@@ -18,21 +18,20 @@ class Util {
|
||||
* @param {SplitOptions} [options] Options controlling the behaviour of the split
|
||||
* @returns {string|string[]}
|
||||
*/
|
||||
static splitMessage(text, { maxLength = 1950, char = '\n', prepend = '', append = '' } = {}) {
|
||||
static splitMessage(text, { maxLength = 2000, char = '\n', prepend = '', append = '' } = {}) {
|
||||
if (text.length <= maxLength) return text;
|
||||
const splitText = text.split(char);
|
||||
if (splitText.length === 1) throw new RangeError('SPLIT_MAX_LEN');
|
||||
const messages = [''];
|
||||
let msg = 0;
|
||||
for (let i = 0; i < splitText.length; i++) {
|
||||
if (messages[msg].length + splitText[i].length + 1 > maxLength) {
|
||||
messages[msg] += append;
|
||||
messages.push(prepend);
|
||||
msg++;
|
||||
const messages = [];
|
||||
let msg = '';
|
||||
for (const chunk of splitText) {
|
||||
if (msg && (msg + char + chunk + append).length > maxLength) {
|
||||
messages.push(msg + append);
|
||||
msg = prepend;
|
||||
}
|
||||
messages[msg] += (messages[msg].length > 0 && messages[msg] !== prepend ? char : '') + splitText[i];
|
||||
msg += (msg && msg !== prepend ? char : '') + chunk;
|
||||
}
|
||||
return messages.filter(m => m);
|
||||
return messages.concat(msg).filter(m => m);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user