sendFile and fix deleteMessage

This commit is contained in:
hydrabolt
2015-11-01 00:16:58 +00:00
parent 53ef5df10d
commit b8aaa590b4
8 changed files with 123 additions and 36 deletions

View File

@@ -161,7 +161,7 @@ var Client = (function (_EventEmitter) {
callback = options;
}
self.internal.deleteMessage(msg).then(function () {
self.internal.deleteMessage(msg, options).then(function () {
callback();
resolve();
})["catch"](function (e) {
@@ -217,6 +217,24 @@ var Client = (function (_EventEmitter) {
});
};
// def sendFile
Client.prototype.sendFile = function sendFile(where, attachment) {
var name = arguments.length <= 2 || arguments[2] === undefined ? "image.png" : arguments[2];
var callback = arguments.length <= 3 || arguments[3] === undefined ? function (err, m) {} : arguments[3];
var self = this;
return new Promise(function (resolve, reject) {
self.internal.sendFile(where, attachment, name).then(function (m) {
callback(null, m);
resolve(m);
})["catch"](function (e) {
callback(e);
reject(e);
});
});
};
return Client;
})(EventEmitter);

View File

@@ -251,6 +251,33 @@ var InternalClient = (function () {
});
};
InternalClient.prototype.sendFile = function sendFile(where, _file) {
var name = arguments.length <= 2 || arguments[2] === undefined ? "image.png" : arguments[2];
var self = this;
return new Promise(function (resolve, reject) {
self.resolver.resolveChannel(where).then(next)["catch"](function (e) {
return reject(new Error("couldn't resolve to channel - " + e));
});
function next(channel) {
var file = self.resolver.resolveFile(_file);
request.post(Endpoints.CHANNEL_MESSAGES(channel.id)).set("authorization", self.token).attach("file", file, name).end(function (err, res) {
if (err) {
reject(new Error(err.response.text));
} else {
resolve(channel.messages.add(new Message(res.body, channel, self.client)));
}
});
}
});
};
// def getChannelLogs
InternalClient.prototype.getChannelLogs = function getChannelLogs(_channel) {
var limit = arguments.length <= 1 || arguments[1] === undefined ? 500 : arguments[1];
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

View File

@@ -2,6 +2,8 @@
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var fs = require("fs");
var User = require("../../Structures/User.js"),
Channel = require("../../Structures/Channel.js"),
TextChannel = require("../../Structures/TextChannel.js"),
@@ -18,6 +20,14 @@ var Resolver = (function () {
this.internal = internal;
}
Resolver.prototype.resolveFile = function resolveFile(resource) {
if (typeof resource === "string" || resource instanceof String) {
return fs.createReadStream(resource);
} else {
return resource;
}
};
Resolver.prototype.resolveMentions = function resolveMentions(resource) {
// resource is a string
var _mentions = [];