diff --git a/app/index.js b/app/index.js index 00dcad7..c9633e1 100644 --- a/app/index.js +++ b/app/index.js @@ -2,8 +2,12 @@ const FileSystem = require("fs"); //external lib imports +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"); @@ -13,12 +17,28 @@ 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).then(writeFile); + //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) { + const promises = []; + for (let guild of guilds) { + const guildData = guildsData[guild.id]; + if (guildData) + 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 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 diff --git a/app/models/guild-data.js b/app/models/guild-data.js index 6ee120c..82de0f9 100644 --- a/app/models/guild-data.js +++ b/app/models/guild-data.js @@ -1,8 +1,19 @@ -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() { + const promises = []; + + this.feeds.forEach(feed => { + promises.push(feed.cachePastPostedLinks(this).catch(Util.dateError)); + }); + + return Promise.all(promises); } }; \ No newline at end of file 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" } }