mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 02:23:31 +01:00
add multi-file support (#1268)
* add multi-file support * (╯°□°)╯︵ ┻━┻ (╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻(╯°□°)╯︵ ┻━┻ * stupid git web rebase editor - Gus2k17 * Update TextBasedChannel.js * Update TextBasedChannel.js
This commit is contained in:
@@ -2,13 +2,13 @@ const request = require('superagent');
|
|||||||
const Constants = require('../../util/Constants');
|
const Constants = require('../../util/Constants');
|
||||||
|
|
||||||
class APIRequest {
|
class APIRequest {
|
||||||
constructor(rest, method, url, auth, data, file) {
|
constructor(rest, method, url, auth, data, files) {
|
||||||
this.rest = rest;
|
this.rest = rest;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.auth = auth;
|
this.auth = auth;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.file = file;
|
this.files = files;
|
||||||
this.route = this.getRoute(this.url);
|
this.route = this.getRoute(this.url);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,8 +34,8 @@ class APIRequest {
|
|||||||
gen() {
|
gen() {
|
||||||
const apiRequest = request[this.method](this.url);
|
const apiRequest = request[this.method](this.url);
|
||||||
if (this.auth) apiRequest.set('authorization', this.getAuth());
|
if (this.auth) apiRequest.set('authorization', this.getAuth());
|
||||||
if (this.file && this.file.file) {
|
if (this.files) {
|
||||||
apiRequest.attach('file', this.file.file, this.file.name);
|
for (const file of this.files) if (file && file.file) apiRequest.attach(file.name, file.file, file.name);
|
||||||
this.data = this.data || {};
|
this.data = this.data || {};
|
||||||
apiRequest.field('payload_json', JSON.stringify(this.data));
|
apiRequest.field('payload_json', JSON.stringify(this.data));
|
||||||
} else if (this.data) {
|
} else if (this.data) {
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class RESTMethods {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(channel, content, { tts, nonce, embed, disableEveryone, split, code, reply } = {}, file = null) {
|
sendMessage(channel, content, { tts, nonce, embed, disableEveryone, split, code, reply } = {}, files = null) {
|
||||||
return new Promise((resolve, reject) => { // eslint-disable-line complexity
|
return new Promise((resolve, reject) => { // eslint-disable-line complexity
|
||||||
if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
|
if (typeof content !== 'undefined') content = this.client.resolver.resolveString(content);
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ class RESTMethods {
|
|||||||
const messages = [];
|
const messages = [];
|
||||||
(function sendChunk(list, index) {
|
(function sendChunk(list, index) {
|
||||||
const options = index === list.length ? { tts, embed } : { tts };
|
const options = index === list.length ? { tts, embed } : { tts };
|
||||||
chan.send(list[index], options, index === list.length ? file : null).then(message => {
|
chan.send(list[index], options, index === list.length ? files : null).then(message => {
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
if (index >= list.length - 1) return resolve(messages);
|
if (index >= list.length - 1) return resolve(messages);
|
||||||
return sendChunk(list, ++index);
|
return sendChunk(list, ++index);
|
||||||
@@ -108,7 +108,7 @@ class RESTMethods {
|
|||||||
} else {
|
} else {
|
||||||
this.rest.makeRequest('post', Constants.Endpoints.channelMessages(chan.id), true, {
|
this.rest.makeRequest('post', Constants.Endpoints.channelMessages(chan.id), true, {
|
||||||
content, tts, nonce, embed,
|
content, tts, nonce, embed,
|
||||||
}, file).then(data => resolve(this.client.actions.MessageCreate.handle(data).message), reject);
|
}, files).then(data => resolve(this.client.actions.MessageCreate.handle(data).message), reject);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ class TextBasedChannel {
|
|||||||
* (see [here](https://discordapp.com/developers/docs/resources/channel#embed-object) for more details)
|
* (see [here](https://discordapp.com/developers/docs/resources/channel#embed-object) for more details)
|
||||||
* @property {boolean} [disableEveryone=this.client.options.disableEveryone] Whether or not @everyone and @here
|
* @property {boolean} [disableEveryone=this.client.options.disableEveryone] Whether or not @everyone and @here
|
||||||
* should be replaced with plain-text
|
* should be replaced with plain-text
|
||||||
* @property {FileOptions|string} [file] A file to send with the message
|
* @property {FileOptions|string} [file] A file to send with the message **(deprecated)**
|
||||||
|
* @property {FileOptions[]|string[]} [files] Files to send with the message
|
||||||
* @property {string|boolean} [code] Language for optional codeblock formatting to apply
|
* @property {string|boolean} [code] Language for optional codeblock formatting to apply
|
||||||
* @property {boolean|SplitOptions} [split=false] Whether or not the message should be split into multiple messages if
|
* @property {boolean|SplitOptions} [split=false] Whether or not the message should be split into multiple messages if
|
||||||
* it exceeds the character limit. If an object is provided, these are the options for splitting the message.
|
* it exceeds the character limit. If an object is provided, these are the options for splitting the message.
|
||||||
@@ -78,26 +79,34 @@ class TextBasedChannel {
|
|||||||
options = {};
|
options = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.embed && options.embed.file) options.file = options.embed.file;
|
// backward compat
|
||||||
|
|
||||||
if (options.file) {
|
if (options.file) {
|
||||||
if (typeof options.file === 'string') options.file = { attachment: options.file };
|
if (options.files) options.files.push(options.file);
|
||||||
if (!options.file.name) {
|
else options.files = [options.file];
|
||||||
if (typeof options.file.attachment === 'string') {
|
}
|
||||||
options.file.name = path.basename(options.file.attachment);
|
|
||||||
} else if (options.file.attachment && options.file.attachment.path) {
|
if (options.files) {
|
||||||
options.file.name = path.basename(options.file.attachment.path);
|
for (const i in options.files) {
|
||||||
} else {
|
let file = options.files[i];
|
||||||
options.file.name = 'file.jpg';
|
if (typeof file === 'string') file = { attachment: file };
|
||||||
|
if (!file.name) {
|
||||||
|
if (typeof file.attachment === 'string') {
|
||||||
|
file.name = path.basename(file.attachment);
|
||||||
|
} else if (file.attachment && file.attachment.path) {
|
||||||
|
file.name = path.basename(file.attachment.path);
|
||||||
|
} else {
|
||||||
|
file.name = 'file.jpg';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
options.files[i] = file;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.client.resolver.resolveBuffer(options.file.attachment).then(file =>
|
return Promise.all(options.files.map(file =>
|
||||||
this.client.rest.methods.sendMessage(this, content, options, {
|
this.client.resolver.resolveBuffer(file.attachment).then(buffer => {
|
||||||
file,
|
file.file = buffer;
|
||||||
name: options.file.name,
|
return file;
|
||||||
})
|
})
|
||||||
);
|
)).then(files => this.client.rest.methods.sendMessage(this, content, options, files));
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.client.rest.methods.sendMessage(this, content, options);
|
return this.client.rest.methods.sendMessage(this, content, options);
|
||||||
@@ -135,6 +144,17 @@ class TextBasedChannel {
|
|||||||
return this.send(content, Object.assign(options, { embed }));
|
return this.send(content, Object.assign(options, { embed }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send files to this channel
|
||||||
|
* @param {FileOptions[]|string[]} files Files to send with the message
|
||||||
|
* @param {StringResolvable} [content] Text for the message
|
||||||
|
* @param {MessageOptions} [options] Options for the message
|
||||||
|
* @returns {Promise<Message>}
|
||||||
|
*/
|
||||||
|
sendFiles(files, content, options) {
|
||||||
|
return this.send(content, Object.assign(options, { files }));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a file to this channel
|
* Send a file to this channel
|
||||||
* @param {BufferResolvable} attachment File to send
|
* @param {BufferResolvable} attachment File to send
|
||||||
@@ -144,7 +164,7 @@ class TextBasedChannel {
|
|||||||
* @returns {Promise<Message>}
|
* @returns {Promise<Message>}
|
||||||
*/
|
*/
|
||||||
sendFile(attachment, name, content, options = {}) {
|
sendFile(attachment, name, content, options = {}) {
|
||||||
return this.send(content, Object.assign(options, { file: { attachment, name } }));
|
return this.sendFiles([{ attachment, name }], content, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user