mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 02:23: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
|
* @param {SplitOptions} [options] Options controlling the behaviour of the split
|
||||||
* @returns {string|string[]}
|
* @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;
|
if (text.length <= maxLength) return text;
|
||||||
const splitText = text.split(char);
|
const splitText = text.split(char);
|
||||||
if (splitText.length === 1) throw new RangeError('SPLIT_MAX_LEN');
|
if (splitText.length === 1) throw new RangeError('SPLIT_MAX_LEN');
|
||||||
const messages = [''];
|
const messages = [];
|
||||||
let msg = 0;
|
let msg = '';
|
||||||
for (let i = 0; i < splitText.length; i++) {
|
for (const chunk of splitText) {
|
||||||
if (messages[msg].length + splitText[i].length + 1 > maxLength) {
|
if (msg && (msg + char + chunk + append).length > maxLength) {
|
||||||
messages[msg] += append;
|
messages.push(msg + append);
|
||||||
messages.push(prepend);
|
msg = prepend;
|
||||||
msg++;
|
|
||||||
}
|
}
|
||||||
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