Merge branch 'past-link-retrieval' into v2
This commit is contained in:
commit
526f66238c
20
app/index.js
20
app/index.js
|
@ -2,8 +2,12 @@
|
||||||
const FileSystem = require("fs");
|
const FileSystem = require("fs");
|
||||||
|
|
||||||
//external lib imports
|
//external lib imports
|
||||||
|
const Dicsord = require("discord.js");
|
||||||
const JSONFile = require("jsonfile");
|
const JSONFile = require("jsonfile");
|
||||||
|
|
||||||
|
//my imports
|
||||||
|
const Util = require("discordjs-util");
|
||||||
|
|
||||||
//app component imports
|
//app component imports
|
||||||
const GuildData = require("./models/guild-data.js");
|
const GuildData = require("./models/guild-data.js");
|
||||||
|
|
||||||
|
@ -13,12 +17,28 @@ const SAVE_FILE = "./guilds.json";
|
||||||
module.exports = (client) => {
|
module.exports = (client) => {
|
||||||
const guildsData = FileSystem.existsSync(SAVE_FILE) ? parseJSON(JSONFile.readFileSync(SAVE_FILE)) : {}; //pull saved data from file
|
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 interval to check all the feeds
|
||||||
|
|
||||||
//set up an on message handler to detect when links are posted
|
//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) {
|
function parseJSON(json) {
|
||||||
const guildIDs = Object.keys(json);
|
const guildIDs = Object.keys(json);
|
||||||
guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); });
|
guildIDs.forEach(guildID => { guildIDs[guildID] = new GuildData(guildIDs[guildID]); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function writeFile(guildsData){
|
||||||
|
JSONFile.write(SAVE_FILE, guildsData, err => {if(err) Util.dateError(err); });
|
||||||
|
}
|
30
app/models/feed-data.js
Normal file
30
app/models/feed-data.js
Normal 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);
|
||||||
|
}
|
|
@ -1,7 +0,0 @@
|
||||||
module.exports = class Feed{
|
|
||||||
constructor({link, channelName, roleID}){
|
|
||||||
this.link = link;
|
|
||||||
this.channelName = channelName;
|
|
||||||
this.roleID = roleID;
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -1,8 +1,19 @@
|
||||||
const Feed = require("./feed.js");
|
const FeedData = require("./feed-data.js");
|
||||||
|
const Util = require("discordjs-util");
|
||||||
|
|
||||||
module.exports = class GuildData {
|
module.exports = class GuildData {
|
||||||
constructor({ id, feeds }) {
|
constructor({ id, feeds }) {
|
||||||
this.id = id;
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -21,6 +21,7 @@
|
||||||
"discordjs-util": "git+https://github.com/benji7425/discordjs-util.git",
|
"discordjs-util": "git+https://github.com/benji7425/discordjs-util.git",
|
||||||
"dns": "0.2.2",
|
"dns": "0.2.2",
|
||||||
"feed-read": "0.0.1",
|
"feed-read": "0.0.1",
|
||||||
|
"jsonfile": "3.0.1",
|
||||||
"urijs": "1.18.10"
|
"urijs": "1.18.10"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue