Merged feature/reconnect-fixing into develop

This commit is contained in:
benji7425 2017-01-04 19:53:55 +00:00
commit 1d9f815ee9
3 changed files with 21 additions and 20 deletions

View File

@ -1,5 +1,10 @@
# Changelog # Changelog
## Unreleased
### Updated
- Updated reconnect logic to hopefully be more stable
## v1.1.1 ## v1.1.1
### Added ### Added

View File

@ -1,6 +1,6 @@
{ {
"feedUrl": "https://www.youtube.com/feeds/videos.xml?user=EthosLab", "feedUrl": "https://www.youtube.com/feeds/videos.xml?user=EthosLab",
"channelID": "241238530376990722", "channelID": "264420391282409473",
"pollingInterval": 5000, "pollingInterval": 5000,
"numLinksToCache": 10, "numLinksToCache": 10,
"youtubeMode": true "youtubeMode": true

View File

@ -12,12 +12,13 @@ var Config = require("./config.json"); //config file containing other settings
var DiscordClient = { var DiscordClient = {
bot: null, bot: null,
feedTimer: null, feedInterval: null,
reconnectTimer: null, reconnectInterval: null,
startup: function () { startup: function () {
//check if we can connect to discordapp.com to authenticate the bot //check if we can connect to discordapp.com to authenticate the bot
Dns.resolve("discordapp.com", function (err) { Dns.resolve("discordapp.com", function (err) {
if (err) Log.error("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot", err); if (err)
Log.error("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot", err);
else { else {
//if there was no error, go ahead and create and authenticate the bot //if there was no error, go ahead and create and authenticate the bot
DiscordClient.bot = new Discord.Client({ DiscordClient.bot = new Discord.Client({
@ -35,26 +36,21 @@ var DiscordClient = {
onReady: function () { onReady: function () {
Log.info("Registered/connected bot " + DiscordClient.bot.username + " - (" + DiscordClient.bot.id + ")"); Log.info("Registered/connected bot " + DiscordClient.bot.username + " - (" + DiscordClient.bot.id + ")");
Log.info("Setting up timer to check feed every " + Config.pollingInterval + " milliseconds"); clearInterval(DiscordClient.reconnectInterval);
DiscordClient.feedTimer = setInterval(Feed.checkAndPost, Config.pollingInterval); //set up the timer to check the feed
//we need to check past messages for links on startup, but also on reconnect because we don't know what has happened during the downtime Log.info("Setting up timer to check feed every " + Config.pollingInterval + " milliseconds");
DiscordClient.checkPastMessagesForLinks(); DiscordClient.feedInterval = setInterval(Feed.checkAndPost, Config.pollingInterval); //set up the timer to check the feed
DiscordClient.checkPastMessagesForLinks(); //we need to check past messages for links on startup, but also on reconnect because we don't know what has happened during the downtime
}, },
onDisconnect: function (err, code) { onDisconnect: function (err, code) {
Log.event("Bot was disconnected! " + err ? err : "" + code ? code : "No disconnect code provided.\nClearing the feed timer and starting reconnect timer", "Discord.io"); Log.event("Bot was disconnected! " + (err ? err : "") + (code ? code : "No disconnect code provided.") + "\nClearing the feed timer and starting reconnect timer", "Discord.io");
clearInterval(DiscordClient.feedTimer); //stop the feed timer clearInterval(DiscordClient.feedInterval)
//set up a timer to try reconnect every 5sec DiscordClient.reconnectInterval = setInterval(function () {
DiscordClient.reconnectTimer = setInterval(function () { DiscordClient.startup();
try { }, (Config.reconnectInterval || Config.pollingInterval));
DiscordClient.bot.connect();
}
catch (ex) {
Log.error("Exception thrown trying to reconnect bot." + ex.message);
}
});
}, },
onMessage: function (user, userID, channelID, message) { onMessage: function (user, userID, channelID, message) {
//check if the message is in the right channel, contains a link, and is not the latest link from the rss feed //check if the message is in the right channel, contains a link, and is not the latest link from the rss feed
@ -119,7 +115,7 @@ var YouTube = {
var Links = { var Links = {
standardise: function (link) { standardise: function (link) {
link = link.replace("https://", "http://"); //cheaty way to get around http and https not matching link = link.replace("https://", "http://"); //cheaty way to get around http and https not matching
if(Config.youtubeMode) link = link.split("&")[0]; //quick way to chop off stuff like &feature=youtube etc if (Config.youtubeMode) link = link.split("&")[0]; //quick way to chop off stuff like &feature=youtube etc
return link; return link;
}, },
messageContainsLink: function (message) { messageContainsLink: function (message) {