Merged feature/feed-reading into develop
This commit is contained in:
commit
fb129feb8f
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -54,4 +54,4 @@ jspm_packages
|
|||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
/config.json
|
||||
/botConfig.json
|
|
@ -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
5
config.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"feedUrl": "https://www.youtube.com/feeds/videos.xml?user=EthosLab",
|
||||
"channelID": "241238530376990722",
|
||||
"pollingInterval": 5000
|
||||
}
|
39
feed-bot.js
39
feed-bot.js
|
@ -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
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
|
@ -17,6 +17,7 @@
|
|||
},
|
||||
"homepage": "https://github.com/benji7425/discord-feed-bot#readme",
|
||||
"dependencies": {
|
||||
"discord.io": "^2.2.4"
|
||||
"discord.io": "^2.2.4",
|
||||
"feed-read": "0.0.1"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue