mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
Add sendFile function (#562)
* sendFile * Add default value to filename * eslint * (╯°□°)╯︵ ┻━┻
This commit is contained in:
@@ -32,8 +32,8 @@
|
|||||||
"opusscript": "^0.0.1"
|
"opusscript": "^0.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"jsdoc-parse": "^1.2.7",
|
"fs-extra": "^0.30.0",
|
||||||
"fs-extra": "^0.30.0"
|
"jsdoc-parse": "^1.2.7"
|
||||||
},
|
},
|
||||||
"optionalDependencies": {
|
"optionalDependencies": {
|
||||||
"node-opus": "^0.1.13"
|
"node-opus": "^0.1.13"
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
const request = require('superagent');
|
||||||
|
|
||||||
const getStructure = name => require(`../structures/${name}`);
|
const getStructure = name => require(`../structures/${name}`);
|
||||||
|
|
||||||
const User = getStructure('User');
|
const User = getStructure('User');
|
||||||
@@ -163,6 +167,41 @@ class ClientDataResolver {
|
|||||||
|
|
||||||
return String(data);
|
return String(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data that can be resolved to give a Buffer. This can be:
|
||||||
|
* * A Buffer
|
||||||
|
* * The path to a local file
|
||||||
|
* * An URL
|
||||||
|
* @typedef {String|Buffer} FileResolvable
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves a FileResolvable to a Buffer
|
||||||
|
* @param {FileResolvable} fileResolvable the file resolvable to resolve
|
||||||
|
* @returns {String|Buffer}
|
||||||
|
*/
|
||||||
|
resolveFile(resource) {
|
||||||
|
if ($string(resource)) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (/^https?:\/\//.test(resource)) {
|
||||||
|
request.get(resource)
|
||||||
|
.set('Content-Type', 'blob')
|
||||||
|
.end((err, res) => err ? reject(err) : resolve(res.body));
|
||||||
|
} else {
|
||||||
|
const file = path.resolve(resource);
|
||||||
|
const stat = fs.statSync(file);
|
||||||
|
if (!stat.isFile()) {
|
||||||
|
return reject(new Error(`The file could not be found: ${file}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolve(fs.readFileSync(file));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve(resource);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = ClientDataResolver;
|
module.exports = ClientDataResolver;
|
||||||
|
|||||||
@@ -29,12 +29,14 @@ class APIRequest {
|
|||||||
if (this.auth) {
|
if (this.auth) {
|
||||||
apiRequest.set('authorization', this.getAuth());
|
apiRequest.set('authorization', this.getAuth());
|
||||||
}
|
}
|
||||||
|
if (this.file && this.file.file) {
|
||||||
|
apiRequest.set('Content-Type', 'multipart/form-data');
|
||||||
|
apiRequest.attach('file', this.file.file, this.file.name);
|
||||||
|
}
|
||||||
if (this.data) {
|
if (this.data) {
|
||||||
apiRequest.send(this.data);
|
apiRequest.send(this.data);
|
||||||
}
|
}
|
||||||
if (this.file) {
|
|
||||||
apiRequest.attach('file', this.file.file, this.file.name);
|
|
||||||
}
|
|
||||||
apiRequest.set('User-Agent', this.rest.userAgentManager.userAgent);
|
apiRequest.set('User-Agent', this.rest.userAgentManager.userAgent);
|
||||||
return apiRequest;
|
return apiRequest;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class RESTMethods {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage($channel, content, tts, nonce) {
|
sendMessage($channel, content, tts, nonce, file) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const $this = this;
|
const $this = this;
|
||||||
let channel = $channel;
|
let channel = $channel;
|
||||||
@@ -49,7 +49,7 @@ class RESTMethods {
|
|||||||
function req() {
|
function req() {
|
||||||
$this.rest.makeRequest('post', Constants.Endpoints.channelMessages(channel.id), true, {
|
$this.rest.makeRequest('post', Constants.Endpoints.channelMessages(channel.id), true, {
|
||||||
content, tts, nonce,
|
content, tts, nonce,
|
||||||
})
|
}, file)
|
||||||
.then(data => resolve($this.rest.client.actions.MessageCreate.handle(data).m))
|
.then(data => resolve($this.rest.client.actions.MessageCreate.handle(data).m))
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ class DMChannel extends Channel {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendFile() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_cacheMessage() {
|
_cacheMessage() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,6 +124,10 @@ class GroupDMChannel extends Channel {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendFile() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_cacheMessage() {
|
_cacheMessage() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,6 +222,10 @@ class GuildMember {
|
|||||||
sendTTSMessage() {
|
sendTTSMessage() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendFile() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TextBasedChannel.applyToClass(GuildMember);
|
TextBasedChannel.applyToClass(GuildMember);
|
||||||
|
|||||||
@@ -32,6 +32,10 @@ class TextChannel extends GuildChannel {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendFile() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_cacheMessage() {
|
_cacheMessage() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,6 +122,10 @@ class User {
|
|||||||
sendTTSMessage() {
|
sendTTSMessage() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendFile() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TextBasedChannel.applyToClass(User);
|
TextBasedChannel.applyToClass(User);
|
||||||
|
|||||||
@@ -68,7 +68,24 @@ class TextBasedChannel {
|
|||||||
sendTTSMessage(content, options = {}) {
|
sendTTSMessage(content, options = {}) {
|
||||||
return this.client.rest.methods.sendMessage(this, content, true, options.nonce);
|
return this.client.rest.methods.sendMessage(this, content, true, options.nonce);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Send a file to this channel
|
||||||
|
* @param {FileResolvable} attachment The file to send
|
||||||
|
* @param {String} [fileName="file.jpg"] The name and extension of the file
|
||||||
|
* @returns {Promise<Message>}
|
||||||
|
*/
|
||||||
|
sendFile(attachment, fileName = 'file.jpg') {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.client.resolver.resolveFile(attachment)
|
||||||
|
.then(file => {
|
||||||
|
this.client.rest.methods.sendMessage(this, undefined, false, undefined, {
|
||||||
|
file,
|
||||||
|
name: fileName,
|
||||||
|
}).then(resolve).catch(reject);
|
||||||
|
})
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
|
* The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
|
||||||
* `after` are mutually exclusive. All the parameters are optional.
|
* `after` are mutually exclusive. All the parameters are optional.
|
||||||
@@ -173,7 +190,7 @@ function applyProp(structure, prop) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.applyToClass = (structure, full = false) => {
|
exports.applyToClass = (structure, full = false) => {
|
||||||
const props = ['sendMessage', 'sendTTSMessage'];
|
const props = ['sendMessage', 'sendTTSMessage', 'sendFile'];
|
||||||
if (full) {
|
if (full) {
|
||||||
props.push('_cacheMessage');
|
props.push('_cacheMessage');
|
||||||
props.push('getMessages');
|
props.push('getMessages');
|
||||||
|
|||||||
Reference in New Issue
Block a user