Add detection of links being posted and fix startup links retrieval

This commit is contained in:
benji7425 2017-08-23 01:04:56 +01:00
parent d5583b2c04
commit ab300263ab
3 changed files with 25 additions and 13 deletions

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);