From 084e805f394b87b5a2871a412179b2d28ab952bf Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 29 Jul 2017 20:31:55 +0100 Subject: [PATCH] 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