This repository was archived by the owner on Dec 13, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAPI.js
More file actions
506 lines (483 loc) · 24.3 KB
/
API.js
File metadata and controls
506 lines (483 loc) · 24.3 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
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
const cors = require('cors');
const BaseRoute = require('../Structure/BaseRoute');
const Ratelimiter = require('../Middleware/RateLimiter');
const Cache = require('../Structure/Cache');
const handleError = require('../Util/handleError');
const handleServerCount = require('../Util/handleServerCount');
const getBotInformation = require('../Util/getBotInformation');
const getUserAgent = require('../Util/getUserAgent');
const isSnowflake = require('../Util/isSnowflake');
const { slugify, librarySlug } = require('../Util/slugs');
const legacyListMap = require('../Util/legacyListMap');
const getList = require('../Util/getList');
const listProps = require('../Util/listProps');
const Renderer = require('../Structure/Markdown');
const { secret } = require('../../config.js');
const FormValidator = require('../Structure/FormValidator');
class APIRoute extends BaseRoute {
constructor(client, db) {
super('/api');
this.router = require('express').Router();
this.client = client;
this.db = db;
this.ratelimit = new Ratelimiter(this.db);
this.renderer = new Renderer();
this.cache = new Cache(this.db);
this.routes();
}
routes() {
this.router.get('/docs', (req, res) => {
this.db.select('id', 'name').from('lists')
.where({
display: true,
defunct: false
})
.whereNot({ api_post: '' }).whereNot({ api_post: null })
.whereNot({ api_field: '' }).whereNot({ api_field: null })
.orderBy([{ column: 'discord_only', order: 'desc' }, { column: 'id', order: 'asc' }])
.then((lists) => {
res.render('api/docs', { title: 'API Docs', lists, ip: req.ip, listProps });
})
.catch((e) => {
handleError(this.db, req, res, e.stack);
});
});
this.router.get('/docs/libs', (req, res) => {
this.db.select().from('libraries').orderBy([
{ column: 'language', order: 'asc' },
{ column: 'name', order: 'asc' }
]).then((libraries) => {
libraries = libraries.map(lib => {
lib.slug = librarySlug(lib);
lib.highlight = `lang-${slugify(lib.language)}`;
lib.description = this.renderer.render(lib.description);
return lib;
});
res.render('api/libs', { title: 'Libraries - API Docs', libraries });
}).catch((e) => {
handleError(this.db, req, res, e.stack);
});
});
this.router.get('/docs/libs/manage', this.requiresAuth.bind(this), this.isMod.bind(this), (req, res) => {
try {
this.db.select().from('libraries')
.orderBy([
{ column: 'name', order: 'asc' }
])
.then((libraries) => {
res.render('libraries/manage', {
title: 'Manage API Libraries',
libraries
});
});
} catch (e) {
handleError(this.db, req, res, e.stack);
}
});
this.router.get('/docs/libs/manage/:name', this.requiresAuth.bind(this), this.isMod.bind(this), (req, res) => {
try {
this.db.select().from('libraries').where({ name: req.params.name }).limit(1).then((libraries) => {
if (!libraries.length) return res.status(404).render('error', {
title: 'Page not found',
status: 404,
message: 'The page you were looking for could not be found.'
});
res.render('libraries/edit', {
title: `Edit Library '${libraries[0].name}'`,
data: libraries[0]
});
});
} catch (e) {
handleError(this.db, req, res, e.stack);
}
});
this.router.post('/docs/libs/manage/:name', this.requiresAuth.bind(this), this.isMod.bind(this), (req, res) => {
this.db.select().from('libraries').where({ name: req.params.name }).then(async (data) => {
if (!data.length) return res.status(404).render('error', {
title: 'Page not found',
status: 404,
message: 'The page you were looking for could not be found.'
});
let changes = {};
const validate = FormValidator.validateLibrary(req.body);
if (validate && validate.length > 0) return res.render('libraries/edit', { title: 'Edit Library', data: req.body, errors: validate });
const columns = Object.keys(await this.db('libraries').columnInfo());
for (const column of columns) {
if (req.body[column]) {
changes[column] = req.body[column];
} else {
changes[column] = null;
}
}
await this.db('libraries').where({ name: req.params.name }).update(changes);
res.redirect('/api/docs/libs/manage');
}).catch((e) => {
handleError(this.db, req, res, e.stack);
});
});
this.router.get('/docs/libs/manage/:name/delete', this.requiresAuth.bind(this), this.isAdmin.bind(this), (req, res) => {
try {
this.db.select().from('libraries').where({ name: req.params.name }).limit(1).then(async (libraries) => {
if (!libraries.length) return res.status(404).render('error', {
title: 'Page not found',
status: 404,
message: 'The page you were looking for could not be found.'
});
await this.db('libraries').where({ name: req.params.name }).del();
res.redirect('/api/docs/libs/manage');
});
} catch (e) {
handleError(this.db, req, res, e.stack);
}
});
this.router.get('/docs/libs/add', this.requiresAuth.bind(this), this.isMod.bind(this), (req, res) => {
res.render('libraries/edit', {
title: 'Add Library',
data: {}
});
});
this.router.post('/docs/libs/add', this.requiresAuth.bind(this), this.isAdmin.bind(this), async (req, res) => {
try {
let changes = {};
const validate = FormValidator.validateLibrary(req.body);
if (validate && validate.length > 0) return res.render('libraries/edit', { title: 'Add Library', data: req.body, errors: validate });
const columns = Object.keys(await this.db('libraries').columnInfo());
for (const column of columns) {
if (req.body[column]) {
changes[column] = req.body[column];
} else {
changes[column] = null;
}
}
await this.db('libraries').insert(changes);
res.redirect('/api/docs/libs/manage');
} catch (e) {
handleError(this.db, req, res, e.stack);
}
});
this.router.get('/lists', cors(), this.ratelimit.checkRatelimit(1, 1), async (req, res) => {
try {
const listIds = await this.db.select('id').from('lists').orderBy([
{ column: 'discord_only', order: 'desc' },
{ column: 'id', order: 'asc' }
]);
const lists = await Promise.all(listIds.map(list => getList(this.db, list.id)));
if (!lists) return res.status(200).json({});
const data = {};
for (let i = 0; i < lists.length; i++) {
// If filtering: Only present API values, drop if all values are null or defunct
if (req.query.filter === 'true') {
if (lists[i].defunct) continue;
const apiEntries = Object.entries(lists[i]).filter(data => data[0].startsWith('api_'));
if (apiEntries.filter(data => data[1] !== null).length === 0) continue;
data[lists[i].id] = apiEntries.reduce((obj, [key, val]) => {
obj[key] = val;
return obj;
}, {});
} else {
data[lists[i].id] = {
...lists[i]
};
}
}
res.status(200).json({ ...data });
} catch (e) {
handleError(this.db, req, res, e.stack, true);
}
});
this.router.get('/lists/:id', cors(), this.ratelimit.checkRatelimit(1, 1), async (req, res) => {
try {
const data = await getList(this.db, req.params.id);
if (!data) return res.status(404).json({ error: true, status: 404, message: 'List not found' });
res.status(200).json({ ...data });
} catch (e) {
handleError(this.db, req, res, e.stack, true);
}
});
this.router.get('/legacy-ids', cors(), this.ratelimit.checkRatelimit(1, 1), (req, res) => {
this.db
.select('id', 'target')
.from('legacy_ids')
.orderBy([
{ column: 'id', order: 'desc' }
])
.then((legacy) => {
const data = legacy.reduce(function (result, item) {
result[item.id] = item.target;
return result;
}, {});
res.status(200).json({ ...data });
})
.catch((e) => {
handleError(this.db, req, res, e.stack, true);
});
});
this.router.post('/count', cors(), this.ratelimit.checkRatelimit(1, 120), (req, res) => {
if (!req.body.bot_id) return res.status(400).json({
error: true,
status: 400,
message: '\'bot_id\' is required'
});
if (typeof req.body.bot_id !== 'string') return res.status(400).json({
error: true,
status: 400,
message: '\'bot_id\' must be a string'
});
if (!isSnowflake(req.body.bot_id)) return res.status(400).json({
error: true,
status: 400,
message: '\'bot_id\' must be a snowflake'
});
if (!req.body.server_count) return res.status(400).json({
error: true,
status: 400,
message: '\'server_count\' is required'
});
if (isNaN(req.body.server_count)) return res.status(400).json({
error: true,
status: 400,
message: '\'server_count\' must be a number'
});
if (req.body.shard_id) {
if (isNaN(req.body.shard_id)) return res.status(400).json({
error: true,
status: 400,
message: '\'shard_id\' must be a number'
});
}
if (req.body.shards) {
if (!Array.isArray(req.body.shards)) return res.status(400).json({
error: true,
status: 400,
message: '\'shards\' must be an array'
});
if (req.body.shards.some((n) => typeof n !== 'number')) return res.status(400).json({
error: true,
status: 400,
message: '\'shards\' contains incorrect values'
});
}
if (req.body.shard_count) {
if (isNaN(req.body.shard_count)) return res.status(400).json({
error: true,
status: 400,
message: '\'shard_count\' must be a number'
});
}
let success = {};
let failure = {};
this.db
.select('id', 'api_docs', 'api_post', 'api_field', 'api_shard_id', 'api_shard_count', 'api_shards', 'api_get')
.from('lists')
.where({ defunct: false })
.whereNot({ api_post: '' }).whereNot({ api_post: null })
.orderBy([
{ column: 'discord_only', order: 'desc' },
{ column: 'id', order: 'asc' }
])
.then(async (lists) => {
const data = Object.keys(req.body);
for (let i = 0; i < data.length; i++) {
const dataId = await legacyListMap(this.db, data[i]);
const list = lists.filter((l) => l.id === dataId)[0];
if (list) {
let payload = {};
if (req.body.shards && list.api_shards) {
payload[list.api_shards] = req.body.shards;
} else if (req.body.server_count && list.api_field) {
payload[list.api_field] = req.body.server_count;
}
if (req.body.shard_id && list.api_shard_id) payload[list.api_shard_id] = req.body.shard_id;
if (req.body.shard_count && list.api_shard_count) payload[list.api_shard_count] = req.body.shard_count;
let userAgent = getUserAgent().random;
if (req.get('User-Agent')) {
userAgent = req.get('User-Agent');
}
try {
success[list.id] = await handleServerCount(list, req.body.bot_id, payload, req.body[data[i]], userAgent);
} catch (e) {
failure[list.id] = e;
}
}
if (i + 1 === data.length) {
res.status(200).json({ success, failure });
}
}
})
.catch((e) => {
handleError(this.db, req, res, e.stack, true);
});
});
this.router.get('/bots/:id', cors(), this.cache.handler(), this.ratelimit.checkRatelimit(1, 30), async (req, res) => {
if (!isSnowflake(req.params.id)) return res.status(400).json({
error: true,
status: 400,
message: '\'id\' must be a snowflake'
});
let lists = [];
let output = {
id: String(req.params.id),
username: [],
discriminator: [],
owners: [],
server_count: [],
invite: [],
prefix: [],
website: [],
github: [],
support: [],
library: [],
presence_status: [],
list_data: lists
};
this.db.select('id', 'api_get').from('lists')
.where({ defunct: false })
.whereNot({ api_get: '' }).whereNot({ api_get: null })
.then(async (data) => {
for (const list of data) {
try {
lists[list.id] = await getBotInformation(list.api_get.replace(':id', req.params.id), {
'User-Agent': getUserAgent().random,
'X-Forwarded-For': req.ip,
REMOTE_ADDR: req.ip,
X_FORWARDED_FOR: req.ip,
HTTP_X_FORWARDED_FOR: req.ip,
HTTP_X_REAL_IP: req.ip,
HTTP_CLIENT_IP: req.ip
});
} catch (e) {
lists[list.id] = e;
}
}
for (let list of Object.keys(lists)) {
list = lists[list];
if (Number(list[1]) === 200 && list[0] && typeof list[0] === 'object') {
// TODO: discordsbestbots.xyz returns everything inside the 'bot' property of a parent object, we need to handle that
const fields = Object.keys(list[0]);
for (let key in fields) {
key = fields[Number(key)].toLowerCase();
const value = list[0][key];
if (!value) continue;
if (key === 'name' || key === 'username' || key === 'bot_name') {
output.username.push(value);
}
if (key === 'discrim' || key === 'discriminator' || key === 'disc') {
output.discriminator.push(String(value));
}
if (key === 'owner' || key === 'owners' || key === 'authors' || key === 'bot_owners' || key === 'owner_id') {
if (!Array.isArray(value) && typeof value !== 'object') {
output.owners.push(value);
} else if (Array.isArray(value) && typeof value !== 'object') {
for (const owner of value) {
if (!Array.isArray(owner) && typeof owner !== 'object') {
output.owners.push(owner);
} else if (typeof value === 'object') {
if (value['id']) output.owners.push(value['id']);
else if (value['userId']) output.owners.push(value['userId']);
}
}
} else if (typeof value === 'object') {
if (value['id']) output.owners.push(value['id']);
else if (value['userId']) output.owners.push(value['userId']);
}
}
if (key === 'count' || key === 'servers' || key === 'server_count' || key === 'servercount' || key === 'serverCount'
|| key === 'bot_server_count' || key === 'guilds' || key === 'guild_count' || key === 'guildcount' || key === 'guildCount') {
const temp = parseInt(value);
if (typeof temp === 'number') output.server_count.push(temp);
}
if (key === 'links') {
if (typeof value === 'object') {
if (value['invite']) output.invite.push(value['invite']);
if (value['support']) output.support.push(value['support']);
}
}
if (key === 'invite' || key === 'bot_invite' || key === 'botInvite' || key === 'bot_invite_link') {
if (typeof key === 'string') output.invite.push(value);
}
if (key === 'prefix' || key === 'bot_prefix') {
if (typeof key === 'string') output.prefix.push(value);
}
if (key === 'website' || key === 'bot_website') {
if (typeof key === 'string') output.website.push(value);
}
if (key === 'github' || key === 'bot_github_repo' || key === 'openSource' || key === 'git' || key === 'source_code') {
if (typeof key === 'string') output.github.push(value);
}
if (key === 'support' || key === 'supportInvite' || key === 'support_server' || key === 'discord'
|| key === 'server_invite' || key === 'bot_support_discord' || key === 'server') {
if (typeof key === 'string') output.support.push(value);
}
if (key === 'library' || key === 'libraryName' || key === 'bot_library' || key === 'lang') {
if (typeof key === 'string') output.library.push(value);
}
if (key === 'presence_status') {
output.presence_status.push(value);
}
}
}
}
let response = {
id: output.id,
username: this.getMostCommon(output.username) || 'Unknown',
discriminator: this.getMostCommon(output.discriminator) || '0000',
owners: output.owners.filter((v, i, a) => a.indexOf(v) === i && isSnowflake(v)) || [],
server_count: Math.max(...output.server_count) || 0,
invite: this.getMostCommon(output.invite) || '',
prefix: this.getMostCommon(output.prefix) || '',
website: this.getMostCommon(output.website) || '',
github: this.getMostCommon(output.github) || '',
support: this.getMostCommon(output.support) || '',
library: this.getMostCommon(output.library) || '',
presence_status: this.getMostCommon(output.presence_status) || 'unknown',
list_data: { ...lists } || {}
};
await this.cache.add(req.originalUrl, 300, response);
res.status(200).json({ ...response, cached: false });
})
.catch((e) => {
handleError(this.db, req, res, e.stack, true);
});
});
this.router.get('/reset', (req, res) => {
const ratelimitBypass = req.get('X-Ratelimit-Bypass');
if (ratelimitBypass !== secret) {
return res.status(404).json({ error: true, status: 404, message: 'Endpoint not found' });
}
this.db('ratelimit').where({ ip: req.ip }).del().then(() => {
res.status(200).json({ error: false, status: 200, message: 'Ratelimit reset' });
});
});
this.router.use('*', cors(), (req, res) => {
res.status(404).json({ error: true, status: 404, message: 'Endpoint not found' });
});
this.router.use('*', cors(), (err, req, res) => {
res.status(404).json({ error: true, status: 404, message: 'Endpoint not found' });
});
}
/**
* Get most common item in an array.
* @param array
* @return string | number | null
*/
getMostCommon(array) {
// Credit: https://codepen.io/AmJustSam/pen/JNmJBL
if (!array || !Array.isArray(array)) return null;
let counts = {};
let compare = 0;
let mostFrequent = null;
for (let i = 0; i < array.length; i++) {
if (!counts[array[i]]) counts[array[i]] = 1;
else counts[array[i]] = counts[array[i]] + 1;
if (counts[array[i]] > compare) {
compare = counts[array[i]];
mostFrequent = array[i];
}
}
return mostFrequent;
}
get getRouter() {
return this.router;
}
}
module.exports = APIRoute;