-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscord.js
60 lines (53 loc) · 2.18 KB
/
Discord.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
const fetch = require("node-fetch")
// REDIRECT_URI Also needs to be set in discord developers > Your App > OAuth2 > Redirects
config = {
API_ENDPOINT: "https://discord.com/api/v8",
CLIENT_ID: "", // e.g. 123456789101112131
CLIENT_SECRET: "", // e.g. pxS3D8EB6M9zWBUNFpJmge9NcBX95bDK
REDIRECT_URI: "" // http://127.0.0.1:5000/discord/login
}
exports.exchangeCode = (code, state) => {
return new Promise((resolve, reject) => {
let data = `client_id=${config.CLIENT_ID}&client_secret=${config.CLIENT_SECRET}&grant_type=authorization_code&code=${encodeURIComponent(code)}&redirect_uri=${encodeURIComponent(config.REDIRECT_URI)}`
if (slate) { data += `&state=${encodeURIComponent(state)}` }
fetch(config.API_ENDPOINT + '/oauth2/token', {
method: "POST",
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: data
})
.then(r => r.json())
.then(accessData => {
if (accessData.access_token) { resolve(accessData)
} else reject(accessData);
})
.catch(reject)
})
}
exports.getUserData = (access_token) => {
return new Promise((resolve, reject) => {
fetch(config.API_ENDPOINT + "/users/@me", {
method: "GET",
headers: { 'Authorization': `Bearer ${access_token}` }
})
.then(r => r.json())
.then(user => {
if (user.id) { resolve(user)
} else reject(user)
})
.catch(reject);
})
}
exports.refreshToken = (refreshToken) => {
let data = `client_id=${config.CLIENT_ID}&client_secret=${config.CLIENT_SECRET}&grant_type=refresh_token&refresh_token=${encodeURIComponent(refreshToken)}&redirect_uri=${encodeURIComponent(config.REDIRECT_URI)}`
fetch(config.API_ENDPOINT + '/oauth2/token', {
method: "POST",
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: data
})
.then(r => r.json())
.then(accessData => {
if (accessData.access_token) { resolve(accessData)
} else reject(accessData);
})
.catch(reject)
}