Fix for sending files with Webhook class (#1449)

This commit is contained in:
SpaceEEC
2017-05-02 07:58:42 +02:00
committed by Amish Shah
parent a7c902c6cf
commit 07623d49e2

View File

@@ -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<Message|Message[]>}
* @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';
}
}
return this.client.resolver.resolveBuffer(options.file.attachment).then(file =>
this.client.rest.methods.sendWebhookMessage(this, content, options, {
file,
name: options.file.name,
})
);
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 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);
}