refactor: rewrite message creation (#2774)

* Rework createMessage
- MessageAttachment is now structurally similar to FileOptions
- No longer mutates the object passed as options
- Supports more permutations of arguments

* Ignore complexity warning

* Refactor name finding

* Fix typo

* Update typings

* Default name to null for MessageAttachment

* Make Message#reply use transformOptions

* Move transformOptions

* Fix Message#reply

* Fix mutation

* Update tests

* Fix options passing

* Refactor into APIMessage

* Fix webhook send

* Expose APIMessage

* Add documentation

* Add types

* Fix type doc

* Fix another type doc

* Fix another another type doc (is this one even right!?)

* Remove trailing comma

* Properly clone split options

* Add support for sending file as stream

* Missed a doc

* Resolve files only once when splitting messages

* This looks nicer

* Assign directly

* Don't cache data and files

* Missing return type

* Use object spread instead Object.assign

* Document constructors

* Crawl is a little dot

* comp pls

* tests: sanitize local file path, disable no-await-in-loop
This commit is contained in:
1Computer1
2018-08-21 12:22:29 -04:00
committed by Crawl
parent 55c58b60e7
commit 19c298f5cc
14 changed files with 551 additions and 284 deletions

View File

@@ -2,77 +2,41 @@ const Util = require('../util/Util');
/**
* Represents an attachment in a message.
* @param {BufferResolvable|Stream} file The file
* @param {string} [name] The name of the file, if any
*/
class MessageAttachment {
constructor(file, name, data) {
this.file = null;
/**
* @param {BufferResolvable|Stream} attachment The file
* @param {string} [name=null] The name of the file, if any
* @param {Object} [data] Extra data
*/
constructor(attachment, name = null, data) {
this.attachment = attachment;
this.name = name;
if (data) this._patch(data);
if (name) this.setAttachment(file, name);
else this._attach(file);
}
/**
* The name of the file
* @type {?string}
* @readonly
*/
get name() {
return this.file.name;
}
/**
* The file
* @type {?BufferResolvable|Stream}
* @readonly
*/
get attachment() {
return this.file.attachment;
}
/**
* Sets the file of this attachment.
* @param {BufferResolvable|Stream} file The file
* @param {string} name The name of the file
* @returns {MessageAttachment} This attachment
*/
setAttachment(file, name) {
this.file = { attachment: file, name };
* Sets the file of this attachment.
* @param {BufferResolvable|Stream} attachment The file
* @param {string} [name=null] The name of the file, if any
* @returns {MessageAttachment} This attachment
*/
setFile(attachment, name = null) {
this.attachment = attachment;
this.name = name;
return this;
}
/**
* Sets the file of this attachment.
* @param {BufferResolvable|Stream} attachment The file
* @returns {MessageAttachment} This attachment
*/
setFile(attachment) {
this.file = { attachment };
return this;
}
/**
* Sets the name of this attachment.
* @param {string} name The name of the image
* @returns {MessageAttachment} This attachment
*/
* Sets the name of this attachment.
* @param {string} name The name of the file
* @returns {MessageAttachment} This attachment
*/
setName(name) {
this.file.name = name;
this.name = name;
return this;
}
/**
* Sets the file of this attachment.
* @param {BufferResolvable|Stream} file The file
* @param {string} name The name of the file
* @private
*/
_attach(file, name) {
if (typeof file === 'string') this.file = file;
else this.setAttachment(file, name);
}
_patch(data) {
/**
* The ID of this attachment