-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscord.js
108 lines (99 loc) · 3.28 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
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
const request = require('request');
const { props } = require('./props');
const { logger } = require('./logger');
const REDIRECT_URI = `${props.hostname}${props.extPortString}/oauth/redirect`;
const DISCORD_BASE = 'https://discordapp.com/api';
const SCOPE = 'identify email guilds';
const discord_options = (token, url, method) => {
return {
method: method || 'GET',
url: `${DISCORD_BASE}${url}`,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${token}`
}
};
};
const oauth_base_options = form => {
return {
method: 'POST',
url: `${DISCORD_BASE}/oauth2/token`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
Authorization: `Basic ${Buffer.from(`${props.clientId}:${props.clientSecret}`).toString('base64')}`
},
form
};
};
const oauth_options = code =>
oauth_base_options({
grant_type: 'authorization_code',
code: code,
redirect_uri: REDIRECT_URI,
scope: SCOPE
});
const refresh_oauth_options = refresh_token =>
oauth_base_options({
grant_type: 'refresh_token',
refresh_token,
scope: SCOPE
});
const discord_base = (callback, options) => {
logger.info('Discord request: ' + options.url);
request(options, (error, response, body) => {
if (error) {
logger.error(error);
} else if (response.statusCode != 200) {
logger.error('Bad Status on response: ' + response.statusCode + '\n\n' + body);
} else {
try {
callback(JSON.parse(body));
logger.debug('Discord response body parsed: ' + body);
return;
} catch (e) {
logger.error(e.toString());
}
}
callback(undefined);
});
};
const discord = (token, url, callback) => discord_base(callback, discord_options(token, url));
/*
{ id: '206111742625972224',
username: 'TrumpetX',
avatar: null,
discriminator: '9894',
email: 'david@greene.ninja',
verified: true,
locale: 'en-US',
mfa_enabled: true,
flags: 0 }
*/
module.exports.me = (token, callback) => discord(token, '/users/@me', callback);
/*
[ { id: '615756929431502848',
name: 'Magma Carta Club',
icon: 'f89d7f5bb0df112f66050af50f0dc885',
owner: false,
permissions: 2147483647,
features: [] }
...
]
*/
module.exports.guilds = (token, callback) => discord(token, '/users/@me/guilds', callback);
module.exports.oauth = (code, callback) => discord_base(callback, oauth_options(code));
module.exports.refreshOauth = (refreshToken, callback) => discord_base(callback, refresh_oauth_options(refreshToken));
module.exports.discordUrl = state =>
`${DISCORD_BASE}/oauth2/authorize?state=${encodeURIComponent(state)}&client_id=${props.clientId}&redirect_uri=${encodeURIComponent(
REDIRECT_URI
)}&response_type=code&scope=${encodeURIComponent(SCOPE)}`;
// https://discordapp.com/developers/docs/topics/permissions
module.exports.ADMIN_PERMISSION = 0x00000008;
module.exports.ADMINISTRATOR = this.ADMIN_PERMISSION;
module.exports.MANAGE_GUILD = 0x00000020;
module.exports.MANAGE_CHANNELS = 0x00000010;
module.exports.MANAGE_ROLES = 0x10000000;
module.exports.MANAGE_WEBHOOKS = 0x20000000;
module.exports.botUrl = `${DISCORD_BASE}/oauth2/authorize?client_id=${props.clientId}&scope=bot&permissions=${props.botPermissions}`;