From 62f15177672b7f6ce55d8d2453607c1f827a790d Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 4 Jan 2017 19:06:29 +0000 Subject: [PATCH] Updated onDisconnect function to call startup func Make it so we just reset everything when we get a disconnect event --- feed-bot.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/feed-bot.js b/feed-bot.js index b3eca88..002a6fb 100644 --- a/feed-bot.js +++ b/feed-bot.js @@ -17,7 +17,9 @@ var DiscordClient = { startup: function () { //check if we can connect to discordapp.com to authenticate the bot Dns.resolve("discordapp.com", function (err) { - if (err) Log.error("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot", err); + if (err) { + throw "CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot", err; + } else { //if there was no error, go ahead and create and authenticate the bot DiscordClient.bot = new Discord.Client({ @@ -35,6 +37,8 @@ var DiscordClient = { onReady: function () { Log.info("Registered/connected bot " + DiscordClient.bot.username + " - (" + DiscordClient.bot.id + ")"); + clearInterval(DiscordClient.reconnectTimer); + Log.info("Setting up timer to check feed every " + Config.pollingInterval + " milliseconds"); DiscordClient.feedTimer = setInterval(Feed.checkAndPost, Config.pollingInterval); //set up the timer to check the feed @@ -44,15 +48,13 @@ var DiscordClient = { 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"); - clearInterval(DiscordClient.feedTimer); //stop the feed timer - - //set up a timer to try reconnect every 5sec DiscordClient.reconnectTimer = setInterval(function () { try { - DiscordClient.bot.connect(); + //I've had enough problems with reconnecting properly so just call the startup function to reset everything when we want to reconnect + DiscordClient.startup(); } - catch (ex) { - Log.error("Exception thrown trying to reconnect bot." + ex.message); + catch(ex){ + Log.error(ex); } }); }, @@ -119,7 +121,7 @@ var YouTube = { var Links = { standardise: function (link) { 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; }, messageContainsLink: function (message) {