From 05a41b5ca44cfa39db2413284a93bd34c402806d Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Mon, 6 Nov 2017 02:42:24 +0100 Subject: [PATCH] fix(Split/Webhook): readd message chunk sending and fix webhook avatar/username (#2085) --- src/structures/Webhook.js | 13 +++++++++++++ src/structures/shared/CreateMessage.js | 11 ++++------- src/structures/shared/SendMessage.js | 11 +++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index 3601b9ca4541..7ee44745e3b7 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -105,6 +105,19 @@ class Webhook { const { data, files } = await createMessage(this, options); + if (data.content instanceof Array) { + const messages = []; + for (let i = 0; i < data.content.length; i++) { + const opt = i === data.content.length - 1 ? { embeds: data.embeds, files } : {}; + Object.assign(opt, { avatarURL: data.avatar_url, content: data.content[i], username: data.username }); + // eslint-disable-next-line no-await-in-loop + const message = await this.send(data.content[i], opt); + messages.push(message); + } + return messages; + } + + return this.client.api.webhooks(this.id, this.token).post({ data, files, query: { wait: true }, diff --git a/src/structures/shared/CreateMessage.js b/src/structures/shared/CreateMessage.js index 65c7c2ca3d6c..5abf0799e191 100644 --- a/src/structures/shared/CreateMessage.js +++ b/src/structures/shared/CreateMessage.js @@ -10,8 +10,9 @@ module.exports = async function createMessage(channel, options) { const User = require('../User'); const GuildMember = require('../GuildMember'); const Webhook = require('../Webhook'); + const WebhookClient = require('../../client/WebhookClient'); - const webhook = channel instanceof Webhook; + const webhook = channel instanceof Webhook || channel instanceof WebhookClient; if (typeof options.nonce !== 'undefined') { options.nonce = parseInt(options.nonce); @@ -88,15 +89,11 @@ module.exports = async function createMessage(channel, options) { return file; }) )); - delete options.files; } if (webhook) { if (!options.username) options.username = this.name; - if (options.avatarURL) { - options.avatar_url = options.avatarURL; - options.avatarURL = null; - } + if (options.avatarURL) options.avatar_url = options.avatarURL; } return { data: { @@ -106,6 +103,6 @@ module.exports = async function createMessage(channel, options) { embed: options.embed, embeds: options.embeds, username: options.username, - avatar_url: options.avatarURL, + avatar_url: options.avatar_url, }, files }; }; diff --git a/src/structures/shared/SendMessage.js b/src/structures/shared/SendMessage.js index 5007a0af5d1d..95ea49ca3599 100644 --- a/src/structures/shared/SendMessage.js +++ b/src/structures/shared/SendMessage.js @@ -7,6 +7,17 @@ module.exports = async function sendMessage(channel, options) { // eslint-disabl const { data, files } = await createMessage(channel, options); + if (data.content instanceof Array) { + const messages = []; + for (let i = 0; i < data.content.length; i++) { + const opt = i === data.content.length - 1 ? { tts: data.tts, embed: data.embed, files } : { tts: data.tts }; + // eslint-disable-next-line no-await-in-loop + const message = await channel.send(data.content[i], opt); + messages.push(message); + } + return messages; + } + return channel.client.api.channels[channel.id].messages.post({ data, files }) .then(d => channel.client.actions.MessageCreate.handle(d).message); };