Backporting Attachments (#1817)

This commit is contained in:
SpaceEEC
2017-08-22 00:39:27 +02:00
committed by Crawl
parent f7664b01a2
commit 0b22d9a774
7 changed files with 208 additions and 36 deletions

View File

@@ -234,6 +234,33 @@ class ClientDataResolver {
return Promise.reject(new TypeError('The resource must be a string or Buffer.'));
}
/**
* @external Stream
* @see {@link https://nodejs.org/api/stream.html}
*/
/**
* 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('The resource must be a string, Buffer or a valid file stream.');
}
}) :
Promise.reject(new TypeError('The resource must be a string, Buffer or a valid file stream.'));
}
/**
* Data that can be resolved to give an emoji identifier. This can be:
* * The unicode representation of an emoji

View File

@@ -102,12 +102,12 @@ class RESTMethods {
if (content instanceof Array) {
const messages = [];
(function sendChunk(list, index) {
const options = index === list.length ? { tts, embed } : { tts };
chan.send(list[index], options, index === list.length ? files : null).then(message => {
const options = index === list.length - 1 ? { tts, embed, files } : { tts };
chan.send(list[index], options).then(message => {
messages.push(message);
if (index >= list.length - 1) return resolve(messages);
return sendChunk(list, ++index);
});
}).catch(reject);
}(content, 0));
} else {
this.rest.makeRequest('post', Endpoints.Channel(chan).messages, true, {
@@ -747,21 +747,30 @@ class RESTMethods {
false, undefined, undefined, reason);
}
sendWebhookMessage(webhook, content, { avatarURL, tts, disableEveryone, embeds, username } = {}, file = null) {
username = username || webhook.name;
if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
if (content) {
if (disableEveryone || (typeof disableEveryone === 'undefined' && this.client.options.disableEveryone)) {
content = content.replace(/@(everyone|here)/g, '@\u200b$1');
sendWebhookMessage(webhook, content, { avatarURL, tts, embeds, username } = {}, files = null) {
return new Promise((resolve, reject) => {
username = username || webhook.name;
if (content instanceof Array) {
const messages = [];
(function sendChunk(list, index) {
const options = index === list.length - 1 ? { tts, embeds, files } : { tts };
webhook.send(list[index], options).then(message => {
messages.push(message);
if (index >= list.length - 1) return resolve(messages);
return sendChunk(list, ++index);
}).catch(reject);
}(content, 0));
} else {
this.rest.makeRequest('post', `${Endpoints.Webhook(webhook.id, webhook.token)}?wait=true`, false, {
username,
avatar_url: avatarURL,
content,
tts,
embeds,
}, files).then(resolve, reject);
}
}
return this.rest.makeRequest('post', `${Endpoints.Webhook(webhook.id, webhook.token)}?wait=true`, false, {
username,
avatar_url: avatarURL,
content,
tts,
embeds,
}, file);
});
}
sendSlackWebhookMessage(webhook, body) {