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;