-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
609 lines (500 loc) · 16 KB
/
db.js
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/* eslint-disable camelcase */
const fetch = require('node-fetch')
const mysql = require('mysql2')
const pool = mysql.createPool({
host: process.env.database.host,
port: process.env.database.port,
user: process.env.database.user,
password: process.env.database.password,
database: process.env.database.database,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
})
// If you were smart, you would complete redo the coin system by using something that could higher the coin limit, while being able to handle big numbers. (since Javascript sucks at handling big numbers)
pool.query(
`
CREATE TABLE IF NOT EXISTS \`accounts\` (
\`discord_id\` varchar(255) PRIMARY KEY,
\`pterodactyl_id\` varchar(255) DEFAULT NULL,
\`blacklisted\` varchar(255) DEFAULT NULL,
\`coins\` int(11) DEFAULT NULL,
\`package\` varchar(255) DEFAULT NULL,
\`memory\` int(11) DEFAULT NULL,
\`disk\` int(11) DEFAULT NULL,
\`cpu\` int(11) DEFAULT NULL,
\`servers\` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`
)
pool.query(
`
CREATE TABLE IF NOT EXISTS \`renewal_timer\` (
\`server_id\` varchar(255) PRIMARY KEY,
\`date\` varchar(255) DEFAULT NULL,
\`action\` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`
)
pool.query(
`
CREATE TABLE IF NOT EXISTS \`coupons\` (
\`code\` varchar(255) PRIMARY KEY,
\`coins\` int(11) DEFAULT NULL,
\`memory\` int(11) DEFAULT NULL,
\`disk\` int(11) DEFAULT NULL,
\`cpu\` int(11) DEFAULT NULL,
\`servers\` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`
)
pool.query( // This is for the list/information of J4Rs.
`
CREATE TABLE IF NOT EXISTS \`all_j4rs\` (
\`j4r_id\` varchar(255) PRIMARY KEY,
\`server_id\` varchar(255) DEFAULT NULL,
\`expires_on\` varchar(255) DEFAULT NULL,
\`memory\` int(11) DEFAULT NULL,
\`disk\` int(11) DEFAULT NULL,
\`cpu\` int(11) DEFAULT NULL,
\`servers\` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`
)
pool.query( // This is for the resources users get from J4Rs.
`
CREATE TABLE IF NOT EXISTS \`user_j4rs\` (
\`j4r_id\` varchar(255) DEFAULT NULL,
\`discord_id\` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`
)
module.exports = {
async createAccountOnDB (discord_id, pterodactyl_id) {
return new Promise((resolve, reject) => {
pool.query(
'INSERT INTO accounts (discord_id, pterodactyl_id, blacklisted, coins, package, memory, disk, cpu, servers) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
discord_id,
pterodactyl_id,
'false',
0,
null,
0,
0,
0,
0
],
function (error, results, fields) {
if (error) return reject(error)
resolve(true)
}
)
})
},
async createOrFindAccount (username, email, first_name, last_name) {
const generated_password = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
const account = await fetch(
`${process.env.pterodactyl.domain}/api/application/users`,
{
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.pterodactyl.key}`
},
body: JSON.stringify({
username: username,
email: email,
first_name: first_name,
last_name: last_name,
password: process.env.pterodactyl.generate_password_on_sign_up ? generated_password : undefined
})
}
)
if (await account.status === 201) { // Successfully created account.
const accountinfo = await account.json()
await this.createAccountOnDB(username, accountinfo.attributes.id)
accountinfo.attributes.password = generated_password
accountinfo.attributes.relationships = { servers: { object: 'list', data: [] } }
return accountinfo.attributes
} else { // Find account.
const accountlistjson = await fetch(
`${process.env.pterodactyl.domain}/api/application/users?include=servers&filter[email]=${encodeURIComponent(email)}`,
{
method: 'get',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.pterodactyl.key}`
}
}
)
const accountlist = await accountlistjson.json()
const user = accountlist.data.filter(acc => acc.attributes.email === email)
if (user.length === 1) {
const userid = user[0].attributes.id
await this.createAccountOnDB(username, userid)
return user[0].attributes
};
return false
};
},
async fetchAccountPterodactylID (pterodactyl_id) {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM accounts WHERE pterodactyl_id = ?', [pterodactyl_id], function (error, results, fields) {
if (error) return reject(error)
if (results.length !== 1) return resolve(null)
const userInfo = results[0]
resolve(userInfo)
})
})
},
async fetchAccountDiscordID (discord_id) {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM accounts WHERE discord_id = ?', [discord_id], function (error, results, fields) {
if (error) return reject(error)
if (results.length !== 1) return resolve(null)
const userInfo = results[0]
userInfo.blacklisted = userInfo.blacklisted === 'true'
resolve(userInfo)
})
})
},
async getCoinsByDiscordID (discord_id) {
const dbinfo = await this.fetchAccountDiscordID(discord_id)
if (!dbinfo) return null
return dbinfo.coins || 0
},
async addCoinsByDiscordID (discord_id, amount) {
const dbinfo = await this.fetchAccountDiscordID(discord_id)
if (!dbinfo) return null
let coins = dbinfo.coins || 0
coins += amount
coins = Math.round(coins)
if (coins < 0) coins = 0
if (coins > 2147483647) coins = 2147483647
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET coins = ? WHERE accounts.discord_id = ?',
[
coins,
discord_id
],
function (error, results, fields) {
if (error) return reject(error)
resolve(coins)
}
)
})
},
async setCoinsByDiscordID (discord_id, amount) {
const dbinfo = await this.fetchAccountDiscordID(discord_id)
if (!dbinfo) return null
let coins = Math.round(amount)
if (coins < 0) coins = 0
if (coins > 2147483647) coins = 2147483647
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET coins = ? WHERE discord_id = ?',
[
coins,
discord_id
],
function (error, results, fields) {
if (error) return reject(error)
resolve(Math.round(coins))
}
)
})
},
async setPackageByDiscordID (discord_id, pkg) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET package = ? WHERE discord_id = ?',
[
pkg,
discord_id
],
function (error, results, fields) {
if (error) return reject(error)
resolve(pkg)
}
)
})
},
async setResourcesByDiscordID (discord_id, memory, disk, cpu, servers) {
const additions = []
const the_array = [] // Contains variables for "?" in MySQL.
// Beautiful code that hurts my eyes, and I'm lazy af. - Two
if (typeof memory === 'number') {
additions.push('memory = ?')
the_array.push(memory)
if (memory > 1073741823) memory = 1073741823
}
if (typeof disk === 'number') {
additions.push('disk = ?')
the_array.push(disk)
if (disk > 1073741823) disk = 1073741823
}
if (typeof cpu === 'number') {
additions.push('cpu = ?')
the_array.push(cpu)
if (cpu > 1073741823) cpu = 1073741823
}
if (typeof servers === 'number') {
additions.push('servers = ?')
the_array.push(servers)
if (servers > 1073741823) servers = 1073741823
}
the_array.push(discord_id)
return new Promise((resolve, reject) => {
pool.query(
`UPDATE accounts SET ${additions.join(', ')} WHERE discord_id = ?`,
the_array,
function (error, results, fields) {
if (error) return reject(error)
resolve(true)
}
)
})
},
async getAllRenewalTimers () {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM renewal_timer', function (error, results, fields) {
if (error) return reject(error)
resolve(results)
})
})
},
async getSingleRenewalDate (server_id) {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM renewal_timer WHERE server_id = ?', [server_id], function (error, results, fields) {
if (error) return reject(error)
if (results.length !== 1) {
return resolve({
action: '???',
timer: '???'
})
}
resolve({
action: results[0].action,
timer: parseFloat(results[0].date)
})
})
})
},
async runDBTimerActions (server_id, date, action = 'suspend') {
await this.removeRenewTimerFromDB(server_id)
await this.addRenewTimerToDB(server_id, date, action)
return true
},
async addRenewTimerToDB (server_id, date, action) {
return new Promise((resolve, reject) => {
pool.query(
'INSERT INTO renewal_timer (server_id, date, action) VALUES (?, ?, ?)',
[
server_id,
date,
action
],
function (error, results, fields) {
if (error) return reject(error)
resolve(true)
}
)
})
},
async removeRenewTimerFromDB (server_id) {
return new Promise((resolve, reject) => {
pool.query(
'DELETE FROM renewal_timer WHERE server_id=?',
[
server_id
],
function (error, results, fields) {
if (error) return reject(error)
resolve(true)
}
)
})
},
async createCoupon (code, coins, memory, disk, cpu, servers) {
const check_if_coupon_exists = await this.getCouponInfo(code)
if (!check_if_coupon_exists) {
return new Promise((resolve, reject) => {
pool.query(
'INSERT INTO coupons (code, coins, memory, disk, cpu, servers) VALUES (?, ?, ?, ?, ?, ?)',
[
code,
coins,
memory,
disk,
cpu,
servers
],
function (error, results, fields) {
if (error) return reject(error)
resolve(true)
}
)
})
} else {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE coupons SET coins = ?, memory = ?, disk = ?, cpu = ?, servers = ? WHERE code = ?',
[
coins,
memory,
disk,
cpu,
servers,
code
],
function (error, results, fields) {
if (error) return reject(error)
resolve(true)
}
)
})
}
},
async claimCoupon (code) {
const check_if_coupon_exists = await this.getCouponInfo(code)
if (check_if_coupon_exists) {
return new Promise((resolve, reject) => {
pool.query(
'DELETE FROM coupons WHERE code = ?',
[
code
],
async (error, results, fields) => {
if (error) return reject(error)
resolve(check_if_coupon_exists)
}
)
})
} else {
return null
}
},
async getCouponInfo (code) {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM coupons WHERE code = ?', [code], function (error, results, fields) {
if (error) return reject(error)
if (results.length !== 1) resolve(null)
resolve(results[0])
})
})
},
async checkJ4R (discord_id, user_guilds) {
const userinfo = await process.db.fetchAccountDiscordID(discord_id)
if (!userinfo) console.error('[CHECK J4R] Could not find user with ID.')
const j4r_list = await this.allJ4Rs()
const supposed_to_be_in = await new Promise((resolve, reject) => {
pool.query('SELECT * FROM user_j4rs WHERE discord_id = ?', [discord_id], function (error, results, fields) {
if (error) return reject(error)
resolve(results)
})
})
for (const { j4r_id, server_id, expires_on, memory, disk, cpu, servers } of j4r_list) {
if (user_guilds.filter(s => s.id === server_id).length === 1) { // In J4R server.
if (expires_on > Date.now()) { // If the J4R didn't expire.
if (supposed_to_be_in.filter(s => s.j4r_id === j4r_id).length !== 1) { // If it didn't give resources already, give resources.
userinfo.memory += memory
userinfo.disk += disk
userinfo.cpu += cpu
userinfo.servers += servers
await new Promise((resolve, reject) => {
pool.query(
'INSERT INTO user_j4rs (j4r_id, discord_id) VALUES (?, ?)',
[
j4r_id,
discord_id
],
function (error, results, fields) {
if (error) return reject(error)
resolve(true)
}
)
})
}
}
} else { // Not in J4R server.
if (supposed_to_be_in.filter(s => s.j4r_id === j4r_id).length === 1) { // If user left the server, remove resources.
userinfo.memory -= memory
userinfo.disk -= disk
userinfo.cpu -= cpu
userinfo.servers -= servers
await new Promise((resolve, reject) => {
pool.query(
'DELETE FROM user_j4rs WHERE j4r_id = ? AND discord_id = ?',
[
j4r_id,
discord_id
],
async (error, results, fields) => {
if (error) return reject(error)
resolve(true)
}
)
})
}
}
}
await this.setResourcesByDiscordID(discord_id, userinfo.memory, userinfo.disk, userinfo.cpu, userinfo.servers)
},
async allJ4Rs () {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM all_j4rs', function (error, results, fields) {
if (error) return reject(error)
resolve(results)
})
})
},
async checkIfJ4RWithNameExists (id) {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM all_j4rs WHERE j4r_id = ?', [id], function (error, results, fields) {
if (error) return reject(error)
resolve(results.length !== 0)
})
})
},
async createJ4R (j4r_id, server_id, expires_on, memory, disk, cpu, servers) {
return new Promise((resolve, reject) => {
pool.query(
'INSERT INTO all_j4rs (j4r_id, server_id, expires_on, memory, disk, cpu, servers) VALUES (?, ?, ?, ?, ?, ?, ?)',
[
j4r_id,
server_id,
expires_on,
memory,
disk,
cpu,
servers
],
function (error, results, fields) {
if (error) return reject(error)
resolve(true)
}
)
})
},
async blacklistStatus (discord_id) {
return (await this.fetchAccountDiscordID(discord_id)).blacklisted
},
async toggleBlacklist (discord_id, specific) {
const new_status = specific || !(await this.blacklistStatus(discord_id))
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET blacklisted = ? WHERE discord_id = ?',
[
new_status.toString(), // Is .toString() required? Too lazy to check.
discord_id
],
function (error, results, fields) {
if (error) return reject(error)
resolve(new_status)
}
)
})
}
}