-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
103 lines (88 loc) · 3.29 KB
/
main.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
const express = require('express');
const app = express();
const { USER_FLAGS } = require("./constant");
const fetch = require('node-fetch');
require('dotenv').config();
app.set('view engine', 'ejs');
app.disable('x-powered-by');
app.use(express.static('cdn'));
app.use((req, res, next) => {
res.append("Access-Control-Allow-Origin", ["*"]);
res.append("Access-Control-Allow-Methods", "GET,POST");
res.append("Access-Control-Allow-Headers", "Content-Type");
next();
});
app.get('/', (req, res) => {
res.status(200).render('index.ejs', { title: 'Home', found: false });
});
app.post('/fetch/user/:id', async (req, res) => {
let id = req.params.id;
try {
const fRes = await fetch(`https://canary.discord.com/api/v10/users/${id}`, {
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${process.env.TOKEN}`,
},
})
const json = await fRes.json()
if (json.id == undefined) {
let output = {
id: id,
created_at: snowflakeToUtc(id),
}
return res.status(200).render('index.ejs', { user: output, found: false });
}
let publicFlags = [];
USER_FLAGS.forEach((flag) => {
if (json.public_flags & flag.bitwise) {
publicFlags.push(flag.id)
};
});
let avatarLink = null;
if (json.avatar) {
avatarLink = `https://cdn.discordapp.com/avatars/${json.id}/${json.avatar}`;
}
let bannerLink = null;
if (json.banner) {
bannerLink = `https://cdn.discordapp.com/banners/${json.id}/${json.banner}`;
}
let output = {
id: json.id,
created_at: snowflakeToUtc(json.id),
tag: `${json.username}#${json.discriminator}`,
badges: publicFlags,
avatar: {
id: json.avatar,
link: avatarLink,
is_animated: json.avatar != null && json.avatar.startsWith("a_") ? true : false,
},
banner: {
id: json.banner,
link: bannerLink,
is_animated: json.banner != null && json.banner.startsWith("a_") ? true : false,
color: json.banner_color,
},
bot: json.bot,
}
res.status(200).render('index.ejs', { title: `${json.username}#${json.discriminator}`, user: output, found: true });
} catch (err) {
console.log(err);
res.status(404).render('index.ejs', { found : false, title: '404' });
}
});
function snowflakeToUtc(snowflakeId) {
const SNOWFLAKE_EPOCH = 1420070400000;
const timestamp = (snowflakeId / 4194304) + SNOWFLAKE_EPOCH;
const date = new Date(timestamp);
return date.toUTCString();
}
app.get('/bot/:id', (req, res) => {
let id = req.params.id;
res.redirect(`https://discord.com/oauth2/authorize?client_id=${id}&scope=bot&permissions=0`);
});
app.use((req, res) => {
res.status(404).render('index.ejs', { found : false, title: '404' });
});
app.listen(process.env.PORT, () => {
console.log(`Example app listening at http://localhost:${process.env.PORT}`)
});