From ab300263ab3bc4d26cff3e7844fafbd1f22bfdfa Mon Sep 17 00:00:00 2001 From: benji7425 Date: Wed, 23 Aug 2017 01:04:56 +0100 Subject: [PATCH] Add detection of links being posted and fix startup links retrieval --- app/index.js | 22 +++++++++++++--------- app/models/feed-data.js | 12 ++++++++++-- app/models/guild-data.js | 4 ++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/index.js b/app/index.js index e20e4ea..0c62d60 100644 --- a/app/index.js +++ b/app/index.js @@ -22,11 +22,9 @@ module.exports = (client) => { const guildsData = FileSystem.existsSync(SAVE_FILE) ? fromJSON(JsonFile.readFileSync(SAVE_FILE)) : {}; setInterval(() => writeFile(guildsData), config.saveIntervalSec * 1000); - parseLinksInGuilds(client.guilds, guildsData).then(writeFile(guildsData)); - - //set up an interval to check all the feeds - checkFeedsInGuilds(client.guilds, guildsData); - setInterval(() => checkFeedsInGuilds(client.guilds, guildsData), config.feedCheckIntervalSec * 1000); + parseLinksInGuilds(client.guilds, guildsData).then(() => writeFile(guildsData)) + .then(() => checkFeedsInGuilds(client.guilds, guildsData)) + .then(() => setInterval(() => checkFeedsInGuilds(client.guilds, guildsData), config.feedCheckIntervalSec * 1000)); //set up an interval to check all the feeds //set up an on message handler to detect when links are posted client.on("message", message => { @@ -59,6 +57,12 @@ const HandleMessage = { break; } } + else if (guildsData[message.guild.id]) { + guildsData[message.guild.id].feeds.forEach(feedData => { + if (message.channel.name === feedData.channelName) + feedData.cachedLinks.push(...GetUrls(message.content)); //spread the urlSet returned by GetUrls into the cache array + }); + } } }; @@ -76,7 +80,7 @@ function addFeed(client, guildsData, message) { const feedData = new FeedData({ url: feedUrl, channelName: channel.name, - roleName: role.name + roleName: role ? role.name : null }); //ask the user if they're happy with the details they set up, save if yes, don't if no @@ -103,10 +107,10 @@ function checkFeedsInGuilds(guilds, guildsData) { function parseLinksInGuilds(guilds, guildsData) { const promises = []; - for (let guild of guilds) { - const guildData = guildsData[guild.id]; + for (let guildId of guilds.keys()) { + const guildData = guildsData[guildId]; if (guildData) - promises.push(guildData.cachePastPostedLinks()); + promises.push(guildData.cachePastPostedLinks(guilds.get(guildId))); } return Promise.all(promises); } diff --git a/app/models/feed-data.js b/app/models/feed-data.js index 7ff2e80..2372304 100644 --- a/app/models/feed-data.js +++ b/app/models/feed-data.js @@ -5,6 +5,7 @@ const DiscordUtil = require("discordjs-util"); const Dns = require("dns"); //for host resolution checking const Url = require("url"); //for url parsing const FeedRead = require("feed-read"); //for extracing new links from RSS feeds +const GetUrls = require("get-urls"); //for extracting urls from messages module.exports = class FeedData { constructor({ url, channelName, roleName, cachedLinks }) { @@ -12,6 +13,13 @@ module.exports = class FeedData { this.channelName = channelName; this.roleName = roleName; this.cachedLinks = cachedLinks || []; + + this.cachedLinks.push = (...elements) => { + const unique = elements + .map(el => normaliseUrl(el)) //normalise all the urls + .filter(el => !this.cachedLinks.includes(el)); //filter out any already cached + Array.prototype.push.apply(this.cachedLinks, unique); + }; } /** @@ -25,7 +33,7 @@ module.exports = class FeedData { return new Promise((resolve, reject) => { channel.fetchMessages({ limit: 100 }) .then(messages => { - messages.forEach(m => Array.prototype.push.apply(this.cachedLinks, getUrls(m))); //push all the links in each message into our links array + messages.forEach(m => this.cachedLinks.push(...GetUrls(m.content))); //push all the links in each message into our links array resolve(this); }) .catch(reject); @@ -64,7 +72,7 @@ function normaliseUrl(url) { if (Url.parse(url).host.includes("youtu")) //detect youtu.be and youtube.com - yes I know it's hacky url = url.split("&")[0]; //quick way to chop off stuff like &feature=youtube - url = url.replace("http://www.youtube.com/watch?v=", "http://youtu.be/"); //turn full url into share url + url = url.replace(/(www.)?youtube.com\/watch\?v=/, "youtu.be/"); //turn full url into share url return url; } diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 94253d2..d1ea352 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -7,11 +7,11 @@ module.exports = class GuildData { this.feeds = feeds.map(feed => new FeedData(feed)); } - cachePastPostedLinks() { + cachePastPostedLinks(guild) { const promises = []; this.feeds.forEach(feed => { - promises.push(feed.cachePastPostedLinks(this).catch(Util.dateError)); + promises.push(feed.updatePastPostedLinks(guild).catch(Util.dateError)); }); return Promise.all(promises);