-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (51 loc) · 2.19 KB
/
server.js
File metadata and controls
65 lines (51 loc) · 2.19 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
// Initalization
const express = require('express');
const config = require('./config.json'); // Website config
const FormData = require('form-data');
const fetch = require('node-fetch');
const app = express();
app.use(require('express-session')(config.session))
app.set('view engine', 'pug');
app.get('/', async (req, resp) => {
const data = await fetch(`https://discord.com/api/users/@me`, {headers: { Authorization: `Bearer ${req.session.bearer_token}` } }); // Fetching user data
const json = await data.json();
resp.render('./index.pug')
})
app.get('/login/callback', async (req, resp) => {
const accessCode = req.query.code;
if (!accessCode) // If something went wrong and access code wasn't given
return resp.send('No access code specified');
// Creating form to make request
const data = new FormData();
data.append('client_id', config.oauth2.client_id);
data.append('client_secret', config.oauth2.secret);
data.append('grant_type', 'authorization_code');
data.append('redirect_uri', config.oauth2.redirect_uri);
data.append('scope', 'identify');
data.append('code', accessCode);
// Making request to oauth2/token to get the Bearer token
const json = await (await fetch('https://discord.com/api/oauth2/token', {method: 'POST', body: data})).json();
req.session.bearer_token = json.access_token;
resp.redirect('/'); // Redirecting to main page
});
app.get('/login', (req, res) => {
// Redirecting to login url
res.redirect(`https://discord.com/api/oauth2/authorize` +
`?client_id=${config.oauth2.client_id}` +
`&redirect_uri=${encodeURIComponent(config.oauth2.redirect_uri)}` +
`&response_type=code&scope=${encodeURIComponent(config.oauth2.scopes.join(" "))}`)
})
app.use(express.static("public"));
app.get('/botstarters', async (req, resp) => {
resp.render('./botstarters.pug')
})
app.get('/aboutme', async (req, resp) => {
resp.render('./aboutme.pug')
})
app.get("*", (request, response) => {
response.sendFile(__dirname + "/views/404.html");
});
// listen for requests :)
app.listen(config.port || 80, () => {
console.log(`Listening on port ${config.port || 80}`)
});