Add feed class methods for retrieving links in past messages

Also added guild property to the model, for identification
This commit is contained in:
benji7425 2017-07-29 20:31:55 +01:00
parent b8d6fe8d3c
commit 084e805f39
2 changed files with 30 additions and 7 deletions

30
app/models/feed-data.js Normal file
View File

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

View File

@ -1,7 +0,0 @@
module.exports = class Feed{
constructor({link, channelName, roleID}){
this.link = link;
this.channelName = channelName;
this.roleID = roleID;
}
};