Skip to content

Commit

Permalink
Fix for sending files with Webhook class (discordjs#1449)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC authored and Ratismal committed Jul 5, 2017
1 parent a30efa4 commit 72dfbf3
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ 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.
*/

/**
* 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<Message|Message[]>}
* @example
Expand All @@ -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);
}

Expand Down

0 comments on commit 72dfbf3

Please sign in to comment.