Added reconnect timer

This commit is contained in:
benji7425 2016-12-30 14:53:33 +00:00
parent 1763d7e068
commit 5d1d43fd56
2 changed files with 18 additions and 6 deletions

View File

@ -1,5 +1,10 @@
# Changelog
## Unreleased
### Added
- Reconnect timer to repeatedly try reconnect at intervals
## 1.1.0 pre
### Added

View File

@ -13,6 +13,7 @@ var Config = require("./config.json"); //config file containing other settings
var DiscordClient = {
bot: null,
feedTimer = null,
reconnectTimer = null,
startup: function () {
//check if we can connect to discordapp.com to authenticate the bot
Dns.resolve("discordapp.com", function (err) {
@ -41,13 +42,19 @@ var DiscordClient = {
DiscordClient.checkPastMessagesForLinks();
},
onDisconnect: function (err, code) {
//do a bunch of logging
Log.event("DiscordClient was disconnected! " + code ? code : "No disconnect code provided", "Discord.io");
if (err) Log.error("DiscordClient disconnected!", err);
Log.info("Trying to reconnect bot");
Log.event("Bot was disconnected! " + err ? err : "" + code ? code : "No disconnect code provided", "Discord.io");;
//then actually attempt to reconnect
DiscordClient.bot.connect();
clearInterval(DiscordClient.feedTimer); //stop the feed timer
//set up a timer to try reconnect every 5sec
DiscordClient.reconnectTimer = setInterval(function () {
try {
DiscordClient.bot.connect();
}
catch (ex) {
Log.error("Exception thrown trying to reconnect bot." + ex.message);
}
});
},
onMessage: function (user, userID, channelID, message) {
//check if the message contains a link, in the right channel, and not the latest link from the rss feed