Added sendFile queue

This commit is contained in:
hydrabolt
2015-08-30 20:27:47 +01:00
parent e9034a3f3c
commit 7b1e7d2efa
3 changed files with 167 additions and 63 deletions

View File

@@ -62,7 +62,7 @@ var Client = (function () {
this.pmChannelCache = []; this.pmChannelCache = [];
this.readyTime = null; this.readyTime = null;
this.checkingQueue = {}; this.checkingQueue = {};
this.messageQueue = {}; this.queue = {};
} }
_createClass(Client, [{ _createClass(Client, [{
@@ -339,7 +339,7 @@ var Client = (function () {
var self = this; var self = this;
return new Promise(function (resolve, reject) { var prom = new Promise(function (resolve, reject) {
if (timeout) { if (timeout) {
setTimeout(remove, timeout); setTimeout(remove, timeout);
} else { } else {
@@ -358,6 +358,8 @@ var Client = (function () {
}); });
} }
}); });
return prom;
} }
}, { }, {
key: "updateMessage", key: "updateMessage",
@@ -563,23 +565,33 @@ var Client = (function () {
self.resolveDestination(destination).then(send)["catch"](error); self.resolveDestination(destination).then(send)["catch"](error);
function send(destination) { function send(destination) {
request.post(Endpoints.CHANNELS + "/" + destination + "/messages").set("authorization", self.token).attach("file", fstream, fileName).end(function (err, res) { if (~self.options.queue.indexOf("send")) {
//queue send file too
if (err) { if (!self.queue[destination]) {
error(err); self.queue[destination] = [];
} else {
var chann = self.getChannel("id", destination);
if (chann) {
var msg = chann.addMessage(new Message(res.body, chann, [], self.user));
callback(null, msg);
resolve(msg);
}
} }
});
self.queue[destination].push({
action: "sendFile",
attachment: fstream,
attachmentName: fileName,
then: good,
error: bad
});
self.checkQueue(destination);
} else {
//not queue
self._sendFile(destination, fstream, fileName).then(good)["catch"](bad);
}
} }
function error(err) { function good(msg) {
callback(null, msg);
resolve(msg);
}
function bad(err) {
callback(err); callback(err);
reject(err); reject(err);
} }
@@ -605,18 +617,18 @@ var Client = (function () {
} }
function send(destination) { function send(destination) {
if (~self.options.queue.indexOf("sendMessage")) { if (~self.options.queue.indexOf("send")) {
//we're QUEUEING messages, so sending them sequentially based on servers. //we're QUEUEING messages, so sending them sequentially based on servers.
if (!self.messageQueue[destination]) { if (!self.queue[destination]) {
self.messageQueue[destination] = []; self.queue[destination] = [];
} }
self.messageQueue[destination].push({ self.queue[destination].push({
action: "sendMessage", action: "sendMessage",
content: message, content: message,
mentions: mentions, mentions: mentions,
then: [mgood], then: mgood,
error: [mbad] error: mbad
}); });
self.checkQueue(destination); self.checkQueue(destination);
@@ -1395,6 +1407,29 @@ var Client = (function () {
}); });
}); });
} }
}, {
key: "_sendFile",
value: function _sendFile(destination, attachment) {
var attachmentName = arguments.length <= 2 || arguments[2] === undefined ? "DEFAULT BECAUSE YOU DIDN'T SPECIFY WHY.png" : arguments[2];
var self = this;
return new Promise(function (resolve, reject) {
request.post(Endpoints.CHANNELS + "/" + destination + "/messages").set("authorization", self.token).attach("file", attachment, attachmentName).end(function (err, res) {
if (err) {
reject(err);
} else {
var chann = self.getChannel("id", destination);
if (chann) {
var msg = chann.addMessage(new Message(res.body, chann, [], self.user));
resolve(msg);
}
}
});
});
}
}, { }, {
key: "checkQueue", key: "checkQueue",
value: function checkQueue(channelID) { value: function checkQueue(channelID) {
@@ -1405,21 +1440,33 @@ var Client = (function () {
if (!this.checkingQueue[channelID]) { if (!this.checkingQueue[channelID]) {
(function () { (function () {
var doNext = function doNext() { var doNext = function doNext() {
if (self.messageQueue[channelID].length === 0) { if (self.queue[channelID].length === 0) {
done(); done();
return; return;
} }
var queuedEvent = self.messageQueue[channelID][0]; var queuedEvent = self.queue[channelID][0];
switch (queuedEvent.action) { switch (queuedEvent.action) {
case "sendMessage": case "sendMessage":
var msgToSend = queuedEvent; var msgToSend = queuedEvent;
self._sendMessage(channelID, msgToSend.content, msgToSend.mentions).then(function (msg) { self._sendMessage(channelID, msgToSend.content, msgToSend.mentions).then(function (msg) {
msgToSend.then[0](msg); msgToSend.then(msg);
self.messageQueue[channelID].shift(); self.queue[channelID].shift();
doNext(); doNext();
})["catch"](function (err) { })["catch"](function (err) {
msgToSend["catch"][0](err); msgToSend["catch"](err);
self.messageQueue[channelID].shift(); self.queue[channelID].shift();
doNext();
});
break;
case "sendFile":
var fileToSend = queuedEvent;
self._sendFile(channelID, fileToSend.attachment, fileToSend.attachmentName).then(function (msg) {
fileToSend.then(msg);
self.queue[channelID].shift();
doNext();
})["catch"](function (err) {
fileToSend["catch"](err);
self.queue[channelID].shift();
doNext(); doNext();
}); });
break; break;

View File

@@ -52,7 +52,7 @@ class Client {
this.pmChannelCache = []; this.pmChannelCache = [];
this.readyTime = null; this.readyTime = null;
this.checkingQueue = {}; this.checkingQueue = {};
this.messageQueue = {}; this.queue = {};
} }
get uptime() { get uptime() {
@@ -577,30 +577,33 @@ class Client {
self.resolveDestination(destination).then(send).catch(error); self.resolveDestination(destination).then(send).catch(error);
function send(destination) { function send(destination) {
request if(~self.options.queue.indexOf("send")){
.post(`${Endpoints.CHANNELS}/${destination}/messages`) //queue send file too
.set("authorization", self.token) if (!self.queue[destination]) {
.attach("file", fstream, fileName) self.queue[destination] = [];
.end(function (err, res) { }
if (err) {
error(err);
} else {
var chann = self.getChannel("id", destination);
if (chann) {
var msg = chann.addMessage(new Message(res.body, chann, [], self.user));
callback(null, msg);
resolve(msg);
}
}
self.queue[destination].push({
action: "sendFile",
attachment : fstream,
attachmentName : fileName,
then: good,
error: bad
}); });
self.checkQueue(destination);
}else{
//not queue
self._sendFile(destination, fstream, fileName).then(good).catch(bad);
}
} }
function error(err) { function good(msg) {
callback(null, msg);
resolve(msg);
}
function bad(err) {
callback(err); callback(err);
reject(err); reject(err);
} }
@@ -625,18 +628,18 @@ class Client {
} }
function send(destination) { function send(destination) {
if (~self.options.queue.indexOf("sendMessage")) { if (~self.options.queue.indexOf("send")) {
//we're QUEUEING messages, so sending them sequentially based on servers. //we're QUEUEING messages, so sending them sequentially based on servers.
if (!self.messageQueue[destination]) { if (!self.queue[destination]) {
self.messageQueue[destination] = []; self.queue[destination] = [];
} }
self.messageQueue[destination].push({ self.queue[destination].push({
action: "sendMessage", action: "sendMessage",
content: message, content: message,
mentions: mentions, mentions: mentions,
then: [mgood], then: mgood,
error: [mbad] error: mbad
}); });
self.checkQueue(destination); self.checkQueue(destination);
@@ -1156,6 +1159,35 @@ class Client {
}); });
} }
_sendFile(destination, attachment, attachmentName = "DEFAULT BECAUSE YOU DIDN'T SPECIFY WHY.png"){
var self = this;
return new Promise(function(resolve, reject){
request
.post(`${Endpoints.CHANNELS}/${destination}/messages`)
.set("authorization", self.token)
.attach("file", attachment, attachmentName)
.end(function (err, res) {
if (err) {
reject(err);
} else {
var chann = self.getChannel("id", destination);
if (chann) {
var msg = chann.addMessage(new Message(res.body, chann, [], self.user));
resolve(msg);
}
}
});
});
}
checkQueue(channelID) { checkQueue(channelID) {
@@ -1167,23 +1199,37 @@ class Client {
doNext(); doNext();
function doNext() { function doNext() {
if (self.messageQueue[channelID].length === 0) { if (self.queue[channelID].length === 0) {
done(); done();
return; return;
} }
var queuedEvent = self.messageQueue[channelID][0]; var queuedEvent = self.queue[channelID][0];
switch (queuedEvent.action) { switch (queuedEvent.action) {
case "sendMessage": case "sendMessage":
var msgToSend = queuedEvent; var msgToSend = queuedEvent;
self._sendMessage(channelID, msgToSend.content, msgToSend.mentions) self._sendMessage(channelID, msgToSend.content, msgToSend.mentions)
.then(function (msg) { .then(function (msg) {
msgToSend.then[0](msg); msgToSend.then(msg);
self.messageQueue[channelID].shift(); self.queue[channelID].shift();
doNext(); doNext();
}) })
.catch(function (err) { .catch(function (err) {
msgToSend.catch[0](err); msgToSend.catch(err);
self.messageQueue[channelID].shift(); self.queue[channelID].shift();
doNext();
});
break;
case "sendFile":
var fileToSend = queuedEvent;
self._sendFile(channelID, fileToSend.attachment, fileToSend.attachmentName)
.then(function (msg){
fileToSend.then(msg);
self.queue[channelID].shift();
doNext();
})
.catch(function(err){
fileToSend.catch(err);
self.queue[channelID].shift();
doNext(); doNext();
}); });
break; break;

View File

@@ -1,7 +1,8 @@
var Discord = require("../"); var Discord = require("../");
var mybot = new Discord.Client({ var mybot = new Discord.Client({
queue : ["sendMessage"] queue : ["send"]
}); });
var fs = require("fs");
var server, channel, message, sentMessage = false; var server, channel, message, sentMessage = false;
@@ -11,11 +12,21 @@ mybot.on("message", function(message){
return; return;
} }
if( message.content !== "$$$" ){
return;
}
var action1 = mybot.sendMessage(message.channel, "this is message " + 1); var action1 = mybot.sendMessage(message.channel, "this is message " + 1);
var action1 = mybot.sendFile(message.channel, fs.createReadStream("./test/image.png"));
var action2 = mybot.sendMessage(message.channel, "this is message " + 2).then(log); var action2 = mybot.sendMessage(message.channel, "this is message " + 2).then(log);
var action1 = mybot.sendFile(message.channel, fs.createReadStream("./test/image.png"));
var action2 = mybot.sendMessage(message.channel, "this is message " + 3).then(log);
var action1 = mybot.sendFile(message.channel, fs.createReadStream("./test/image.png"));
var action2 = mybot.sendMessage(message.channel, "this is message " + 4).then(log);
var action1 = mybot.sendFile(message.channel, fs.createReadStream("./test/image.png"));
var action2 = mybot.sendMessage(message.channel, "this is message " + 5).then(log);
function log(){ function log(){
mybot.sendMessage(message.channel, action1.message ? action1.message : action1.error);
} }
}); });