Spaces:
Runtime error
Runtime error
Update storage.js
Browse files- storage.js +45 -17
storage.js
CHANGED
|
@@ -1,23 +1,51 @@
|
|
| 1 |
const fs = require('fs');
|
| 2 |
const path = require('path');
|
|
|
|
| 3 |
class Storage {
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
}
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
-
module.exports = new Storage();
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
}
|
| 23 |
-
}
|
|
|
|
| 1 |
const fs = require('fs');
|
| 2 |
const path = require('path');
|
| 3 |
+
|
| 4 |
class Storage {
|
| 5 |
+
constructor() {
|
| 6 |
+
this.dir = path.join(__dirname, 'data');
|
| 7 |
+
if (!fs.existsSync(this.dir)) fs.mkdirSync(this.dir);
|
| 8 |
+
|
| 9 |
+
this.files = {
|
| 10 |
+
settings: path.join(this.dir, 'settings.json'),
|
| 11 |
+
roles: path.join(this.dir, 'roles.json'),
|
| 12 |
+
stats: path.join(this.dir, 'stats.json'),
|
| 13 |
+
polls: path.join(this.dir, 'active_polls.json')
|
| 14 |
+
};
|
| 15 |
+
|
| 16 |
+
this.data = {
|
| 17 |
+
settings: {},
|
| 18 |
+
roles: {},
|
| 19 |
+
stats: {},
|
| 20 |
+
active_polls: {}
|
| 21 |
+
};
|
| 22 |
+
|
| 23 |
+
this.loadAll();
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
loadAll() {
|
| 27 |
+
for (const [key, file] of Object.entries(this.files)) {
|
| 28 |
+
if (fs.existsSync(file)) {
|
| 29 |
+
try {
|
| 30 |
+
this.data[key] = JSON.parse(fs.readFileSync(file, 'utf8'));
|
| 31 |
+
} catch (e) {
|
| 32 |
+
console.error(`Error loading ${key}: ${e}`);
|
| 33 |
+
this.data[key] = {};
|
| 34 |
}
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
save(key) {
|
| 40 |
+
if (this.files[key]) {
|
| 41 |
+
fs.writeFileSync(this.files[key], JSON.stringify(this.data[key], null, 2));
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
saveAll() {
|
| 46 |
+
for (const key of Object.keys(this.data)) this.save(key);
|
| 47 |
+
}
|
| 48 |
}
|
|
|
|
| 49 |
|
| 50 |
+
const storage = new Storage();
|
| 51 |
+
module.exports = storage;
|
|
|
|
|
|