update attachment & docs (#1815)

This commit is contained in:
Isabella
2017-08-21 16:11:58 -05:00
committed by Crawl
parent 0c0ec72cb8
commit 4520c801d3
4 changed files with 23 additions and 32 deletions

View File

@@ -202,12 +202,17 @@ class ClientDataResolver {
* @typedef {string|Buffer} BufferResolvable * @typedef {string|Buffer} BufferResolvable
*/ */
/**
* @external Stream
* @see {@link https://nodejs.org/api/stream.html}
*/
/** /**
* Resolves a BufferResolvable to a Buffer. * Resolves a BufferResolvable to a Buffer.
* @param {BufferResolvable} resource The buffer resolvable to resolve * @param {BufferResolvable|Stream} resource The buffer or stream resolvable to resolve
* @returns {Promise<Buffer>} * @returns {Promise<Buffer>}
*/ */
resolveBuffer(resource) { resolveFile(resource) {
if (resource instanceof Buffer) return Promise.resolve(resource); if (resource instanceof Buffer) return Promise.resolve(resource);
if (this.client.browser && resource instanceof ArrayBuffer) return Promise.resolve(Util.convertToBuffer(resource)); if (this.client.browser && resource instanceof ArrayBuffer) return Promise.resolve(Util.convertToBuffer(resource));
@@ -232,33 +237,18 @@ class ClientDataResolver {
}); });
} }
}); });
} else if (resource.pipe && typeof resource.pipe === 'function') {
return new Promise((resolve, reject) => {
const buffers = [];
resource.once('error', reject);
resource.on('data', data => buffers.push(data));
resource.once('end', () => resolve(Buffer.concat(buffers)));
});
} }
return Promise.reject(new TypeError('REQ_RESOURCE_TYPE')); return Promise.reject(new TypeError('REQ_RESOURCE_TYPE'));
} }
/**
* Converts a Stream to a Buffer.
* @param {Stream} resource The stream to convert
* @returns {Promise<Buffer>}
*/
resolveFile(resource) {
return resource ? this.resolveBuffer(resource)
.catch(() => {
if (resource.pipe && typeof resource.pipe === 'function') {
return new Promise((resolve, reject) => {
const buffers = [];
resource.once('error', reject);
resource.on('data', data => buffers.push(data));
resource.once('end', () => resolve(Buffer.concat(buffers)));
});
} else {
throw new TypeError('REQ_RESOURCE_TYPE');
}
}) :
Promise.reject(new TypeError('REQ_RESOURCE_TYPE'));
}
/** /**
* Data that can be resolved to give an emoji identifier. This can be: * Data that can be resolved to give an emoji identifier. This can be:
* * The unicode representation of an emoji * * The unicode representation of an emoji

View File

@@ -1,10 +1,13 @@
/** /**
* Represents an attachment in a message. * Represents an attachment in a message.
* @param {BufferResolvable|Stream} file The file
* @param {string} [name] The name of the file, if any
*/ */
class Attachment { class Attachment {
constructor(file, name) { constructor(file, name) {
this.file = null; this.file = null;
this._attach(file, name); if (name) this.setAttachment(file, name);
else this._attach(file);
} }
/** /**
@@ -42,7 +45,7 @@ class Attachment {
* @returns {Attachment} This attachment * @returns {Attachment} This attachment
*/ */
setFile(attachment) { setFile(attachment) {
this.file.attachment = attachment; this.file = { attachment };
return this; return this;
} }
@@ -63,10 +66,8 @@ class Attachment {
* @private * @private
*/ */
_attach(file, name) { _attach(file, name) {
if (file) { if (typeof file === 'string') this.file = file;
if (typeof file === 'string') this.file = file; else this.setAttachment(file, name);
else this.setAttachment(file, name);
}
} }
} }

View File

@@ -94,7 +94,7 @@ class Webhook {
/** /**
* Send a message with this 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 * @param {WebhookMessageOptions|MessageEmbed|Attachment|Attachment[]} [options={}] The options to provide
* @returns {Promise<Message|Object>} * @returns {Promise<Message|Object>}
* @example * @example
* // Send a message * // Send a message

View File

@@ -66,7 +66,7 @@ class TextBasedChannel {
/** /**
* Send a message to this channel. * Send a message to this channel.
* @param {StringResolvable} [content] Text for the message * @param {StringResolvable} [content] Text for the message
* @param {MessageOptions} [options={}] Options for the message * @param {MessageOptions|MessageEmbed|Attachment|Attachment[]} [options={}] Options for the message
* @returns {Promise<Message|Message[]>} * @returns {Promise<Message|Message[]>}
* @example * @example
* // Send a message * // Send a message