2016-10-29 21:09:10 +03:00
|
|
|
var console = require("console");
|
2016-10-29 20:31:16 +03:00
|
|
|
var Discord = require("discord.io");
|
2016-10-29 21:09:10 +03:00
|
|
|
var FeedRead = require("feed-read");
|
|
|
|
var BotConfig = require("./botConfig.json");
|
2016-10-29 20:31:16 +03:00
|
|
|
var Config = require("./config.json");
|
|
|
|
|
|
|
|
var bot = new Discord.Client({
|
2016-10-29 21:09:10 +03:00
|
|
|
token: BotConfig.token,
|
2016-10-29 20:31:16 +03:00
|
|
|
autorun: true
|
|
|
|
});
|
|
|
|
|
|
|
|
bot.on("ready", function () {
|
|
|
|
console.log(bot.username + " - (" + bot.id + ")");
|
2016-10-29 21:09:10 +03:00
|
|
|
|
|
|
|
setInterval(checkFeedAndPost, Config.pollingInterval);
|
2016-10-29 20:31:16 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
bot.on("message", function (user, userID, channelID, message) {
|
|
|
|
if (message === "ping") {
|
|
|
|
bot.sendMessage({
|
|
|
|
to: channelID,
|
|
|
|
message: "pong"
|
|
|
|
});
|
|
|
|
}
|
2016-10-29 21:09:10 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
function checkFeedAndPost() {
|
|
|
|
//check the feed, with a callback
|
2016-10-30 21:39:46 +02:00
|
|
|
FeedRead(Config.feedUrl, function (err, articles){
|
|
|
|
try{
|
|
|
|
checkLinkAndPost(err, articles);
|
|
|
|
}
|
|
|
|
catch(ex){
|
|
|
|
console.log("ERROR: " + (ex.message || ex));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-10-29 21:09:10 +03:00
|
|
|
|
2016-10-30 21:39:46 +02:00
|
|
|
function checkLinkAndPost(err, articles) {
|
|
|
|
if (err) throw "Error reading RSS feed: " + (err.message || err);
|
2016-10-29 21:09:10 +03:00
|
|
|
|
2016-10-30 21:39:46 +02:00
|
|
|
var latestLink = articles[0].link;
|
2016-10-29 21:09:10 +03:00
|
|
|
|
2016-10-30 21:39:46 +02:00
|
|
|
//get the latest 100 messages (100 is the limit)
|
|
|
|
bot.getMessages({
|
|
|
|
channelID: Config.channelID,
|
|
|
|
limit: 100
|
|
|
|
}, function (err, messages) {
|
|
|
|
if (err) throw err;
|
2016-10-29 21:09:10 +03:00
|
|
|
|
2016-10-30 21:39:46 +02:00
|
|
|
//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
|
|
|
|
});
|
2016-10-29 21:09:10 +03:00
|
|
|
});
|
|
|
|
}
|