Added internet disconnection error handling

This commit is contained in:
benji7425 2016-10-30 20:15:15 +00:00
parent 12ac8683a8
commit 4cf4c10323

View file

@ -1,41 +1,66 @@
var console = require("console");
var Dns = require("dns");
var Url = require("url");
var Discord = require("discord.io");
var FeedRead = require("feed-read");
var BotConfig = require("./botConfig.json");
var Config = require("./config.json");
var bot = new Discord.Client({
token: BotConfig.token,
autorun: true
});
//get a URL object from the feedUrl so we can examine it and check connectivity later
var url = Url.parse(Config.feedUrl);
bot.on("ready", function () {
console.log(bot.username + " - (" + bot.id + ")");
//placeholder for our bot - we need to check for connectivity before assigning this though
var bot;
setInterval(checkFeedAndPost, Config.pollingInterval);
});
//check if we can connect to discordapp.com to authenticate the bot
Dns.resolve("discordapp.com", function (err) {
if (err) console.log("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot (you are probably not connected to the internet). Details: " + (err.message || err));
else {
//if there was no error, go ahead and create and authenticate the bot
bot = new Discord.Client({
token: BotConfig.token,
autorun: true
});
bot.on("message", function (user, userID, channelID, message) {
if (message === "ping") {
bot.sendMessage({
to: channelID,
message: "pong"
//when the bot is ready, set a polling interval for the rss feed
bot.on("ready", function () {
console.log(bot.username + " - (" + bot.id + ")");
setInterval(checkFeedAndPost, Config.pollingInterval);
});
//easy way to check if the bot is active - replies "pong" when you type "ping" in discord
bot.on("message", function (user, userID, channelID, message) {
if (message === "ping") {
bot.sendMessage({
to: channelID,
message: "pong"
});
}
});
}
});
function checkFeedAndPost() {
//check the feed, with a callback
FeedRead(Config.feedUrl, function (err, articles){
try{
checkLinkAndPost(err, articles);
}
catch(ex){
console.log("ERROR: " + (ex.message || ex));
//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) {
if (err) console.log("CONNECTION ERROR: Cannot resolve host (you are probably not connected to the internet). Details: " + (err.message || err));
else {
//check the feed asynchronously, check the latest link when done
FeedRead(Config.feedUrl, function (err, articles) {
try {
checkLinkAndPost(err, articles);
}
catch (ex) {
//checkFeedAndPost is async due to being called by setInterval so the console log has to be here
console.log("FEED ERROR: " + (ex.message || ex));
}
});
}
});
}
//checks if the link has been posted previously, posts if not
function checkLinkAndPost(err, articles) {
if (err) throw "Error reading RSS feed: " + (err.message || err);