Browse Source

Add different role mentioning for different feeds

shorten
benji7425 4 years ago
parent
commit
cdd7cb056b
  1. 10
      config.json
  2. 38
      index.js

10
config.json

@ -1,17 +1,15 @@
{
"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",

38
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));
};
if (Config.feedUrls.length > 1)
Feed.checkMultiple(Config.feedUrls, callback);
else
Feed.check(Config.feedUrls[0], callback);
var callback = (err, articles, feed) => Links.validate(err, articles, (latestLink) => Actions.post(bot, latestLink, feed.roleID));
Feed.checkFeeds(Config.feeds, callback);
};
setInterval(() => { intervalFunc(); }, Config.pollingInterval);
@ -90,11 +84,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) => {
@ -130,7 +124,7 @@ var Actions = {
var Subscriptions = {
subscribe: function (bot, user, userID, channelID, message) {
bot.addToRole({
serverID: Config.serverID,
serverID: bot.channels[Config.channelID].guild_id,
userID: userID,
roleID: Config.subscribersRoleID
},
@ -151,7 +145,7 @@ var Subscriptions = {
unsubscribe: function (bot, user, userID, channelID, message) {
bot.removeFromRole({
serverID: Config.serverID,
serverID: bot.channels[Config.channelID].guild_id,
userID: userID,
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
}
});
},
mention: function () {
return Config.allowSubscriptions ? "<@&" + Config.subscribersRoleID + "> " : "";
}
};
@ -246,17 +236,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));
});
});
}

Loading…
Cancel
Save