Brozy123 commited on
Commit
99fd4c3
·
verified ·
1 Parent(s): a716694

Create roleStorage.js

Browse files
Files changed (1) hide show
  1. roleStorage.js +50 -0
roleStorage.js ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ class RoleManager {
5
+ constructor() {
6
+ this.file = path.join(__dirname, 'data', 'roles.json');
7
+ this.roles = {};
8
+ this.load();
9
+ }
10
+
11
+ load() {
12
+ if (fs.existsSync(this.file)) {
13
+ try {
14
+ this.roles = JSON.parse(fs.readFileSync(this.file, 'utf8'));
15
+ } catch (e) {
16
+ this.roles = {};
17
+ }
18
+ }
19
+ }
20
+
21
+ save() {
22
+ // Ensure data directory exists
23
+ const dir = path.dirname(this.file);
24
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
25
+
26
+ fs.writeFileSync(this.file, JSON.stringify(this.roles, null, 2));
27
+ }
28
+
29
+ addRole(guildId, roleId) {
30
+ if (!this.roles[guildId]) this.roles[guildId] = [];
31
+ if (!this.roles[guildId].includes(roleId)) {
32
+ this.roles[guildId].push(roleId);
33
+ this.save();
34
+ }
35
+ }
36
+
37
+ removeRole(guildId, roleId) {
38
+ if (!this.roles[guildId]) return;
39
+ this.roles[guildId] = this.roles[guildId].filter(id => id !== roleId);
40
+ this.save();
41
+ }
42
+
43
+ isWhitelisted(guildId, member) {
44
+ if (member.permissions.has('Administrator')) return true;
45
+ const allowedRoles = this.roles[guildId] || [];
46
+ return member.roles.cache.some(r => allowedRoles.includes(r.id));
47
+ }
48
+ }
49
+
50
+ module.exports = new RoleManager();