Updated various functions to use callbacks + updated some function names
This commit is contained in:
parent
1c4b42b299
commit
85f10dc99a
117
feed-bot.js
117
feed-bot.js
|
@ -36,7 +36,11 @@ var DiscordClient = {
|
||||||
|
|
||||||
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;
|
intervalFunc = () => {
|
||||||
|
Feed.check(FeedRead(Config.feedUrl, (err, articles) => {
|
||||||
|
Links.validate(err, articles, DiscordClient.post);
|
||||||
|
}));
|
||||||
|
};
|
||||||
},
|
},
|
||||||
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");
|
||||||
|
@ -45,16 +49,16 @@ var DiscordClient = {
|
||||||
},
|
},
|
||||||
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
|
||||||
if (channelID === Config.channelID && Links.messageContainsLink(message) && (message !== Links.latestFromFeed)) {
|
if (channelID === Config.channelID && Links.messageContainsLink(message) && (message !== Links.latestFromFeedlatestFeedLink
|
||||||
Log.event("Detected posted link in this message: " + message, "Discord.io");
|
Log.event("Detected posted link in this message: " + message, "Discord.io");
|
||||||
|
|
||||||
//extract the url from the string, and cache it
|
//extract the url from the string, and cache it
|
||||||
Uri.withinString(message, function (url) {
|
Uri.withinString(message, function (url) {
|
||||||
Links.cache(Links.standardise(url));
|
Links.cache(Links.standardise(url));
|
||||||
return url;
|
return url;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
checkPastMessagesForLinks: function () {
|
checkPastMessagesForLinks: function () {
|
||||||
var limit = 100;
|
var limit = 100;
|
||||||
Log.info("Attempting to check past " + limit + " messages for links");
|
Log.info("Attempting to check past " + limit + " messages for links");
|
||||||
|
@ -82,6 +86,25 @@ var DiscordClient = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
post: function (link) {
|
||||||
|
//send a messsage containing the new feed link to our discord channel
|
||||||
|
DiscordClient.bot.sendMessage({
|
||||||
|
to: Config.channelID,
|
||||||
|
message: link
|
||||||
|
}, function (err, message) {
|
||||||
|
if (err) {
|
||||||
|
Log.error("ERROR: Failed to send message: " + message.substring(0, 15) + "...", err);
|
||||||
|
//if there is an error posting the message, check if it is because the bot isn't connected
|
||||||
|
if (DiscordClient.bot.connected)
|
||||||
|
Log.info("Connectivity seems fine - I have no idea why the message didn't post");
|
||||||
|
else {
|
||||||
|
Log.error("DiscordClient appears to be disconnected! Attempting to reconnect...", err);
|
||||||
|
|
||||||
|
DiscordClient.bot.connect(); //attempt to reconnect
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -89,14 +112,13 @@ var YouTube = {
|
||||||
url: {
|
url: {
|
||||||
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) {
|
createFullUrl: function (shareUrl) {
|
||||||
return shareUrl.replace(YouTube.url.share, YouTube.url.full);
|
return shareUrl.replace(YouTube.url.share, YouTube.url.full);
|
||||||
},
|
},
|
||||||
convertFullToShare: function (fullUrl) {
|
createShareUrl: function (fullUrl) {
|
||||||
var shareUrl = fullUrl.replace(YouTube.url.full, YouTube.url.share);
|
var shareUrl = fullUrl.replace(YouTube.url.full, YouTube.url.share);
|
||||||
|
|
||||||
if (shareUrl.includes("&"))
|
if (shareUrl.includes("&")) shareUrl = shareUrl.slice(0, fullUrl.indexOf("&"));
|
||||||
shareUrl = shareUrl.slice(0, fullUrl.indexOf("&"));
|
|
||||||
|
|
||||||
return shareUrl;
|
return shareUrl;
|
||||||
}
|
}
|
||||||
|
@ -114,81 +136,64 @@ var Links = {
|
||||||
return messageLower.includes("http://") || messageLower.includes("https://") || messageLower.includes("www.");
|
return messageLower.includes("http://") || messageLower.includes("https://") || messageLower.includes("www.");
|
||||||
},
|
},
|
||||||
cached: [],
|
cached: [],
|
||||||
latestFromFeed: "",
|
latestFeedLink: "",
|
||||||
cache: function (link) {
|
cache: function (link) {
|
||||||
link = Links.standardise(link);
|
link = Links.standardise(link);
|
||||||
|
|
||||||
if (Config.youtubeMode && link.includes(YouTube.url.full)) {
|
if (Config.youtubeMode) link = YouTube.url.createShareUrl(link);
|
||||||
link = YouTube.url.convertFullToShare(link);
|
|
||||||
}
|
|
||||||
|
|
||||||
//store the new link if not stored already
|
//store the new link if not stored already
|
||||||
if (!Links.checkCache(link)) {
|
if (!Links.isCached(link)) {
|
||||||
Links.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
|
|
||||||
if (Links.cached.length > (Config.numLinksToCache || 10))
|
if (Links.cached.length > (Config.numLinksToCache || 10)) Links.cached.shift(); //get rid of the first array element if we have reached our cache limit
|
||||||
Links.cached.shift();
|
|
||||||
},
|
},
|
||||||
checkCache: function (link) {
|
isCached: function (link) {
|
||||||
link = Links.standardise(link);
|
link = Links.standardise(link);
|
||||||
|
|
||||||
if (Config.youtubeMode && link.includes(YouTube.url.full)) {
|
if (Config.youtubeMode)
|
||||||
return Links.cached.includes(YouTube.url.convertFullToShare(link));
|
return Links.cached.includes(YouTube.url.createShareUrl(link));
|
||||||
}
|
|
||||||
return Links.cached.includes(link);
|
return Links.cached.includes(link);
|
||||||
},
|
},
|
||||||
validateAndPost: function (err, articles) {
|
validate: function (err, articles, callback) {
|
||||||
if (err) Log.error("FEED ERROR: Error reading RSS feed.", err);
|
if (err) Log.error("FEED ERROR: Error reading RSS feed.", err);
|
||||||
else {
|
else {
|
||||||
var latestLink = Links.standardise(articles[0].link); //get the latest link and check if it has already been posted and cached
|
var latestLink = Links.standardise(articles[0].link); //get the latest link and check if it has already been posted and cached
|
||||||
|
|
||||||
//check whether the latest link out the feed exists in our cache
|
//make sure we don't spam the latest link
|
||||||
if (!Links.checkCache(latestLink)) {
|
if (latestLink == Links.latestFeedLink)
|
||||||
if (Config.youtubeMode && latestLink.includes(YouTube.url.full))
|
return;
|
||||||
latestLink = YouTube.url.convertFullToShare(latestLink);
|
|
||||||
Log.info("Attempting to post new link: " + latestLink);
|
|
||||||
|
|
||||||
//send a messsage containing the new feed link to our discord channel
|
//make sure the latest link hasn't been posted already
|
||||||
DiscordClient.bot.sendMessage({
|
if (Links.isCached(latestLink)) {
|
||||||
to: Config.channelID,
|
Log.info("Didn't post new feed link because already detected as posted " + latestLink);
|
||||||
message: latestLink
|
return;
|
||||||
}, function (err, message) {
|
|
||||||
if (err) {
|
|
||||||
Log.error("ERROR: Failed to send message: " + message.substring(0, 15) + "...", err);
|
|
||||||
//if there is an error posting the message, check if it is because the bot isn't connected
|
|
||||||
if (DiscordClient.bot.connected)
|
|
||||||
Log.info("Connectivity seems fine - I have no idea why the message didn't post");
|
|
||||||
else {
|
|
||||||
Log.error("DiscordClient appears to be disconnected! Attempting to reconnect...", err);
|
|
||||||
|
|
||||||
DiscordClient.bot.connect(); //attempt to reconnect
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Links.cache(latestLink); //finally make sure the link is cached, so it doesn't get posted again
|
|
||||||
}
|
}
|
||||||
else if (Links.latestFromFeed != latestLink)
|
|
||||||
Log.info("Didn't post new feed link because already detected as posted " + latestLink); //alternatively, if we have a new link from the feed, but its been posted already, just alert the console
|
|
||||||
|
|
||||||
Links.latestFromFeed = latestLink; //ensure our latest feed link variable is up to date, so we can track when the feed updates
|
if (Config.youtubeMode) latestLink = YouTube.url.createShareUrl(latestLink);
|
||||||
|
|
||||||
|
callback(latestLink);
|
||||||
|
|
||||||
|
Links.cache(latestLink); //make sure the link is cached, so it doesn't get posted again
|
||||||
|
Links.latestFeedLink = latestLink; //ensure our latest feed link variable is up to date, so we can track when the feed updates
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var Feed = {
|
var Feed = {
|
||||||
urlObj: Url.parse(Config.feedUrl),
|
urlObj: Url.parse(Config.feedUrl),
|
||||||
checkAndPost: function () {
|
check: function (callback) {
|
||||||
Dns.resolve(Feed.urlObj.host, function (err) { //check that we have an internet connection (well not exactly - check that we have a connection to the host of the feedUrl)
|
Dns.resolve(Feed.urlObj.host, function (err) { //check that we have an internet connection (well not exactly - check that we have a connection to the host of the feedUrl)
|
||||||
if (err) Log.error("CONNECTION ERROR: Cannot resolve host.", err);
|
if (err) Log.error("CONNECTION ERROR: Cannot resolve host.", err);
|
||||||
else FeedRead(Config.feedUrl, Links.validateAndPost);
|
else callback();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var intervalFunc = () => {}; //do nothing by default
|
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 () {
|
||||||
|
|
Loading…
Reference in New Issue