-
-
Notifications
You must be signed in to change notification settings - Fork 719
Expand file tree
/
Copy pathdatabase.js
More file actions
178 lines (154 loc) · 4.06 KB
/
database.js
File metadata and controls
178 lines (154 loc) · 4.06 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Simple JSON-based Database for Group Settings
*/
const fs = require('fs');
const path = require('path');
const config = require('./config');
const DB_PATH = path.join(__dirname, 'database');
const GROUPS_DB = path.join(DB_PATH, 'groups.json');
const USERS_DB = path.join(DB_PATH, 'users.json');
const WARNINGS_DB = path.join(DB_PATH, 'warnings.json');
const MODS_DB = path.join(DB_PATH, 'mods.json');
// Initialize database directory
if (!fs.existsSync(DB_PATH)) {
fs.mkdirSync(DB_PATH, { recursive: true });
}
// Initialize database files
const initDB = (filePath, defaultData = {}) => {
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, JSON.stringify(defaultData, null, 2));
}
};
initDB(GROUPS_DB, {});
initDB(USERS_DB, {});
initDB(WARNINGS_DB, {});
initDB(MODS_DB, { moderators: [] });
// Read database
const readDB = (filePath) => {
try {
const data = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(data);
} catch (error) {
console.error(`Error reading database: ${error.message}`);
return {};
}
};
// Write database
const writeDB = (filePath, data) => {
try {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
return true;
} catch (error) {
console.error(`Error writing database: ${error.message}`);
return false;
}
};
// Group Settings
const getGroupSettings = (groupId) => {
const groups = readDB(GROUPS_DB);
if (!groups[groupId]) {
groups[groupId] = { ...config.defaultGroupSettings };
writeDB(GROUPS_DB, groups);
}
return groups[groupId];
};
const updateGroupSettings = (groupId, settings) => {
const groups = readDB(GROUPS_DB);
groups[groupId] = { ...groups[groupId], ...settings };
return writeDB(GROUPS_DB, groups);
};
// User Data
const getUser = (userId) => {
const users = readDB(USERS_DB);
if (!users[userId]) {
users[userId] = {
registered: Date.now(),
premium: false,
banned: false
};
writeDB(USERS_DB, users);
}
return users[userId];
};
const updateUser = (userId, data) => {
const users = readDB(USERS_DB);
users[userId] = { ...users[userId], ...data };
return writeDB(USERS_DB, users);
};
// Warnings System
const getWarnings = (groupId, userId) => {
const warnings = readDB(WARNINGS_DB);
const key = `${groupId}_${userId}`;
return warnings[key] || { count: 0, warnings: [] };
};
const addWarning = (groupId, userId, reason) => {
const warnings = readDB(WARNINGS_DB);
const key = `${groupId}_${userId}`;
if (!warnings[key]) {
warnings[key] = { count: 0, warnings: [] };
}
warnings[key].count++;
warnings[key].warnings.push({
reason,
date: Date.now()
});
writeDB(WARNINGS_DB, warnings);
return warnings[key];
};
const removeWarning = (groupId, userId) => {
const warnings = readDB(WARNINGS_DB);
const key = `${groupId}_${userId}`;
if (warnings[key] && warnings[key].count > 0) {
warnings[key].count--;
warnings[key].warnings.pop();
writeDB(WARNINGS_DB, warnings);
return true;
}
return false;
};
const clearWarnings = (groupId, userId) => {
const warnings = readDB(WARNINGS_DB);
const key = `${groupId}_${userId}`;
delete warnings[key];
return writeDB(WARNINGS_DB, warnings);
};
// Moderators System
const getModerators = () => {
const mods = readDB(MODS_DB);
return mods.moderators || [];
};
const addModerator = (userId) => {
const mods = readDB(MODS_DB);
if (!mods.moderators) mods.moderators = [];
if (!mods.moderators.includes(userId)) {
mods.moderators.push(userId);
return writeDB(MODS_DB, mods);
}
return false;
};
const removeModerator = (userId) => {
const mods = readDB(MODS_DB);
if (mods.moderators) {
mods.moderators = mods.moderators.filter(id => id !== userId);
return writeDB(MODS_DB, mods);
}
return false;
};
const isModerator = (userId) => {
const mods = getModerators();
return mods.includes(userId);
};
module.exports = {
getGroupSettings,
updateGroupSettings,
getUser,
updateUser,
getWarnings,
addWarning,
removeWarning,
clearWarnings,
getModerators,
addModerator,
removeModerator,
isModerator
};