Merged feature/error-handling into develop

This commit is contained in:
benji7425 2016-10-30 20:15:25 +00:00
commit 986e776508
2 changed files with 113 additions and 33 deletions

46
.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,46 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}\\feed-bot.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": false,
"outFiles": []
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outFiles": [],
"localRoot": "${workspaceRoot}",
"remoteRoot": null
},
{
"name": "Attach to Process",
"type": "node",
"request": "attach",
"processId": "${command.PickProcess}",
"port": 5858,
"sourceMaps": false,
"outFiles": []
}
]
}

View file

@ -1,52 +1,86 @@
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) {
//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);
var latestLink = articles[0].link;
//get the latest 100 messages (100 is the limit)
bot.getMessages({
channelID: Config.channelID,
limit: 100
}, function (err, messages) {
if (err) throw err;
var latestLink = articles[0].link;
//get an array of strings from the array of message objects
var messageContents = messages.map((message) => { return message.content; });
//get the latest 100 messages (100 is the limit)
bot.getMessages({
channelID: Config.channelID,
limit: 100
}, function (err, messages) {
if (err) throw err;
//get an array of strings from the array of message objects
var messageContents = messages.map((message) => { return message.content; });
//if the messageContents array doesn't include the latest link, post it
if (!messageContents.includes(latestLink))
bot.sendMessage({
to: Config.channelID,
message: latestLink
});
});
//if the messageContents array doesn't include the latest link, post it
if (!messageContents.includes(latestLink))
bot.sendMessage({
to: Config.channelID,
message: latestLink
});
});
}