Fixed this. references that weren't working how I expected

This commit is contained in:
benji7425 2016-12-03 01:37:02 +00:00
parent ff4d44ed88
commit 124a8a1668
1 changed files with 26 additions and 21 deletions

View File

@ -20,15 +20,15 @@ var Bot = {
if (err) Log.error("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot (you are probably not connected to the internet).", err); if (err) Log.error("CONNECTION ERROR: Unable to locate discordapp.com to authenticate the bot (you are probably not connected to the internet).", 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
this.bot = new Discord.Client({ Bot.bot = new Discord.Client({
token: BotConfig.token, token: BotConfig.token,
autorun: true autorun: true
}); });
//set up the bot's event handlers //set up the bot's event handlers
this.bot.on("ready", this.onReady); Bot.bot.on("ready", Bot.onReady);
this.bot.on("disconnect", this.onDisconnect); Bot.bot.on("disconnect", Bot.onDisconnect);
this.bot.on("message", this.onMessage); Bot.bot.on("message", Bot.onMessage);
} }
}); });
}, },
@ -36,7 +36,7 @@ var Bot = {
if (IS_FIRST_RUN) { if (IS_FIRST_RUN) {
IS_FIRST_RUN = false; IS_FIRST_RUN = false;
Log.info("Registered bot " + this.bot.username + " - (" + this.bot.id + ")"); Log.info("Registered bot " + Bot.bot.username + " - (" + Bot.bot.id + ")");
Log.info("Setting up timer to check feed every " + Config.pollingInterval + " milliseconds"); Log.info("Setting up timer to check feed every " + Config.pollingInterval + " milliseconds");
//set up the timer to check the feed //set up the timer to check the feed
@ -47,7 +47,7 @@ var Bot = {
} }
//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 //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
this.checkPastMessagesForLinks(); Bot.checkPastMessagesForLinks();
}, },
onDisconnect: function (err, code) { onDisconnect: function (err, code) {
//do a bunch of logging //do a bunch of logging
@ -56,7 +56,7 @@ var Bot = {
Log.info("Trying to reconnect bot"); Log.info("Trying to reconnect bot");
//then actually attempt to reconnect //then actually attempt to reconnect
this.bot.connect(); Bot.bot.connect();
}, },
onMessage: function (user, userID, channelID, message) { onMessage: function (user, userID, channelID, message) {
//check if the message contains a link, in the right channel, and not the latest link from the rss feed //check if the message contains a link, in the right channel, and not the latest link from the rss feed
@ -107,10 +107,10 @@ var YouTube = {
share: "http://youtu.be/", share: "http://youtu.be/",
full: "http://www.youtube.com/watch?v=", full: "http://www.youtube.com/watch?v=",
convertShareToFull: function (shareUrl) { convertShareToFull: function (shareUrl) {
return shareUrl.replace(this.share, this.full); return shareUrl.replace(YouTube.share, YouTube.full);
}, },
convertFullToShare: function (fullUrl) { convertFullToShare: function (fullUrl) {
var shareUrl = fullUrl.replace(this.share, this.full); var shareUrl = fullUrl.replace(YouTube.share, YouTube.full);
if (shareUrl.includes("&")) if (shareUrl.includes("&"))
shareUrl = shareUrl.slice(0, fullUrl.indexOf("&")); shareUrl = shareUrl.slice(0, fullUrl.indexOf("&"));
@ -128,19 +128,19 @@ var Links = {
//cheaty way to get around http and https not matching //cheaty way to get around http and https not matching
link = link.replace("https://", "http://"); link = link.replace("https://", "http://");
//store the new link if not stored already //store the new link if not stored already
if (!this.cached.includes(link)) { if (!Links.cached.includes(link)) {
this.cached.push(link); Links.cached.push(link);
Log.info("Cached URL: " + link); Log.info("Cached URL: " + link);
} }
//get rid of the first array element if we have reached our cache limit //get rid of the first array element if we have reached our cache limit
if (this.cached.length > (Config.numLinksToCache || 10)) if (Links.cached.length > (Config.numLinksToCache || 10))
this.cached.shift(); Links.cached.shift();
}, },
checkCache: function (link) { checkCache: function (link) {
if (Config.youtubeMode && link.includes(link)) { if (Config.youtubeMode && link.includes(link)) {
return this.cached.includes(YouTube.convertFullToShare(link)); return Links.cached.includes(YouTube.convertFullToShare(link));
} }
return this.cached.includes(link); return Links.cached.includes(link);
}, },
validateAndPost: function (err, articles) { validateAndPost: function (err, articles) {
if (err) Log.error("FEED ERROR: Error reading RSS feed.", err); if (err) Log.error("FEED ERROR: Error reading RSS feed.", err);
@ -149,7 +149,7 @@ var Links = {
var latestLink = articles[0].link.replace("https", "http"); var latestLink = articles[0].link.replace("https", "http");
//check whether the latest link out the feed exists in our cache //check whether the latest link out the feed exists in our cache
if (!this.checkCache(latestLink)) { if (!Links.checkCache(latestLink)) {
if (Config.youtubeMode && latestLink.includes(YouTube.fullUrl)) if (Config.youtubeMode && latestLink.includes(YouTube.fullUrl))
latestLink = YouTube.convertFullToShare(latestLink); latestLink = YouTube.convertFullToShare(latestLink);
Log.info("Attempting to post new link: " + latestLink); Log.info("Attempting to post new link: " + latestLink);
@ -174,14 +174,14 @@ var Links = {
}); });
//finally make sure the link is cached, so it doesn't get posted again //finally make sure the link is cached, so it doesn't get posted again
this.cache(latestLink); Links.cache(latestLink);
} }
else if (this.latestFromFeed != latestLink) else if (Links.latestFromFeed != latestLink)
//alternatively, if we have a new link from the feed, but its been posted already, just alert the console //alternatively, if we have a new link from the feed, but its been posted already, just alert the console
Log.info("Didn't post new feed link because already detected as posted " + latestLink); Log.info("Didn't post new feed link because already detected as posted " + latestLink);
//ensure our latest feed link variable is up to date, so we can track when the feed updates //ensure our latest feed link variable is up to date, so we can track when the feed updates
this.latestFromFeed = latestLink; Links.latestFromFeed = latestLink;
} }
} }
}; };
@ -190,9 +190,14 @@ var Feed = {
urlObj: Url.parse(Config.feedUrl), urlObj: Url.parse(Config.feedUrl),
checkAndPost: function () { checkAndPost: function () {
//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(this.urlObj.host, function (err) { Dns.resolve(Links.urlObj.host, function (err) {
if (err) Log.error("CONNECTION ERROR: Cannot resolve host (you are probably not connected to the internet)", err); if (err) Log.error("CONNECTION ERROR: Cannot resolve host (you are probably not connected to the internet)", err);
else FeedRead(Config.feedUrl, Links.checkAndPost); else FeedRead(Config.feedUrl, Links.checkAndPost);
}); });
} }
}; };
//IIFE to kickstart the bot when the app loads
(function(){
Bot.startup();
})();