mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
sendFile via URL
This commit is contained in:
@@ -462,7 +462,6 @@ export default class InternalClient {
|
||||
|
||||
// def sendFile
|
||||
sendFile(where, _file, name) {
|
||||
|
||||
if (!name) {
|
||||
if (_file instanceof String || typeof _file === "string") {
|
||||
name = require("path").basename(_file);
|
||||
@@ -470,17 +469,19 @@ export default class InternalClient {
|
||||
// fs.createReadStream()'s have .path that give the path. Not sure about other streams though.
|
||||
name = require("path").basename(_file.path);
|
||||
} else {
|
||||
name = "image.png"; // Just have to go with default filenames.
|
||||
name = "default.png"; // Just have to go with default filenames.
|
||||
}
|
||||
}
|
||||
|
||||
return this.resolver.resolveChannel(where)
|
||||
.then(channel =>
|
||||
this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
|
||||
name,
|
||||
file: this.resolver.resolveFile(_file)
|
||||
})
|
||||
.then(res => channel.messages.add(new Message(res, channel, this.client)))
|
||||
this.resolver.resolveFile(_file)
|
||||
.then(file =>
|
||||
this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
|
||||
name,
|
||||
file
|
||||
}).then(res => channel.messages.add(new Message(res, channel, this.client)))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
/* global Buffer */
|
||||
|
||||
import fs from "fs";
|
||||
import request from "superagent";
|
||||
|
||||
import User from "../../Structures/User";
|
||||
import Channel from "../../Structures/Channel";
|
||||
@@ -76,9 +77,21 @@ export default class Resolver {
|
||||
|
||||
resolveFile(resource) {
|
||||
if (typeof resource === "string" || resource instanceof String) {
|
||||
return fs.createReadStream(resource);
|
||||
if (/^http(s):\/\//.test(resource)) {
|
||||
return new Promise((resolve, reject) => {
|
||||
request.get(resource).end((err, res) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(res.body);
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return Promise.resolve(resource);
|
||||
}
|
||||
}
|
||||
return resource;
|
||||
return Promise.resolve(resource);
|
||||
}
|
||||
|
||||
resolveMentions(resource) {
|
||||
|
||||
Reference in New Issue
Block a user