Add removal of data on startup for guilds deleted during downtime

This commit is contained in:
benji7425 2017-12-08 00:07:02 +00:00
parent 8dfbab0122
commit da1edc57f0
2 changed files with 24 additions and 12 deletions

View file

@ -52,7 +52,10 @@ function doGuildIteration() {
function parseLinksInGuilds() { function parseLinksInGuilds() {
const promises = []; const promises = [];
client.guildDataModel.find().then(guildDatas => client.guildDataModel.find().then(guildDatas =>
guildDatas.forEach(guildData => promises.push(guildData.cachePastPostedLinks(client.guilds.get(guildData.guildID))))); guildDatas.forEach(guildData => {
if (client.guilds.get(guildData.guildID))
promises.push(guildData.cachePastPostedLinks(client.guilds.get(guildData.guildID)));
}));
return Promise.all(promises); return Promise.all(promises);
} }

View file

@ -35,20 +35,11 @@ module.exports = class Client extends Discord.Client {
process.on("uncaughtException", err => this._onUnhandledException(this, err)); process.on("uncaughtException", err => this._onUnhandledException(this, err));
} }
bootstrap() {
Camo.connect("nedb://guilds-data").then(db => {
neDB = db;
this.emit("beforeLogin");
this.login(this._token);
});
}
_onReady() { _onReady() {
this.user.setGame(InternalConfig.website.replace(/^https?:\/\//, "")); this.user.setGame(InternalConfig.website.replace(/^https?:\/\//, ""));
CoreUtil.dateLog(`Registered bot ${this.user.username}`); CoreUtil.dateLog(`Registered bot ${this.user.username}`);
new CronJob(InternalConfig.dbCompactionSchedule, () => compactCollections(), null, true); this.removeDeletedGuilds();
} }
_onMessage(message) { _onMessage(message) {
@ -80,6 +71,24 @@ module.exports = class Client extends Discord.Client {
setTimeout(() => client.login(client._token), InternalConfig.reconnectTimeout); setTimeout(() => client.login(client._token), InternalConfig.reconnectTimeout);
}); });
} }
bootstrap() {
Camo.connect("nedb://guilds-data").then(db => {
neDB = db;
new CronJob(InternalConfig.dbCompactionSchedule, compactCollections, null, true);
this.emit("beforeLogin");
this.login(this._token);
});
}
removeDeletedGuilds() {
this.guildDataModel.find().then(guildDatas => {
for (let guildData of guildDatas)
if (!this.guilds.get(guildData.guildID))
guildData.delete();
});
}
}; };
function compactCollections() { function compactCollections() {
@ -89,6 +98,6 @@ function compactCollections() {
and camo is designed to work with both NeDB and MongoDB, which is presumably why it doesn't alraedy exist */ and camo is designed to work with both NeDB and MongoDB, which is presumably why it doesn't alraedy exist */
for (let collectionName of Object.keys(neDB._collections)) for (let collectionName of Object.keys(neDB._collections))
neDB._collections[collectionName].persistence.compactDatafile(); neDB._collections[collectionName].persistence.compactDatafile();
Util.dateLog("Executed compaction on loaded NeDB collections"); Util.dateLog("Executed compaction on loaded NeDB collections");
} }