Merged feature/disconnect-handling into develop

This commit is contained in:
benji7425 2016-11-02 01:52:04 +00:00
commit 3373d6d2ec

View file

@ -52,32 +52,28 @@ Dns.resolve("discordapp.com", function (err) {
setInterval(checkFeedAndPost, Config.pollingInterval); setInterval(checkFeedAndPost, Config.pollingInterval);
}); });
bot.on("disconnect", function(err, code){ bot.on("disconnect", function (err, code) {
logEvent("Bot was disconnected. Code: " + code + ". Details: " + (err.message || err)); logEvent("Bot was disconnected! " + code != null ? code : "No disconnect code provided");
if (err) reportError("Bot disconnect error: " + (err.message || err));
logEvent("Trying to reconnect bot"); logEvent("Trying to reconnect bot");
bot.connect(); bot.connect();
}); });
bot.on("message", function (user, userID, channelID, message) { bot.on("message", function (user, userID, channelID, message) {
//check if the message is a link, cache it if it is //check if the message contains a link, in the right channel, and not the latest link from the rss feed
if (linkRegExp.test(message) && (message !== latestFeedLink)) { if (channelID === Config.channelID && linkRegExp.test(message) && (message !== latestFeedLink)) {
logEvent("Detected posted link: " + message); logEvent("Detected posted link in this message: " + message);
//detect the url inside the string, and cache it //detect the url inside the string, and cache it
Uri.withinString(message, function (url) { Uri.withinString(message, function (url) {
cacheLink(url); cacheLink(url);
return url; return url;
}); });
} else if(message === "enableVerboseLogging"){
verboseLogging = true;
} else if(message === "disableVerboseLogging"){
verboseLogging = false;
} }
}); });
} }
}); });
function checkFeedAndPost() { function checkFeedAndPost() {
if(verboseLogging) logEvent("Bot is currently " + (bot.connected ? "connected to" : "disconnected from") + " discord");
//check that we have an internet connection (well not exactly - check that we have a connection to the host of the feedUrl) //check that we have an internet connection (well not exactly - check that we have a connection to the host of the feedUrl)
Dns.resolve(url.host, function (err) { Dns.resolve(url.host, function (err) {
if (err) reportError("CONNECTION ERROR: Cannot resolve host (you are probably not connected to the internet). Details: " + (err.message || err)); if (err) reportError("CONNECTION ERROR: Cannot resolve host (you are probably not connected to the internet). Details: " + (err.message || err));
@ -97,16 +93,16 @@ function checkLinkAndPost(err, articles) {
bot.sendMessage({ bot.sendMessage({
to: Config.channelID, to: Config.channelID,
message: latestLink message: latestLink
}, function(err, message){ }, function (err, message) {
reportError("ERROR: Failed to send message: " + (err.message || err) + " " + message); reportError("ERROR: Failed to send message: " + (err.message || err) + " " + message);
logEvent("Checking bot connectivity"); logEvent("Checking bot connectivity");
if(bot.connected) if (bot.connected)
logEvent("Connectivity seems fine - I have no idea why the message didn't post"); logEvent("Connectivity seems fine - I have no idea why the message didn't post");
else{ else {
reportError("Bot appears to be disconnected! Attempting to reconnect...") reportError("Bot appears to be disconnected! Attempting to reconnect...")
bot.connect(); bot.connect();
} }
}); });
cacheLink(latestLink); cacheLink(latestLink);
} }