Allow message formatters to return full message content objects as well as strings

cshd
Dragory 2020-07-14 01:31:50 +03:00
parent 8a975d7da4
commit 662c6b0c21
No known key found for this signature in database
GPG Key ID: 5F387BA66DF8AAC1
2 changed files with 52 additions and 24 deletions

View File

@ -1,4 +1,5 @@
const moment = require('moment'); const moment = require('moment');
const Eris = require('eris');
const bot = require('../bot'); const bot = require('../bot');
const knex = require('../knex'); const knex = require('../knex');
@ -30,46 +31,73 @@ class Thread {
} }
/** /**
* @param {string} text * @param {Eris.MessageContent} text
* @param {Eris.MessageFile|Eris.MessageFile[]} file * @param {Eris.MessageFile|Eris.MessageFile[]} file
* @returns {Promise<Eris.Message>} * @returns {Promise<Eris.Message>}
* @throws Error * @throws Error
* @private * @private
*/ */
async _sendDMToUser(text, file = null) { async _sendDMToUser(content, file = null) {
// Try to open a DM channel with the user // Try to open a DM channel with the user
const dmChannel = await this.getDMChannel(); const dmChannel = await this.getDMChannel();
if (! dmChannel) { if (! dmChannel) {
throw new Error('Could not open DMs with the user. They may have blocked the bot or set their privacy settings higher.'); 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 let firstMessage;
const chunks = utils.chunk(text, 2000);
const messages = await Promise.all(chunks.map((chunk, i) => { if (typeof content === 'string') {
return dmChannel.createMessage( // Content is a string, chunk it and send it as individual messages.
chunk, // Files (attachments) are only sent with the last message.
(i === chunks.length - 1 ? file : undefined) // Only send the file with the last message const chunks = utils.chunk(content, 2000);
); for (const [i, chunk] of chunks.entries()) {
})); let msg;
return messages[0]; 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<Eris.Message>} * @param {Eris.MessageContent} content
* @param {Eris.MessageFile} file
* @return {Promise<Eris.Message|null>}
* @private * @private
*/ */
async _postToThreadChannel(...args) { async _postToThreadChannel(content, file = null) {
try { try {
if (typeof args[0] === 'string') { let firstMessage;
const chunks = utils.chunk(args[0], 2000);
const messages = await Promise.all(chunks.map((chunk, i) => { if (typeof content === 'string') {
const rest = (i === chunks.length - 1 ? args.slice(1) : []); // Only send the rest of the args (files, embeds) with the last message // Content is a string, chunk it and send it as individual messages.
return bot.createMessage(this.channel_id, chunk, ...rest); // Files (attachments) are only sent with the last message.
})); const chunks = utils.chunk(content, 2000);
return messages[0]; 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 { } 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) { } catch (e) {
// Channel not found // Channel not found
if (e.code === 10003) { if (e.code === 10003) {

View File

@ -10,7 +10,7 @@ const config = require('./config');
* @param {{ * @param {{
* isAnonymous: boolean, * isAnonymous: boolean,
* }} opts={} * }} 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 {{ * @param {{
* isAnonymous: boolean, * isAnonymous: boolean,
* }} opts={} * }} 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 {{ * @param {{
* attachmentLinks: string[], * attachmentLinks: string[],
* }} opts * }} opts
* @return {string} Message content to post in the thread channel * @return {Eris.MessageContent} Message content to post in the thread channel
*/ */
/** /**