Added link posting if link not found posted in last 100 messages

This commit is contained in:
benji7425 2016-10-29 19:09:10 +01:00
parent 8a8c2f88e6
commit 961c167108
4 changed files with 42 additions and 6 deletions

2
.gitignore vendored
View file

@ -54,4 +54,4 @@ jspm_packages
# Output of 'npm pack'
*.tgz
/config.json
/botConfig.json

View file

@ -1,6 +1,6 @@
# Development
Requires a config.json file with field "token" containing your bot token
Requires a botConfig.json file with field "token" containing your bot token
`{
"token": "abc123blahblahblahyourtokengoeshere"

5
config.json Normal file
View file

@ -0,0 +1,5 @@
{
"feedUrl": "https://www.youtube.com/feeds/videos.xml?user=EthosLab",
"channelID": "241238530376990722",
"pollingInterval": 5000
}

View file

@ -1,14 +1,18 @@
var Discord = require("discord.io");
var Config = require("./config.json");
var console = require("console");
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: Config.token,
token: BotConfig.token,
autorun: true
});
bot.on("ready", function () {
console.log(bot.username + " - (" + bot.id + ")");
setInterval(checkFeedAndPost, Config.pollingInterval);
});
bot.on("message", function (user, userID, channelID, message) {
@ -18,4 +22,31 @@ bot.on("message", function (user, userID, channelID, message) {
message: "pong"
});
}
});
});
function checkFeedAndPost() {
//check the feed, with a callback
FeedRead(Config.feedUrl, function (err, articles) {
if (err) throw 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;
//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
});
});
});
}