From 662c6b0c2167704f30a126ef2d07bbde5637c05e Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Tue, 14 Jul 2020 01:31:50 +0300 Subject: [PATCH] Allow message formatters to return full message content objects as well as strings --- src/data/Thread.js | 70 ++++++++++++++++++++++++++++++++-------------- src/formatters.js | 6 ++-- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/data/Thread.js b/src/data/Thread.js index 3e5faf9..13501fe 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -1,4 +1,5 @@ const moment = require('moment'); +const Eris = require('eris'); const bot = require('../bot'); const knex = require('../knex'); @@ -30,46 +31,73 @@ class Thread { } /** - * @param {string} text + * @param {Eris.MessageContent} text * @param {Eris.MessageFile|Eris.MessageFile[]} file * @returns {Promise} * @throws Error * @private */ - async _sendDMToUser(text, file = null) { + async _sendDMToUser(content, file = null) { // Try to open a DM channel with the user const dmChannel = await this.getDMChannel(); if (! dmChannel) { throw new Error('Could not open DMs with the user. They may have blocked the bot or set their privacy settings higher.'); } - // Send the DM - 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]; + let firstMessage; + + if (typeof content === 'string') { + // Content is a string, chunk it and send it as individual messages. + // Files (attachments) are only sent with the last message. + const chunks = utils.chunk(content, 2000); + for (const [i, chunk] of chunks.entries()) { + let msg; + if (i === chunks.length - 1) { + // Only send embeds, files, etc. with the last message + msg = await dmChannel.createMessage(chunk, file); + } else { + msg = await dmChannel.createMessage(chunk); + } + + firstMessage = firstMessage || msg; + } + } else { + // Content is a full message content object, send it as-is with the files (if any) + firstMessage = await dmChannel.createMessage(content, file); + } + + return firstMessage; } /** - * @returns {Promise} + * @param {Eris.MessageContent} content + * @param {Eris.MessageFile} file + * @return {Promise} * @private */ - async _postToThreadChannel(...args) { + async _postToThreadChannel(content, file = null) { try { - 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]; + let firstMessage; + + if (typeof content === 'string') { + // Content is a string, chunk it and send it as individual messages. + // Files (attachments) are only sent with the last message. + const chunks = utils.chunk(content, 2000); + for (const [i, chunk] of chunks.entries()) { + let msg; + if (i === chunks.length - 1) { + // Only send embeds, files, etc. with the last message + msg = await bot.createMessage(this.channel_id, chunk, file); + } + + firstMessage = firstMessage || msg; + } } else { - return bot.createMessage(this.channel_id, ...args); + // Content is a full message content object, send it as-is with the files (if any) + firstMessage = await bot.createMessage(this.channel_id, content, file); } + + return firstMessage; } catch (e) { // Channel not found if (e.code === 10003) { diff --git a/src/formatters.js b/src/formatters.js index 15b5660..4230e57 100644 --- a/src/formatters.js +++ b/src/formatters.js @@ -10,7 +10,7 @@ const config = require('./config'); * @param {{ * isAnonymous: boolean, * }} opts={} - * @return {string} Message content to send as a DM + * @return {Eris.MessageContent} Message content to send as a DM */ /** @@ -22,7 +22,7 @@ const config = require('./config'); * @param {{ * isAnonymous: boolean, * }} opts={} - * @return {string} Message content to post in the thread channel + * @return {Eris.MessageContent} Message content to post in the thread channel */ /** @@ -45,7 +45,7 @@ const config = require('./config'); * @param {{ * attachmentLinks: string[], * }} opts - * @return {string} Message content to post in the thread channel + * @return {Eris.MessageContent} Message content to post in the thread channel */ /**