File size: 1,570 Bytes
2344c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fs = require('fs');
const path = require('path');

console.log(">>> [MODULE] roleStorage.js: Initializing...");

const storage = require('./storage');

class RoleManager {
    constructor() {
        this.file = path.join(storage.PERSISTENT_DIR, 'roles.json');
        this.roles = {};
        this.load();
    }

    load() {
        if (fs.existsSync(this.file)) {
            try {
                this.roles = JSON.parse(fs.readFileSync(this.file, 'utf8'));
            } catch (e) {
                this.roles = {};
            }
        }
    }

    save() {
        // Ensure data directory exists
        const dir = path.dirname(this.file);
        if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
        
        fs.writeFileSync(this.file, JSON.stringify(this.roles, null, 2));
    }

    addRole(guildId, roleId) {
        if (!this.roles[guildId]) this.roles[guildId] = [];
        if (!this.roles[guildId].includes(roleId)) {
            this.roles[guildId].push(roleId);
            this.save();
        }
    }

    removeRole(guildId, roleId) {
        if (!this.roles[guildId]) return;
        this.roles[guildId] = this.roles[guildId].filter(id => id !== roleId);
        this.save();
    }

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

module.exports = new RoleManager();