Browse Source

Add writing back to data file after all guilds update their caches

shorten
benji7425 4 years ago
parent
commit
c82bece834
  1. 13
      app/index.js

13
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); });
}
Loading…
Cancel
Save