forked from mruniquehacker/KnightBot-Mini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
244 lines (215 loc) · 6.17 KB
/
Copy pathdatabase.js
File metadata and controls
244 lines (215 loc) · 6.17 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* 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');
const SUDO_DB = path.join(DB_PATH, 'sudo.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: [] });
initDB(SUDO_DB, { sudoUsers: [] });
// 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);
};
// Sudo Users System - stores full JIDs (e.g. 602455062940@s.whatsapp.net or xyz@lid)
const toStoredJid = (input) => {
const s = String(input).trim();
if (!s) return null;
if (s.includes('@')) return s; // Already a JID
const digits = s.replace(/\D/g, '');
return digits.length >= 10 ? `${digits}@s.whatsapp.net` : null;
};
const getSudoUsers = () => {
const sudo = readDB(SUDO_DB);
const raw = sudo.sudoUsers || [];
return raw.map((entry) => (entry.includes('@') ? entry : `${entry}@s.whatsapp.net`));
};
const addSudoUser = (userIdOrJid) => {
const sudo = readDB(SUDO_DB);
if (!sudo.sudoUsers) sudo.sudoUsers = [];
const jid = toStoredJid(userIdOrJid);
if (!jid) return false;
if (!sudo.sudoUsers.includes(jid)) {
sudo.sudoUsers.push(jid);
return writeDB(SUDO_DB, sudo);
}
return false;
};
const removeSudoUser = (userIdOrJid) => {
const sudo = readDB(SUDO_DB);
if (!sudo.sudoUsers) return false;
const jid = toStoredJid(userIdOrJid);
if (!jid) return false;
const before = sudo.sudoUsers.length;
const jidUser = jid.split('@')[0];
sudo.sudoUsers = sudo.sudoUsers.filter((entry) => {
const entryJid = entry.includes('@') ? entry : `${entry}@s.whatsapp.net`;
const entryUser = entryJid.split('@')[0];
return entryJid !== jid && entryUser !== jidUser;
});
return writeDB(SUDO_DB, sudo) && sudo.sudoUsers.length < before;
};
const isSudoUser = (sender) => {
const sudo = getSudoUsers();
if (!sender) return false;
const senderStr = String(sender).trim();
const senderUser = senderStr.includes('@')
? senderStr.split('@')[0].split(':')[0]
: (senderStr.replace(/\D/g, '') || senderStr);
if (!senderUser) return false;
return sudo.some((jid) => {
const storedUser = jid.split('@')[0]?.split(':')[0] || '';
if (!storedUser) return false;
return (
storedUser === senderUser ||
(senderUser.length >= 10 && (storedUser.startsWith(senderUser) || senderUser.startsWith(storedUser)))
);
});
};
module.exports = {
getGroupSettings,
updateGroupSettings,
getUser,
updateUser,
getWarnings,
addWarning,
removeWarning,
clearWarnings,
getModerators,
addModerator,
removeModerator,
isModerator,
getSudoUsers,
addSudoUser,
removeSudoUser,
isSudoUser
};