Skip to content

Commit

Permalink
Better persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPresso committed Apr 23, 2024
1 parent db3bce0 commit b754486
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea
/node_modules
/data
config.json
23 changes: 20 additions & 3 deletions classes/Judy.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ module.exports = class Judy {

try {
this._client.config = require('../config.json');
this._client.persistentData = require('../data.json') || {};
} catch (e) {
console.error("Cannot retrieve config file, please create a config.json file");
process.exit(1);
}
}

async bootstrap() {
this._client.savePersistentData = this.savePersistentData;
this._client.logger = new Logger();

this.loadPersistentData();

this._client.savePersistentData = this.savePersistentData;
this._client._commands = new Map();
this._client._state = {
tempChannels: new Map(),
Expand All @@ -56,6 +58,21 @@ module.exports = class Judy {
this.scheduleTasks();
}

loadPersistentData() {
if(!fs.existsSync("./data")) {
this._client.logger.debug("Creating data directory.");
fs.mkdirSync("./data");
}

if(!fs.existsSync("./data/data.json")) {
this._client.logger.debug("Creating data.json with default content.")
fs.writeFileSync("./data/data.json", fs.readFileSync("./defaultData.json"));
}

this._client.persistentData = require('../data/data.json');
this._client.logger.info("Loaded persistent data.");
}

registerEvents() {
fs.readdirSync('./events').forEach(file => {
const event = require(`../events/${file}`),
Expand Down Expand Up @@ -88,7 +105,7 @@ module.exports = class Judy {
}

async savePersistentData() {
fs.writeFile('./data.json', JSON.stringify(this.persistentData, null, 4), (err) => {
fs.writeFile('./data/data.json', JSON.stringify(this.persistentData, null, 4), (err) => {
if(err)
console.error(err);
});
Expand Down
File renamed without changes.

0 comments on commit b754486

Please sign in to comment.