-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
297 lines (246 loc) · 9.65 KB
/
db.js
File metadata and controls
297 lines (246 loc) · 9.65 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const fs = require('fs');
const path = require('path');
const DB_PATH = path.join(__dirname, 'database.json');
// Initialize database if it doesn't exist
if (!fs.existsSync(DB_PATH)) {
fs.writeFileSync(DB_PATH, JSON.stringify({ users: {}, verifications: {} }, null, 2));
}
function readDB() {
try {
return JSON.parse(fs.readFileSync(DB_PATH, 'utf8'));
} catch (error) {
console.error('Error reading database:', error);
return { users: {}, verifications: {} };
}
}
function writeDB(data) {
fs.writeFileSync(DB_PATH, JSON.stringify(data, null, 2));
}
function cacheUsername(userId, username) {
const db = readDB();
if (!db.usernameCache) db.usernameCache = {};
db.usernameCache[userId] = username;
writeDB(db);
}
function getCachedUsername(userId) {
const db = readDB();
return db.usernameCache?.[userId] || null;
}
function generateVerificationCode() {
const words = [
'cat', 'dog', 'apple', 'banana', 'ocean', 'mountain', 'computer', 'keyboard',
'sunshine', 'moonlight', 'coffee', 'pizza', 'guitar', 'piano', 'garden',
'whisper', 'thunder', 'lightning', 'rainbow', 'butterfly', 'dragon', 'wizard',
'unicorn', 'castle', 'forest', 'river', 'desert', 'star', 'planet', 'comet',
'rocket', 'alien', 'robot', 'diamond', 'emerald', 'fire', 'ice', 'storm',
'cloud', 'sand', 'leaf', 'flower', 'shadow', 'mirror', 'music', 'dance',
'magic', 'adventure', 'treasure', 'island', 'volcano', 'cave', 'night', 'day',
// Nature & Animals
'elephant', 'penguin', 'dolphin', 'eagle', 'lion', 'tiger', 'bear', 'wolf',
'fox', 'rabbit', 'squirrel', 'owl', 'peacock', 'flamingo', 'whale', 'shark',
'turtle', 'frog', 'butterfly', 'bee', 'spider', 'coral', 'seaweed', 'pebble',
'crystal', 'amber', 'pearl', 'shell', 'feather', 'moss', 'vine', 'bamboo',
// Technology & Objects
'smartphone', 'tablet', 'headphones', 'camera', 'telescope', 'microscope',
'calculator', 'compass', 'watch', 'bicycle', 'skateboard', 'helicopter',
'submarine', 'spaceship', 'telescope', 'antenna', 'satellite', 'engine',
// Food & Drinks
'chocolate', 'vanilla', 'strawberry', 'mango', 'pineapple', 'coconut',
'sandwich', 'pasta', 'soup', 'bread', 'cheese', 'honey', 'cinnamon',
'lemonade', 'smoothie', 'pancake', 'waffle', 'cookie', 'cupcake',
// Colors & Art
'crimson', 'turquoise', 'lavender', 'golden', 'silver', 'bronze', 'scarlet',
'violet', 'indigo', 'magenta', 'canvas', 'palette', 'sculpture', 'painting',
'sketch', 'portrait', 'landscape', 'mosaic', 'origami',
// Weather & Elements
'breeze', 'blizzard', 'drizzle', 'tornado', 'hurricane', 'mist', 'frost',
'snowflake', 'icicle', 'hailstone', 'sunrise', 'sunset', 'twilight', 'dawn',
// Fantasy & Mythology
'phoenix', 'griffin', 'centaur', 'mermaid', 'fairy', 'goblin', 'elf',
'dwarf', 'knight', 'princess', 'kingdom', 'crown', 'sword', 'shield',
'potion', 'spell', 'wand', 'crystal', 'portal', 'quest',
// Emotions & Abstract
'courage', 'wisdom', 'kindness', 'laughter', 'serenity', 'mystery',
'wonder', 'harmony', 'freedom', 'hope', 'dream', 'memory', 'whisper',
'echo', 'silence', 'rhythm', 'melody', 'symphony', 'journey'
];
const selected = [];
for (let i = 0; i < 4; i++) {
selected.push(words[Math.floor(Math.random() * words.length)]);
}
return `eoogle-${selected.join('-')}`;
}
function createVerification(discordId, accountId, type = 'ecsr') {
const db = readDB();
const code = generateVerificationCode();
if (!db.verifications) db.verifications = {};
// Don't link the account yet, just store the verification data
db.verifications[discordId] = {
accountId,
type,
code,
expiresAt: Date.now() + (24 * 60 * 60 * 1000), // 24 hours from now
verified: false // Mark as unverified by default
};
writeDB(db);
return code;
}
function checkVerification(discordId, aboutText) {
const db = readDB();
const verification = db.verifications?.[discordId];
if (!verification || !aboutText) return { success: false };
if (aboutText.includes(verification.code) && verification.expiresAt > Date.now()) {
// Link the account
if (!db.users) db.users = {};
if (!db.users[discordId]) db.users[discordId] = {};
// Store the account ID in the appropriate field based on type
db.users[discordId][verification.type] = verification.accountId;
db.users[discordId].type = verification.type;
delete db.verifications[discordId];
writeDB(db);
return {
success: true,
accountId: verification.accountId,
type: verification.type
};
}
// If verification is expired, clean it up
if (verification.expiresAt <= Date.now()) {
delete db.verifications[discordId];
writeDB(db);
}
return { success: false };
}
function getLinkedAccount(discordId, type = 'ecsr') {
const db = readDB();
if (!db.users || !db.users[discordId]) return null;
// For backward compatibility with old format where users were stored directly by ID
if (typeof db.users[discordId] === 'string') {
// Migrate old format to new format
const oldId = db.users[discordId];
db.users[discordId] = {
ecsr: oldId,
type: 'ecsr'
};
writeDB(db);
}
const userData = db.users[discordId];
// If no type specified, return based on the user's current type
if (!type) {
return userData[userData.type] || null;
}
// Return the specific type if it exists
return userData[type] || null;
}
function clearUsernameCache() {
const db = readDB();
db.usernameCache = {};
writeDB(db);
return true;
}
function getCachedUsers() {
const db = readDB();
return db.usernameCache || {};
}
function getLinkedUsers() {
const db = readDB();
const linkedUsers = [];
if (db.users) {
for (const [discordId, accounts] of Object.entries(db.users)) {
if (accounts.ecsr) {
linkedUsers.push({
discordId,
type: 'ecsr',
userId: accounts.ecsr
});
}
if (accounts.korone) {
linkedUsers.push({
discordId,
type: 'korone',
userId: accounts.korone
});
}
}
}
return linkedUsers;
}
function unlinkAccount(discordId, type = null) {
const db = readDB();
<<<<<<< HEAD
// Initialize users object if it doesn't exist
if (!db.users) {
db.users = {};
writeDB(db);
return { success: false, message: 'User has no linked accounts.' };
}
// Check if user exists and has any linked accounts
if (!db.users[discordId] || (!db.users[discordId].ecsr && !db.users[discordId].korone)) {
=======
if (!db.users || !db.users[discordId]) {
>>>>>>> 14347e912349c1c6c154943925175025f8aa437f
return { success: false, message: 'User has no linked accounts.' };
}
const userData = db.users[discordId];
<<<<<<< HEAD
// If type is specified, validate it
if (type && type !== 'ecsr' && type !== 'korone') {
return { success: false, message: 'Invalid account type. Must be "ecsr" or "korone".' };
}
// If type is specified, unlink only that type
if (type) {
if (!userData[type]) {
const hasOtherAccount = userData.ecsr || userData.korone;
const otherType = userData.ecsr ? 'ecsr' : 'korone';
const message = hasOtherAccount
? `User has no linked ${type} account, but has a linked ${otherType} account.`
: 'User has no linked accounts.';
return { success: false, message };
=======
// If type is specified, unlink only that type
if (type) {
if (!userData[type]) {
return { success: false, message: `User has no linked ${type} account.` };
>>>>>>> 14347e912349c1c6c154943925175025f8aa437f
}
const unlinkedId = userData[type];
delete userData[type];
// If this was the current type, update it
if (userData.type === type) {
// Set to the other type if it exists
if (userData.ecsr) userData.type = 'ecsr';
else if (userData.korone) userData.type = 'korone';
else delete userData.type;
}
// If no accounts left, remove the user entry
if (!userData.ecsr && !userData.korone) {
delete db.users[discordId];
}
writeDB(db);
return { success: true, message: `Unlinked ${type} account: ${unlinkedId}`, unlinkedId };
}
// If no type specified, unlink all accounts
const unlinkedAccounts = [];
if (userData.ecsr) unlinkedAccounts.push({ type: 'ecsr', id: userData.ecsr });
if (userData.korone) unlinkedAccounts.push({ type: 'korone', id: userData.korone });
delete db.users[discordId];
writeDB(db);
return {
success: true,
message: `Unlinked all accounts for user.`,
unlinkedAccounts
};
}
module.exports = {
createVerification,
checkVerification,
getLinkedAccount,
readDB,
writeDB,
cacheUsername,
getCachedUsername,
clearUsernameCache,
getCachedUsers,
getLinkedUsers,
unlinkAccount
};