From b8d6fe8d3ca04e668f7b54ffc706f48400ebe2ce Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:28:00 +0100 Subject: [PATCH 1/6] Install jsonfile (it was missing) --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 795442e..1223f18 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "discordjs-util": "git+https://github.com/benji7425/discordjs-util.git", "dns": "0.2.2", "feed-read": "0.0.1", + "jsonfile": "3.0.1", "urijs": "1.18.10" } } From 084e805f394b87b5a2871a412179b2d28ab952bf Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:31:55 +0100 Subject: [PATCH 2/6] Add feed class methods for retrieving links in past messages Also added guild property to the model, for identification --- app/models/feed-data.js | 30 ++++++++++++++++++++++++++++++ app/models/feed.js | 7 ------- 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 app/models/feed-data.js delete mode 100644 app/models/feed.js diff --git a/app/models/feed-data.js b/app/models/feed-data.js new file mode 100644 index 0000000..6179f07 --- /dev/null +++ b/app/models/feed-data.js @@ -0,0 +1,30 @@ +module.exports = class FeedData { + constructor({ link, channelName, roleID, cachedLinks }) { + this.link = link; + this.channelName = channelName; + this.roleID = roleID; + this.cachedLinks = cachedLinks; + } + + /** + * Returns a promise providing all the links posted in the last 100 messages + * @param {Discord.Guild} guild The guild this feed belongs to + * @returns {Promise} Links posted in last 100 messages + */ + updatePastPostedLinks(guild) { + const channel = guild.channels.find(ch => ch.type === "text" && ch.name === this.channelName); + + 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 + resolve(this); + }) + .catch(reject); + }); + } +}; + +function getUrls(str) { + return str.match(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig); +} \ No newline at end of file diff --git a/app/models/feed.js b/app/models/feed.js deleted file mode 100644 index f9281a1..0000000 --- a/app/models/feed.js +++ /dev/null @@ -1,7 +0,0 @@ -module.exports = class Feed{ - constructor({link, channelName, roleID}){ - this.link = link; - this.channelName = channelName; - this.roleID = roleID; - } -}; \ No newline at end of file From 36aa5639abe9d8c887b2f272c4ed58d71e1aae6f Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:32:33 +0100 Subject: [PATCH 3/6] Add method for recursively iterating over feeds in guild --- app/models/guild-data.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 6ee120c..53941dd 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -1,8 +1,21 @@ -const Feed = require("./feed.js"); +const FeedData = require("./feed-data.js"); +const Util = require("discordjs-util"); module.exports = class GuildData { - constructor({id, feeds}) { + constructor({ id, feeds }) { this.id = id; - this.feeds = feeds.filter(feed => new Feed(feed)); + this.feeds = feeds.filter(feed => new FeedData(feed)); + } + + cachePastPostedLinks() { + let i = 0; + const recurse = () => { + this.feeds[i++].cachePastPostedLinks(this) + .catch(Util.dateError) + .then(recurse); + if (i > this.feeds.length) + return Promise.resolve(); + }; + } }; \ No newline at end of file From 02b400266daa98dfb965d29ea5c8ad722385fcab Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:33:05 +0100 Subject: [PATCH 4/6] Add method to iterate over all guild to update their feed caches --- app/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/index.js b/app/index.js index 00dcad7..90894e9 100644 --- a/app/index.js +++ b/app/index.js @@ -2,6 +2,7 @@ const FileSystem = require("fs"); //external lib imports +const Dicsord = require("discord.js"); const JSONFile = require("jsonfile"); //app component imports @@ -13,11 +14,21 @@ const SAVE_FILE = "./guilds.json"; module.exports = (client) => { const guildsData = FileSystem.existsSync(SAVE_FILE) ? parseJSON(JSONFile.readFileSync(SAVE_FILE)) : {}; //pull saved data from file + parseLinksInAllGuilds(client.guilds, guildsData); + //set up an interval to check all the feeds //set up an on message handler to detect when links are posted }; +function parseLinksInAllGuilds(guilds, guildsData) { + for (let guild of guilds) { + const guildData = guildsData[guild.id]; + if (guildData) + guildData.cachePastPostedLinks(); + } +} + function parseJSON(json) { const guildIDs = Object.keys(json); guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); }); From c82bece834ae0dbebfdc974a57f52c93f056b713 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:36:41 +0100 Subject: [PATCH 5/6] Add writing back to data file after all guilds update their caches --- app/index.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/index.js b/app/index.js index 90894e9..c9633e1 100644 --- a/app/index.js +++ b/app/index.js @@ -5,6 +5,9 @@ const FileSystem = require("fs"); const Dicsord = require("discord.js"); const JSONFile = require("jsonfile"); +//my imports +const Util = require("discordjs-util"); + //app component imports const GuildData = require("./models/guild-data.js"); @@ -14,7 +17,7 @@ const SAVE_FILE = "./guilds.json"; module.exports = (client) => { const guildsData = FileSystem.existsSync(SAVE_FILE) ? parseJSON(JSONFile.readFileSync(SAVE_FILE)) : {}; //pull saved data from file - parseLinksInAllGuilds(client.guilds, guildsData); + parseLinksInAllGuilds(client.guilds, guildsData).then(writeFile); //set up an interval to check all the feeds @@ -22,14 +25,20 @@ module.exports = (client) => { }; function parseLinksInAllGuilds(guilds, guildsData) { + const promises = []; for (let guild of guilds) { const guildData = guildsData[guild.id]; if (guildData) - guildData.cachePastPostedLinks(); + promises.push(guildData.cachePastPostedLinks()); } + return Promise.all(promises); } function parseJSON(json) { const guildIDs = Object.keys(json); guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); }); +} + +function writeFile(guildsData){ + JSONFile.write(SAVE_FILE, guildsData, err => {if(err) Util.dateError(err); }); } \ No newline at end of file From 5e5929565ebba1a35cf7cf993d9996da6fe7038f Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:40:55 +0100 Subject: [PATCH 6/6] Fix mistake whereby the caches for each guild would update synchronously --- app/models/guild-data.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 53941dd..82de0f9 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -8,14 +8,12 @@ module.exports = class GuildData { } cachePastPostedLinks() { - let i = 0; - const recurse = () => { - this.feeds[i++].cachePastPostedLinks(this) - .catch(Util.dateError) - .then(recurse); - if (i > this.feeds.length) - return Promise.resolve(); - }; + const promises = []; + + this.feeds.forEach(feed => { + promises.push(feed.cachePastPostedLinks(this).catch(Util.dateError)); + }); + return Promise.all(promises); } }; \ No newline at end of file