standardize message object creation (#1986)

* standardize message object creation so i don't flip out again

* fix stuff

* Update Message.js

* Update index.js

* Update SendMessage.js

* Update Message.js
This commit is contained in:
Gus Caplan
2017-10-29 08:54:00 -05:00
committed by Crawl
parent 94a4a068b9
commit 29a81eab73
6 changed files with 135 additions and 244 deletions

View File

@@ -1,9 +1,5 @@
const Util = require('../util/Util');
const DataResolver = require('../util/DataResolver');
const Embed = require('./MessageEmbed');
const MessageAttachment = require('./MessageAttachment');
const MessageEmbed = require('./MessageEmbed');
const { browser } = require('../util/Constants');
const { createMessage } = require('./shared');
/**
* Represents a webhook.
@@ -98,115 +94,24 @@ class Webhook {
* .catch(console.error);
*/
/* eslint-enable max-len */
send(content, options) { // eslint-disable-line complexity
async send(content, options) { // eslint-disable-line complexity
if (!options && typeof content === 'object' && !(content instanceof Array)) {
options = content;
content = '';
content = null;
} else if (!options) {
options = {};
}
if (!options.content) options.content = content;
if (options instanceof MessageAttachment) options = { files: [options.file] };
if (options instanceof MessageEmbed) options = { embeds: [options] };
if (options.embed) options = { embeds: [options.embed] };
if (content instanceof Array || options instanceof Array) {
const which = content instanceof Array ? content : options;
const attachments = which.filter(item => item instanceof MessageAttachment);
const embeds = which.filter(item => item instanceof MessageEmbed);
if (attachments.length) options = { files: attachments };
if (embeds.length) options = { embeds };
if ((embeds.length || attachments.length) && content instanceof Array) content = '';
}
if (!options.username) options.username = this.name;
if (options.avatarURL) {
options.avatar_url = options.avatarURL;
options.avatarURL = null;
}
if (content) {
content = Util.resolveString(content);
let { split, code, disableEveryone } = options;
if (split && typeof split !== 'object') split = {};
if (typeof code !== 'undefined' && (typeof code !== 'boolean' || code === true)) {
content = Util.escapeMarkdown(content, true);
content = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n${content}\n\`\`\``;
if (split) {
split.prepend = `\`\`\`${typeof code !== 'boolean' ? code || '' : ''}\n`;
split.append = '\n```';
}
}
if (disableEveryone || (typeof disableEveryone === 'undefined' && this.client.options.disableEveryone)) {
content = content.replace(/@(everyone|here)/g, '@\u200b$1');
}
if (split) content = Util.splitMessage(content, split);
}
options.content = content;
if (options.embeds) options.embeds = options.embeds.map(embed => new Embed(embed)._apiTransform());
if (options.files) {
for (let i = 0; i < options.files.length; i++) {
let file = options.files[i];
if (typeof file === 'string' || (!browser && Buffer.isBuffer(file))) file = { attachment: file };
if (!file.name) {
if (typeof file.attachment === 'string') {
file.name = Util.basename(file.attachment);
} else if (file.attachment && file.attachment.path) {
file.name = Util.basename(file.attachment.path);
} else if (file instanceof MessageAttachment) {
file = { attachment: file.file, name: Util.basename(file.file) || 'file.jpg' };
} else {
file.name = 'file.jpg';
}
} else if (file instanceof MessageAttachment) {
file = file.file;
}
options.files[i] = file;
}
return Promise.all(options.files.map(file =>
DataResolver.resolveFile(file.attachment).then(resource => {
file.file = resource;
return file;
})
)).then(files => this.client.api.webhooks(this.id, this.token).post({
data: options,
query: { wait: true },
files,
auth: false,
}));
}
if (content instanceof Array) {
return new Promise((resolve, reject) => {
const messages = [];
(function sendChunk() {
const opt = content.length ? null : { embeds: options.embeds, files: options.files };
this.client.api.webhooks(this.id, this.token).post({
data: { content: content.shift(), opt },
query: { wait: true },
auth: false,
})
.then(message => {
messages.push(message);
if (content.length === 0) return resolve(messages);
return sendChunk.call(this);
})
.catch(reject);
}.call(this));
});
}
const { data, files } = await createMessage(this, options);
return this.client.api.webhooks(this.id, this.token).post({
data: options,
data, files,
query: { wait: true },
auth: false,
}).then(data => {
if (!this.client.channels) return data;
return this.client.channels.get(data.channel_id).messages.create(data, false);
}).then(d => {
if (!this.client.channels) return d;
return this.client.channels.get(d.channel_id).messages.create(d, false);
});
}