From 72dfbf3feec3e39b3b0929e24ec50c925c552b3f Mon Sep 17 00:00:00 2001 From: SpaceEEC Date: Tue, 2 May 2017 07:58:42 +0200 Subject: [PATCH] Fix for sending files with Webhook class (#1449) --- src/structures/Webhook.js | 41 ++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index 4d96382acb3a..509de2e66985 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -81,6 +81,7 @@ class Webhook { * @property {boolean} [disableEveryone=this.client.options.disableEveryone] Whether or not @everyone and @here * should be replaced with plain-text * @property {FileOptions|string} [file] A file to send with the message + * @property {FileOptions[]|string[]} [files] Files to send with the message * @property {string|boolean} [code] Language for optional codeblock formatting to apply * @property {boolean|SplitOptions} [split=false] Whether or not the message should be split into multiple messages if * it exceeds the character limit. If an object is provided, these are the options for splitting the message. @@ -88,7 +89,7 @@ class Webhook { /** * Send a message with this webhook. - * @param {StringResolvable} content The content to send + * @param {StringResolvable} [content] The content to send * @param {WebhookMessageOptions} [options={}] The options to provide * @returns {Promise} * @example @@ -104,24 +105,36 @@ class Webhook { } else if (!options) { options = {}; } + if (options.file) { - if (typeof options.file === 'string') options.file = { attachment: options.file }; - if (!options.file.name) { - if (typeof options.file.attachment === 'string') { - options.file.name = path.basename(options.file.attachment); - } else if (options.file.attachment && options.file.attachment.path) { - options.file.name = path.basename(options.file.attachment.path); - } else { - options.file.name = 'file.jpg'; + if (options.files) options.files.push(options.file); + else options.files = [options.file]; + } + + if (options.files) { + for (const i in options.files) { + let file = options.files[i]; + if (typeof file === 'string') file = { attachment: file }; + if (!file.name) { + if (typeof file.attachment === 'string') { + file.name = path.basename(file.attachment); + } else if (file.attachment && file.attachment.path) { + file.name = path.basename(file.attachment.path); + } else { + file.name = 'file.jpg'; + } } + options.files[i] = file; } - return this.client.resolver.resolveBuffer(options.file.attachment).then(file => - this.client.rest.methods.sendWebhookMessage(this, content, options, { - file, - name: options.file.name, + + return Promise.all(options.files.map(file => + this.client.resolver.resolveBuffer(file.attachment).then(buffer => { + file.file = buffer; + return file; }) - ); + )).then(files => this.client.rest.methods.sendWebhookMessage(this, content, options, files)); } + return this.client.rest.methods.sendWebhookMessage(this, content, options); }