Updated intervals to use a single timer and reassign to its func var

This commit is contained in:
benji7425 2017-01-08 05:00:07 +00:00
parent 61487cb652
commit e86e6f3059
1 changed files with 6 additions and 12 deletions

View File

@ -12,8 +12,6 @@ var Config = require("./config.json"); //config file containing other settings
var DiscordClient = { var DiscordClient = {
bot: null, bot: null,
feedInterval: 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) {
@ -36,21 +34,14 @@ 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 + ")");
clearInterval(DiscordClient.reconnectInterval);
Log.info("Setting up timer to check feed every " + Config.pollingInterval + " milliseconds");
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 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
intervalFunc = Feed.checkAndPost;
}, },
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.feedInterval) intervalFunc = DiscordClient.startup; //reassign the interval function to try restart the bot every 5 sec
DiscordClient.reconnectInterval = setInterval(function () {
DiscordClient.startup();
}, (Config.reconnectInterval || Config.pollingInterval));
}, },
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
@ -197,7 +188,10 @@ var Feed = {
} }
}; };
var intervalFunc = () => {}; //do nothing by default
//IIFE to kickstart the bot when the app loads //IIFE to kickstart the bot when the app loads
(function () { (function () {
DiscordClient.startup(); DiscordClient.startup();
setInterval(intervalFunc, Config.pollingInterval);
})(); })();