Spaces:
Runtime error
Runtime error
Create storage.js
Browse files- storage.js +23 -0
storage.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const fs = require('fs');
|
| 2 |
+
const path = require('path');
|
| 3 |
+
class Storage {
|
| 4 |
+
constructor() {
|
| 5 |
+
this.dir = path.join(__dirname, 'data');
|
| 6 |
+
if (!fs.existsSync(this.dir)) fs.mkdirSync(this.dir);
|
| 7 |
+
this.files = { settings: path.join(this.dir, 'settings.json'), roles: path.join(this.dir, 'roles.json'), stats: path.join(this.dir, 'stats.json'), polls: path.join(this.dir, 'active_polls.json') };
|
| 8 |
+
this.data = { settings: {}, roles: {}, stats: {}, active_polls: {} };
|
| 9 |
+
this.loadAll();
|
| 10 |
+
}
|
| 11 |
+
loadAll() {
|
| 12 |
+
for (const [key, file] of Object.entries(this.files)) {
|
| 13 |
+
if (fs.existsSync(file)) { try { this.data[key] = JSON.parse(fs.readFileSync(file, 'utf8')); } catch (e) { this.data[key] = {}; } }
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
save(key) { if (this.files[key]) fs.writeFileSync(this.files[key], JSON.stringify(this.data[key], null, 2)); }
|
| 17 |
+
}
|
| 18 |
+
module.exports = new Storage();
|
| 19 |
+
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
}
|