-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (116 loc) · 4.21 KB
/
index.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
const express = require("express");
const Auth = require("discord-oauth2");
const jwt = require('express-jwt');
const token = require('jsonwebtoken');
const { join } = require("path");
const { Client } = require("eris")
const Database = require("./Database");
const { secret, clientId, clientSecret, discordToken } = require("./config.json");
const { whitelisted } = require("./whitelisted.json");
const oauth = new Auth({ clientId, clientSecret });
const client = new Client(discordToken, {
disableEvents: {
CHANNEL_CREATE: true,
CHANNEL_DELETE: true,
CHANNEL_UPDATE: true,
GUILD_BAN_ADD: true,
GUILD_BAN_REMOVE: true,
GUILD_DELETE: true,
GUILD_MEMBER_ADD: true,
GUILD_MEMBER_REMOVE: true,
GUILD_MEMBER_UPDATE: true,
GUILD_ROLE_CREATE: true,
GUILD_ROLE_DELETE: true,
GUILD_ROLE_UPDATE: true,
GUILD_UPDATE: true,
MESSAGE_CREATE: true,
MESSAGE_DELETE: true,
MESSAGE_DELETE_BULK: true,
MESSAGE_UPDATE: true,
TYPING_START: true,
VOICE_STATE_UPDATE: true
}, restMode: true
});
const app = express();
const db = new Database();
db.init().then();
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(jwt({
secret: secret,
credentialsRequired: false,
algorithms: ["HS256"],
getToken: function fromHeaderOrQuerystring(req) {
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
return req.headers.authorization.split(' ')[1];
} else if (req.query && req.query.token) {
return req.query.token;
}
return null;
}
}));
app.post("/api/auth/code", async (req, res) => {
const access_token = req.query.access_token;
const refresh_token = req.query.refresh_token;
try {
const user = await oauth.getUser(access_token);
const auth_token = token.sign({ id: user.id, email: user.email, refreshToken: refresh_token }, secret);
return res.json({ "code": auth_token });
} catch (error) {
console.log(error);
res.status(401);
res.json({ "message": "unauthorized" });
}
});
app.get("/api/users/@me", async (req, res) => {
if (req.user && whitelisted.includes(req.user.id)) {
try {
const { id, username, discriminator, avatar } = await client.getRESTUser(req.user.id);
return res.json({ id, username, discriminator, avatar: !!avatar ? `https://cdn.discordapp.com/avatars/${id}/${avatar}.webp` : "https://cdn.discordapp.com/embed/avatars/0.png" });
} catch (error) {
console.log(error);
res.status(404).send("User not found");
return
}
} else {
res.status(401).send("unauthorized");
return
}
});
app.get("/api/stickers/@me", async (req, res) => {
if (req.user && whitelisted.includes(req.user.id)) {
if (req.query && req.query.pack_id) {
const data = await db.getPack(req.query.pack_id, req.user.id);
const { id, username, discriminator, avatar } = await client.getRESTUser(req.user.id);
const user = { id, username, discriminator, avatar: !!avatar ? `https://cdn.discordapp.com/avatars/${id}/${avatar}.webp` : "https://cdn.discordapp.com/embed/avatars/0.png" };
if (data) {
return res.json({ data, user });
}
return res.status(404).send({ "sticker": "not found", "pack_id": req.query.pack_id })
}
return (await handleRawStickers(req, res));
} else {
res.status(401).send("unauthorized");
return
}
});
// static stuff
app.get("/assets/login", async (req, res) => {
return res.sendFile(join(__dirname, "assets", "logo.png"))
});
async function handleRawStickers(req, res) {
try {
const packs = await db.getUserPacks(req.user.id);
res.json(packs);
return
} catch (error) {
console.log(error);
res.status(500).send("we ran out of cookies");
return
}
}
client.connect();
app.listen(2217, () => console.log("Started"));