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

@@ -1,6 +1,6 @@
const DataResolver = require('../util/DataResolver');
const Channel = require('./Channel');
const { createMessage } = require('./shared');
const APIMessage = require('./APIMessage');
/**
* Represents a webhook.
@@ -82,11 +82,10 @@ class Webhook {
* it exceeds the character limit. If an object is provided, these are the options for splitting the message.
*/
/* eslint-disable max-len */
/**
* Sends a message with this webhook.
* @param {StringResolvable} [content] The content to send
* @param {WebhookMessageOptions|MessageEmbed|MessageAttachment|MessageAttachment[]} [options={}] The options to provide
* @param {StringResolvable} [content=''] The content to send
* @param {WebhookMessageOptions|MessageAdditions} [options={}] The options to provide
* @returns {Promise<Message|Object>}
* @example
* // Send a basic message
@@ -127,20 +126,18 @@ class Webhook {
* .catch(console.error);
*/
async send(content, options) {
if (!options && typeof content === 'object' && !(content instanceof Array)) {
options = content;
content = null;
} else if (!options) {
options = {};
}
if (!options.content) options.content = content;
const { data, files } = await createMessage(this, options);
const apiMessage = APIMessage.create(this, content, options);
const data = apiMessage.resolveData();
if (data.content instanceof Array) {
const messages = [];
for (let i = 0; i < data.content.length; i++) {
const opt = i === data.content.length - 1 ? { embeds: data.embeds, files } : {};
let opt;
if (i === data.content.length - 1) {
opt = { embeds: data.embeds, files: apiMessage.options.files };
} else {
opt = {};
}
Object.assign(opt, { avatarURL: data.avatar_url, content: data.content[i], username: data.username });
// eslint-disable-next-line no-await-in-loop
const message = await this.send(data.content[i], opt);
@@ -149,7 +146,7 @@ class Webhook {
return messages;
}
const files = await apiMessage.resolveFiles();
return this.client.api.webhooks(this.id, this.token).post({
data, files,
query: { wait: true },