Brozy123 commited on
Commit
ad0e3bf
·
verified ·
1 Parent(s): 65e21cf

Update storage.js

Browse files
Files changed (1) hide show
  1. 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
- 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
- }
 
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;