-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
132 lines (117 loc) · 4.31 KB
/
server.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
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const fs = require('fs')
const fetch = require('node-fetch')
const client = require('.')
// const url = (port === 3000) ? `https://discord.com/api/oauth2/authorize?client_id=${process.env.client}&redirect_uri=https%3A%2F%2F${port}-${process.env.GITPOD_WORKSPACE_URL.replace('https://', '')}&response_type=code&scope=identify%20guilds%20guilds.join%20guilds.members.read` : `https://discord.com/api/oauth2/authorize?client_id=${process.env.client}&redirect_uri=https%3A%2F%2Ffract-m3ac.onrender.com%2F&response_type=code&scope=identify%20guilds%20guilds.join%20guilds.members.read`
const uri = (process.env.GITPOD_WORKSPACE_URL) ? `https://${port}-${process.env.GITPOD_WORKSPACE_URL.replace('https://', '')}` : `https://fract-m3ac.onrender.com`
const redirect = 'https://discord.com/api/oauth2/authorize?' + new URLSearchParams({
client_id: process.env.client,
redirect_uri: uri,
response_type: 'code',
scope: 'identify guilds',
}).toString()
console.log(redirect)
app.use(express.static('public'))
function type(data) {
return Object.prototype.toString.call(data).slice(8, -1)
}
function isObject(data) {
return type(data) === 'Object' && Object.getPrototypeOf(data).constructor === Object && Object.getPrototypeOf(data) === Object.prototype
}
function execute(code, locals) {
let s = ''
for (const [k, v] of Object.entries(locals)) {
if (type(v) === 'Array') {
s += `const ${k} = [${v.join(', ')}];\n`
continue
}
if (type(v) === 'String') {
s += `let ${k} = "${v}";\n`
continue
}
if (type(v) === 'Number') {
s += `let ${k} = ${v};\n`
continue
}
if (isObject(v)) {
s += `let ${k} = ${JSON.stringify(v)};\n`
continue
}
}
let output = ''
try {
const forge = new Function('process', 'eval', 'Function', 'Object', 'include', 'client', `${s} return ${code.match(/(?<=#{)(.|\n)+?(?=})/g)};`)
output = forge(null, null, null, Object, (file) => fs.readFileSync(`views/${file}`, 'utf-8'), client)
} catch (e) {
console.log(e)
}
return output
}
function render(file, locals) {
let html = fs.readFileSync(file, 'utf-8')
const matches = html.matchAll(/#{(.|\n)+?}/g)
for (const match of matches) {
console.log(match[0])
html = html.replace(match[0], execute(match[0], locals))
}
return html
}
function route(req, res, locals) {
res.send(render(`views${req.originalUrl.split('?')[0] + '/index.html'}`, locals))
}
app.get('/', async (req, res) => {
const { code } = req.query
console.log(code)
if (code) {
try {
const params = new URLSearchParams({
client_id: process.env.client,
client_secret: process.env.secret,
code,
grant_type: 'authorization_code',
redirect_uri: uri, // needs checking
scope: 'identify guilds',
})
console.log(params)
const tokenResponseData = await fetch('https://discord.com/api/oauth2/token', {
method: 'POST',
body: params,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
const oauthData = await tokenResponseData.json()
console.log(oauthData)
if (oauthData.error === 'invalid_grant') {
console.log(`OAUTH ERROR: ${oauthData.error}`)
route(req, res, { title: 'Discord', message: 'Hello, World!', auth: redirect })
} else {
const access = oauthData.access
const refresh = oauthData.refresh
const userResult = await fetch('https://discord.com/api/users/@me', {
headers: {
authorization: `${oauthData.token_type} ${access}`,
},
})
const user = await userResult.json()
console.log(user)
route(req, res, { title: 'Discord', message: 'Hello, World!', auth: redirect, user })
}
} catch (error) {
// NOTE: An unauthorized token will not throw an error
// tokenResponseData.statusCode will be 401
console.log(error)
route(req, res, { title: 'Discord', message: 'Hello, World!', auth: redirect })
}
} else {
route(req, res, { title: 'Discord', message: 'Hello, World!', auth: redirect })
}
})
app.get('/status', (req, res) => {
route(req, res, { title: client.user.tag, version: process.version })
})
app.listen(port, () => {
console.log(`Server is online at port ${port}!`)
})