diff --git a/src/util/Util.js b/src/util/Util.js index 5eb6e6dfe..5cd7ed065 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -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); } /**