Add different role mentioning for different feeds

This commit is contained in:
benji7425 2017-05-17 18:52:27 +01:00
parent 698155a38f
commit cdd7cb056b
2 changed files with 14 additions and 32 deletions

View file

@ -1,17 +1,15 @@
{ {
"feedUrls": [ "feeds": [
"https://www.youtube.com/feeds/videos.xml?user=EthosLab", { "url": "https://www.youtube.com/feeds/videos.xml?user=CorridorDigital", "roleID": "272788856447959040" },
"https://www.youtube.com/feeds/videos.xml?user=VintageBeef" { "url": "https://www.youtube.com/feeds/videos.xml?user=samandniko" },
{ "url": "https://www.youtube.com/feeds/videos.xml?user=Node", "roleID": "306212762504134667" }
], ],
"channelID": "264420391282409473", "channelID": "264420391282409473",
"serverID": "264420391282409473",
"pollingInterval": 5000, "pollingInterval": 5000,
"numLinksToCache": 10, "numLinksToCache": 10,
"messageDeleteDelay": 10000, "messageDeleteDelay": 10000,
"youtubeMode": true, "youtubeMode": true,
"allowSubscriptions": true, "allowSubscriptions": true,
"subscribersRoleID": "272788856447959040",
"logFile": "./log",
"userCommands": { "userCommands": {
"subscribe": "!subscribe", "subscribe": "!subscribe",
"unsubscribe": "!unsubscribe", "unsubscribe": "!unsubscribe",

View file

@ -3,7 +3,6 @@ var Dns = require("dns"); //for connectivity checking
var Url = require("url"); //for url parsing var Url = require("url"); //for url parsing
var Uri = require("urijs"); //for finding urls within message strings var Uri = require("urijs"); //for finding urls within message strings
var FeedRead = require("feed-read"); //for rss feed reading var FeedRead = require("feed-read"); //for rss feed reading
var JsonFile = require("jsonfile"); //reading/writing json
var Console = require("console"); var Console = require("console");
//my imports //my imports
@ -15,14 +14,9 @@ module.exports = {
//set the interval function to check the feed //set the interval function to check the feed
intervalFunc = () => { intervalFunc = () => {
var callback = (err, articles) => { var callback = (err, articles, feed) => Links.validate(err, articles, (latestLink) => Actions.post(bot, latestLink, feed.roleID));
Links.validate(err, articles, (latestLink) => Actions.post(bot, latestLink));
};
if (Config.feedUrls.length > 1) Feed.checkFeeds(Config.feeds, callback);
Feed.checkMultiple(Config.feedUrls, callback);
else
Feed.check(Config.feedUrls[0], callback);
}; };
setInterval(() => { intervalFunc(); }, Config.pollingInterval); setInterval(() => { intervalFunc(); }, Config.pollingInterval);
@ -90,11 +84,11 @@ module.exports = {
}; };
var Actions = { var Actions = {
post: (bot, link) => { post: (bot, link, roleID) => {
//send a messsage containing the new feed link to our discord channel //send a messsage containing the new feed link to our discord channel
bot.sendMessage({ bot.sendMessage({
to: Config.channelID, to: Config.channelID,
message: Subscriptions.mention() + link message: ((roleID !== "" && roleID !== undefined) ? "<@&" + roleID + ">" : "") + " " + link
}); });
}, },
checkPastMessagesForLinks: (bot) => { checkPastMessagesForLinks: (bot) => {
@ -130,7 +124,7 @@ var Actions = {
var Subscriptions = { var Subscriptions = {
subscribe: function (bot, user, userID, channelID, message) { subscribe: function (bot, user, userID, channelID, message) {
bot.addToRole({ bot.addToRole({
serverID: Config.serverID, serverID: bot.channels[Config.channelID].guild_id,
userID: userID, userID: userID,
roleID: Config.subscribersRoleID roleID: Config.subscribersRoleID
}, },
@ -151,7 +145,7 @@ var Subscriptions = {
unsubscribe: function (bot, user, userID, channelID, message) { unsubscribe: function (bot, user, userID, channelID, message) {
bot.removeFromRole({ bot.removeFromRole({
serverID: Config.serverID, serverID: bot.channels[Config.channelID].guild_id,
userID: userID, userID: userID,
roleID: Config.subscribersRoleID roleID: Config.subscribersRoleID
}, },
@ -166,10 +160,6 @@ var Subscriptions = {
}, (err, response) => { setTimeout(() => { bot.deleteMessage({ channelID: channelID, messageID: response.id }); }, Config.messageDeleteDelay); }); //delete the un-subscription confirmation message after a delay }, (err, response) => { setTimeout(() => { bot.deleteMessage({ channelID: channelID, messageID: response.id }); }, Config.messageDeleteDelay); }); //delete the un-subscription confirmation message after a delay
} }
}); });
},
mention: function () {
return Config.allowSubscriptions ? "<@&" + Config.subscribersRoleID + "> " : "";
} }
}; };
@ -246,17 +236,11 @@ var Links = {
}; };
var Feed = { var Feed = {
check: function (feedUrl, callback) { checkFeeds: function (feeds, individualCallback) {
Dns.resolve(Url.parse(feedUrl).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) feeds.forEach((feed) => {
Dns.resolve(Url.parse(feed.url).host, (err) => {
if (err) Console.error("CONNECTION ERROR: Cannot resolve host.", err); if (err) Console.error("CONNECTION ERROR: Cannot resolve host.", err);
else FeedRead(feedUrl, callback); else FeedRead(feed.url, (err, articles) => individualCallback(err, articles, feed));
});
},
checkMultiple: function (feedUrls, individualCallback) {
feedUrls.forEach((url) => {
Dns.resolve(Url.parse(url).host, (err) => {
if (err) Console.error("CONNECTION ERROR: Cannot resolve host.", err);
else FeedRead(url, individualCallback);
}); });
}); });
} }