mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
Added message queue handling
This commit is contained in:
@@ -61,7 +61,8 @@ var Client = (function () {
|
|||||||
this.serverCache = [];
|
this.serverCache = [];
|
||||||
this.pmChannelCache = [];
|
this.pmChannelCache = [];
|
||||||
this.readyTime = null;
|
this.readyTime = null;
|
||||||
this.optionsQueue = {};
|
this.checkingQueue = {};
|
||||||
|
this.messageQueue = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
_createClass(Client, [{
|
_createClass(Client, [{
|
||||||
@@ -607,7 +608,18 @@ var Client = (function () {
|
|||||||
|
|
||||||
if (self.options.queue) {
|
if (self.options.queue) {
|
||||||
//we're QUEUEING messages, so sending them sequentially based on servers.
|
//we're QUEUEING messages, so sending them sequentially based on servers.
|
||||||
self.addMessageQueue(destination);
|
if (!self.messageQueue[destination]) {
|
||||||
|
self.messageQueue[destination] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
self.messageQueue[destination].push({
|
||||||
|
content: message,
|
||||||
|
mentions: mentions,
|
||||||
|
then: [resolve, callback],
|
||||||
|
error: [reject, callback]
|
||||||
|
});
|
||||||
|
|
||||||
|
self.checkQueue(destination);
|
||||||
} else {
|
} else {
|
||||||
self._sendMessage(destination, message, mentions).then(mgood)["catch"](mbad);
|
self._sendMessage(destination, message, mentions).then(mgood)["catch"](mbad);
|
||||||
}
|
}
|
||||||
@@ -1379,6 +1391,45 @@ var Client = (function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}, {
|
||||||
|
key: "checkQueue",
|
||||||
|
value: function checkQueue(channelID) {
|
||||||
|
var _this = this;
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
if (!this.checkingQueue[channelID]) {
|
||||||
|
(function () {
|
||||||
|
var doNext = function doNext() {
|
||||||
|
if (self.messageQueue[channelID].length === 0) {
|
||||||
|
done();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var msgToSend = self.messageQueue[channelID][0];
|
||||||
|
self._sendMessage(channelID, msgToSend.content, msgToSend.mentions).then(function (msg) {
|
||||||
|
msgToSend.then[0](msg);
|
||||||
|
msgToSend.then[1](null, msg);
|
||||||
|
self.messageQueue[channelID].shift();
|
||||||
|
doNext();
|
||||||
|
})["catch"](function (err) {
|
||||||
|
msgToSend["catch"][0](err);
|
||||||
|
msgToSend["catch"][1](err);
|
||||||
|
self.messageQueue[channelID].shift();
|
||||||
|
doNext();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
var done = function done() {
|
||||||
|
self.checkingQueue[channelID] = false;
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
//if we aren't already checking this queue.
|
||||||
|
_this.checkingQueue[channelID] = true;
|
||||||
|
doNext();
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}
|
||||||
}, {
|
}, {
|
||||||
key: "uptime",
|
key: "uptime",
|
||||||
get: function get() {
|
get: function get() {
|
||||||
|
|||||||
@@ -51,7 +51,8 @@ class Client {
|
|||||||
this.serverCache = [];
|
this.serverCache = [];
|
||||||
this.pmChannelCache = [];
|
this.pmChannelCache = [];
|
||||||
this.readyTime = null;
|
this.readyTime = null;
|
||||||
this.optionsQueue = {};
|
this.checkingQueue = {};
|
||||||
|
this.messageQueue = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
get uptime() {
|
get uptime() {
|
||||||
@@ -625,7 +626,18 @@ class Client {
|
|||||||
|
|
||||||
if(self.options.queue){
|
if(self.options.queue){
|
||||||
//we're QUEUEING messages, so sending them sequentially based on servers.
|
//we're QUEUEING messages, so sending them sequentially based on servers.
|
||||||
self.addMessageQueue(destination);
|
if(!self.messageQueue[destination]){
|
||||||
|
self.messageQueue[destination] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
self.messageQueue[destination].push({
|
||||||
|
content : message,
|
||||||
|
mentions : mentions,
|
||||||
|
then : [resolve, callback],
|
||||||
|
error : [reject, callback]
|
||||||
|
});
|
||||||
|
|
||||||
|
self.checkQueue(destination);
|
||||||
}else{
|
}else{
|
||||||
self._sendMessage(destination, message, mentions).then(mgood).catch(mbad);
|
self._sendMessage(destination, message, mentions).then(mgood).catch(mbad);
|
||||||
}
|
}
|
||||||
@@ -1138,7 +1150,43 @@ class Client {
|
|||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
checkQueue(channelID){
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
if(!this.checkingQueue[channelID]){
|
||||||
|
//if we aren't already checking this queue.
|
||||||
|
this.checkingQueue[channelID] = true;
|
||||||
|
doNext();
|
||||||
|
|
||||||
|
function doNext(){
|
||||||
|
if(self.messageQueue[channelID].length === 0){
|
||||||
|
done();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var msgToSend = self.messageQueue[channelID][0];
|
||||||
|
self._sendMessage(channelID, msgToSend.content, msgToSend.mentions)
|
||||||
|
.then(function(msg){
|
||||||
|
msgToSend.then[0](msg);
|
||||||
|
msgToSend.then[1](null, msg);
|
||||||
|
self.messageQueue[channelID].shift();
|
||||||
|
doNext();
|
||||||
|
})
|
||||||
|
.catch(function(err){
|
||||||
|
msgToSend.catch[0](err);
|
||||||
|
msgToSend.catch[1](err);
|
||||||
|
self.messageQueue[channelID].shift();
|
||||||
|
doNext();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function done(){
|
||||||
|
self.checkingQueue[channelID] = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getGateway() {
|
function getGateway() {
|
||||||
|
|||||||
27
test/bot.1.js
Normal file
27
test/bot.1.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
var Discord = require("../");
|
||||||
|
var mybot = new Discord.Client({
|
||||||
|
queue : true
|
||||||
|
});
|
||||||
|
|
||||||
|
var server, channel, message, sentMessage = false;
|
||||||
|
|
||||||
|
mybot.on("message", function(message){
|
||||||
|
|
||||||
|
if(message.content === "$$$"){
|
||||||
|
mybot.sendMessage(message.channel, "this is part 1");
|
||||||
|
mybot.sendMessage(message.channel, "this is part 2");
|
||||||
|
mybot.sendMessage(message.channel, "this is part 3");
|
||||||
|
mybot.sendMessage(message.channel, "this is part 4");
|
||||||
|
mybot.sendMessage(message.channel, "this is part 5");
|
||||||
|
mybot.sendMessage(message.channel, "this is part 6");
|
||||||
|
mybot.sendMessage(message.channel, "this is part 7");
|
||||||
|
mybot.sendMessage(message.channel, "this is part 8");
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
function error(err){
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
mybot.login(process.env["ds_email"], process.env["ds_password"]).catch(error);
|
||||||
0
test/msgbot.js
Normal file
0
test/msgbot.js
Normal file
Reference in New Issue
Block a user