mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
sendFile via URL
This commit is contained in:
@@ -583,16 +583,18 @@ var InternalClient = (function () {
|
|||||||
// fs.createReadStream()'s have .path that give the path. Not sure about other streams though.
|
// fs.createReadStream()'s have .path that give the path. Not sure about other streams though.
|
||||||
name = require("path").basename(_file.path);
|
name = require("path").basename(_file.path);
|
||||||
} else {
|
} 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(function (channel) {
|
return this.resolver.resolveChannel(where).then(function (channel) {
|
||||||
return _this15.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
|
return _this15.resolver.resolveFile(_file).then(function (file) {
|
||||||
name: name,
|
return _this15.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
|
||||||
file: _this15.resolver.resolveFile(_file)
|
name: name,
|
||||||
}).then(function (res) {
|
file: file
|
||||||
return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this15.client));
|
}).then(function (res) {
|
||||||
|
return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this15.client));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ var _fs = require("fs");
|
|||||||
|
|
||||||
var _fs2 = _interopRequireDefault(_fs);
|
var _fs2 = _interopRequireDefault(_fs);
|
||||||
|
|
||||||
|
var _superagent = require("superagent");
|
||||||
|
|
||||||
|
var _superagent2 = _interopRequireDefault(_superagent);
|
||||||
|
|
||||||
var _StructuresUser = require("../../Structures/User");
|
var _StructuresUser = require("../../Structures/User");
|
||||||
|
|
||||||
var _StructuresUser2 = _interopRequireDefault(_StructuresUser);
|
var _StructuresUser2 = _interopRequireDefault(_StructuresUser);
|
||||||
@@ -130,9 +134,21 @@ var Resolver = (function () {
|
|||||||
|
|
||||||
Resolver.prototype.resolveFile = function resolveFile(resource) {
|
Resolver.prototype.resolveFile = function resolveFile(resource) {
|
||||||
if (typeof resource === "string" || resource instanceof String) {
|
if (typeof resource === "string" || resource instanceof String) {
|
||||||
return _fs2["default"].createReadStream(resource);
|
if (/^http(s):\/\//.test(resource)) {
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
_superagent2["default"].get(resource).end(function (err, res) {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
resolve(res.body);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return Promise.resolve(resource);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return resource;
|
return Promise.resolve(resource);
|
||||||
};
|
};
|
||||||
|
|
||||||
Resolver.prototype.resolveMentions = function resolveMentions(resource) {
|
Resolver.prototype.resolveMentions = function resolveMentions(resource) {
|
||||||
|
|||||||
@@ -462,7 +462,6 @@ export default class InternalClient {
|
|||||||
|
|
||||||
// def sendFile
|
// def sendFile
|
||||||
sendFile(where, _file, name) {
|
sendFile(where, _file, name) {
|
||||||
|
|
||||||
if (!name) {
|
if (!name) {
|
||||||
if (_file instanceof String || typeof _file === "string") {
|
if (_file instanceof String || typeof _file === "string") {
|
||||||
name = require("path").basename(_file);
|
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.
|
// fs.createReadStream()'s have .path that give the path. Not sure about other streams though.
|
||||||
name = require("path").basename(_file.path);
|
name = require("path").basename(_file.path);
|
||||||
} else {
|
} 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)
|
return this.resolver.resolveChannel(where)
|
||||||
.then(channel =>
|
.then(channel =>
|
||||||
this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
|
this.resolver.resolveFile(_file)
|
||||||
name,
|
.then(file =>
|
||||||
file: this.resolver.resolveFile(_file)
|
this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, null, {
|
||||||
})
|
name,
|
||||||
.then(res => channel.messages.add(new Message(res, channel, this.client)))
|
file
|
||||||
|
}).then(res => channel.messages.add(new Message(res, channel, this.client)))
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
/* global Buffer */
|
/* global Buffer */
|
||||||
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import request from "superagent";
|
||||||
|
|
||||||
import User from "../../Structures/User";
|
import User from "../../Structures/User";
|
||||||
import Channel from "../../Structures/Channel";
|
import Channel from "../../Structures/Channel";
|
||||||
@@ -76,9 +77,21 @@ export default class Resolver {
|
|||||||
|
|
||||||
resolveFile(resource) {
|
resolveFile(resource) {
|
||||||
if (typeof resource === "string" || resource instanceof String) {
|
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) {
|
resolveMentions(resource) {
|
||||||
|
|||||||
Reference in New Issue
Block a user