diff --git a/config.json b/config.json index ebef3c9..f3c03d8 100644 --- a/config.json +++ b/config.json @@ -1,20 +1,16 @@ { - "feedUrls": [ - "https://www.youtube.com/feeds/videos.xml?user=EthosLab", - "https://www.youtube.com/feeds/videos.xml?user=VintageBeef" + "feeds": [ + { "url": "https://www.youtube.com/feeds/videos.xml?user=CorridorDigital", "roleID": "272788856447959040" }, + { "url": "https://www.youtube.com/feeds/videos.xml?user=samandniko" }, + { "url": "https://www.youtube.com/feeds/videos.xml?user=Node", "roleID": "306212762504134667" } ], "channelID": "264420391282409473", - "serverID": "264420391282409473", "pollingInterval": 5000, "numLinksToCache": 10, "messageDeleteDelay": 10000, "youtubeMode": true, "allowSubscriptions": true, - "subscribersRoleID": "272788856447959040", - "logFile": "./log", "userCommands": { - "subscribe": "!subscribe", - "unsubscribe": "!unsubscribe", "help": "!help" }, "developerCommands": { diff --git a/index.js b/index.js index fe8a5a2..d6189dc 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,6 @@ var Dns = require("dns"); //for connectivity checking var Url = require("url"); //for url parsing var Uri = require("urijs"); //for finding urls within message strings var FeedRead = require("feed-read"); //for rss feed reading -var JsonFile = require("jsonfile"); //reading/writing json var Console = require("console"); //my imports @@ -15,14 +14,9 @@ module.exports = { //set the interval function to check the feed intervalFunc = () => { - var callback = (err, articles) => { - Links.validate(err, articles, (latestLink) => Actions.post(bot, latestLink)); - }; + var callback = (err, articles, feed) => Links.validate(err, articles, (latestLink) => Actions.post(bot, latestLink, feed.roleID)); - if (Config.feedUrls.length > 1) - Feed.checkMultiple(Config.feedUrls, callback); - else - Feed.check(Config.feedUrls[0], callback); + Feed.checkFeeds(Config.feeds, callback); }; setInterval(() => { intervalFunc(); }, Config.pollingInterval); @@ -41,18 +35,6 @@ module.exports = { }, commands: [ - { - command: Config.userCommands.subscribe, - type: "equals", - action: (bot, user, userID, channelID, message) => { if (Config.allowSubscriptions) Subscriptions.subscribe(bot, user, userID, channelID, message); }, - channelIDs: [Config.channelID] - }, - { - command: Config.userCommands.unsubscribe, - type: "equals", - action: (bot, user, userID, channelID, message) => { if (Config.allowSubscriptions) Subscriptions.unsubscribe(bot, user, userID, channelID, message); }, - channelIDs: [Config.channelID] - }, { command: Config.userCommands.help, type: "equals", @@ -90,11 +72,11 @@ module.exports = { }; var Actions = { - post: (bot, link) => { + post: (bot, link, roleID) => { //send a messsage containing the new feed link to our discord channel bot.sendMessage({ to: Config.channelID, - message: Subscriptions.mention() + link + message: ((roleID !== "" && roleID !== undefined) ? "<@&" + roleID + ">" : "") + " " + link }); }, checkPastMessagesForLinks: (bot) => { @@ -127,52 +109,6 @@ var Actions = { }, }; -var Subscriptions = { - subscribe: function (bot, user, userID, channelID, message) { - bot.addToRole({ - serverID: Config.serverID, - userID: userID, - roleID: Config.subscribersRoleID - }, - (err) => { - if (err) Console.log(err); //log the error if there is an error - else { //else go ahead and confirm subscription - Console.info("Subscribed user " + (user ? user + "(" + userID + ")" : userID)); - - bot.sendMessage({ - to: channelID, - message: "You have successfully subscribed" - }, (err, response) => { setTimeout(() => { bot.deleteMessage({ channelID: channelID, messageID: response.id }); }, Config.messageDeleteDelay); }); //delete the subscription confirmation message after a delay - } - }); - - - }, - - unsubscribe: function (bot, user, userID, channelID, message) { - bot.removeFromRole({ - serverID: Config.serverID, - userID: userID, - roleID: Config.subscribersRoleID - }, - (err) => { - if (err) Console.log(err); //log the error if there is an error - else { //else go ahead and confirm un-subscription - Console.info("Unsubscribed user " + (user ? user + "(" + userID + ")" : userID)); - - bot.sendMessage({ - to: channelID, - message: "You have successfully unsubscribed" - }, (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 + "> " : ""; - } -}; - var YouTube = { url: { share: "http://youtu.be/", @@ -246,17 +182,11 @@ var Links = { }; var Feed = { - check: function (feedUrl, callback) { - 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) - if (err) Console.error("CONNECTION ERROR: Cannot resolve host.", err); - else FeedRead(feedUrl, callback); - }); - }, - checkMultiple: function (feedUrls, individualCallback) { - feedUrls.forEach((url) => { - Dns.resolve(Url.parse(url).host, (err) => { + checkFeeds: function (feeds, individualCallback) { + feeds.forEach((feed) => { + Dns.resolve(Url.parse(feed.url).host, (err) => { if (err) Console.error("CONNECTION ERROR: Cannot resolve host.", err); - else FeedRead(url, individualCallback); + else FeedRead(feed.url, (err, articles) => individualCallback(err, articles, feed)); }); }); }