File size: 2,648 Bytes
17346ad
 
ad0e3bf
17346ad
ad0e3bf
 
 
 
 
 
 
 
a476e1b
9c0bb1a
 
ad0e3bf
 
 
 
 
 
1e19c34
9c0bb1a
 
ad0e3bf
 
 
 
 
 
 
 
 
 
 
 
 
17346ad
ad0e3bf
 
 
 
 
 
a476e1b
 
ad0e3bf
 
 
 
 
 
1e19c34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c0bb1a
 
 
 
 
 
 
 
 
 
 
17346ad
 
ad0e3bf
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require('fs');
const path = require('path');

class Storage {
    constructor() {
        this.dir = path.join(__dirname, 'data');
        if (!fs.existsSync(this.dir)) fs.mkdirSync(this.dir);

        this.files = {
            settings: path.join(this.dir, 'settings.json'),
            roles: path.join(this.dir, 'roles.json'),
            stats: path.join(this.dir, 'stats.json'),
            active_polls: path.join(this.dir, 'active_polls.json'),
            records: path.join(this.dir, 'records.json'),
            config: path.join(this.dir, 'config.json')
        };

        this.data = {
            settings: {},
            roles: {},
            stats: {},
            active_polls: {},
            records: {},
            config: {}
        };

        this.loadAll();
    }

    loadAll() {
        for (const [key, file] of Object.entries(this.files)) {
            if (fs.existsSync(file)) {
                try {
                    this.data[key] = JSON.parse(fs.readFileSync(file, 'utf8'));
                } catch (e) {
                    console.error(`Error loading ${key}: ${e}`);
                    this.data[key] = {};
                }
            }
        }
    }

    save(key) {
        if (this.files[key]) {
            const output = this.data[key] || {};
            fs.writeFileSync(this.files[key], JSON.stringify(output, null, 2));
        }
    }

    saveAll() {
        for (const key of Object.keys(this.data)) this.save(key);
    }

    // --- Helper Methods ---
    getRecord(guildId) {
        if (!this.data.records[guildId]) {
            this.data.records[guildId] = { wins: 0, losses: 0, draws: 0 };
        }
        return this.data.records[guildId];
    }

    addStat(userId, type) {
        if (!this.data.stats[userId]) {
            this.data.stats[userId] = { matches: 0, goals: 0, mvps: 0 };
        }
        if (this.data.stats[userId][type] !== undefined) {
            this.data.stats[userId][type]++;
            this.save('stats');
        }
    }

    isWhitelisted(guildId, member) {
        if (member.permissions.has('Administrator')) return true;
        const allowedRoles = this.data.roles[guildId] || [];
        return member.roles.cache.some(r => allowedRoles.includes(r.id));
    }

    getPrefix(guildId) {
        if (!guildId) return "!";
        return this.data.config[guildId]?.prefix || "!";
    }

    setPrefix(guildId, prefix) {
        if (!this.data.config[guildId]) this.data.config[guildId] = {};
        this.data.config[guildId].prefix = prefix;
        this.save('config');
    }
}

const storage = new Storage();
module.exports = storage;