From 54e9fbd597531532e217cf0cacf3618710af28ea Mon Sep 17 00:00:00 2001 From: Dragory Date: Thu, 3 May 2018 20:33:19 +0300 Subject: [PATCH] Chunk long messages so they don't fail to send --- src/data/Thread.js | 20 ++++++++++++++++++-- src/utils.js | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/data/Thread.js b/src/data/Thread.js index 136343a..e92a805 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -176,7 +176,14 @@ class Thread { } // Send the DM - return dmChannel.createMessage(text, file); + const chunks = utils.chunk(text, 2000); + const messages = await Promise.all(chunks.map((chunk, i) => { + return dmChannel.createMessage( + chunk, + (i === chunks.length - 1 ? file : undefined) // Only send the file with the last message + ); + })); + return messages[0]; } /** @@ -184,7 +191,16 @@ class Thread { */ async postToThreadChannel(...args) { try { - return await bot.createMessage(this.channel_id, ...args); + if (typeof args[0] === 'string') { + const chunks = utils.chunk(args[0], 2000); + const messages = await Promise.all(chunks.map((chunk, i) => { + const rest = (i === chunks.length - 1 ? args.slice(1) : []); // Only send the rest of the args (files, embeds) with the last message + return bot.createMessage(this.channel_id, chunk, ...rest); + })); + return messages[0]; + } else { + return bot.createMessage(this.channel_id, ...args); + } } catch (e) { // Channel not found if (e.code === 10003) { diff --git a/src/utils.js b/src/utils.js index 91d7339..85518f8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -181,7 +181,7 @@ function getMainRole(member) { /** * Splits array items into chunks of the specified size - * @param {Array} items + * @param {Array|String} items * @param {Number} chunkSize * @returns {Array} */