This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_discord_token.js
97 lines (95 loc) · 2.5 KB
/
request_discord_token.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
var credentials = scload("credentials.json", {warnOnFileNotFound: true}); //put at root directory of the mc server
function paramsToString(params) {
var result = '',
paramNames = [],
i;
for (i in params) {
paramNames.push(i);
}
for (i = 0; i < paramNames.length; i++) {
result += paramNames[i] + '=' + encodeURIComponent(params[paramNames[i]]);
if (i < paramNames.length - 1) result += '&';
}
return result;
}
function invokeNow(fn) {
if (__plugin.bukkit) {
server.scheduler['runTask(org.bukkit.plugin.Plugin, java.lang.Runnable)'](
__plugin,
fn
);
return;
}
if (__plugin.canary) {
fn();
return;
}
}
function invokeLater(fn) {
if (__plugin.bukkit) {
server.scheduler['runTaskAsynchronously(org.bukkit.plugin.Plugin, java.lang.Runnable)'](__plugin, fn);
return;
}
if (__plugin.canary) {
setTimeout(fn, 20);
return;
}
}
exports.request = function (request, callback) {
invokeLater(function () {
var url, paramsAsString, conn, requestMethod;
if (typeof request === 'string') {
url = request;
requestMethod = 'GET';
} else {
url = request.url;
paramsAsString = paramsToString(request.params);
if (request.method) {
requestMethod = request.method;
} else {
requestMethod = 'GET';
}
if (requestMethod == 'GET' && request.params) {
// append each parameter to the URL
url = request.url + '?' + paramsAsString;
}
}
conn = new java.net.URL(url).openConnection();
conn.requestMethod = requestMethod;
conn.doOutput = true;
conn.instanceFollowRedirects = false;
conn.setRequestProperty(
'Authorization',
'Bot ' + credentials.discord_token
);
conn.setRequestProperty(
'user-agent',
'DiscordBot (https://github.com/walterhiggins/ScriptCraft/blob/development/src/main/js/modules/http/request.js, 3.4.0)'
);
if (conn.requestMethod == 'POST') {
conn.doInput = true;
// put each parameter in the outputstream
conn.setRequestProperty(
'Content-Type',
'application/x-www-form-urlencoded'
);
conn.setRequestProperty('charset', 'utf-8');
conn.setRequestProperty('Content-Length', '' + paramsAsString.length);
conn.useCaches = false;
var wr = new java.io.DataOutputStream(conn.getOutputStream());
wr.writeBytes(paramsAsString);
wr.flush();
wr.close();
}
var rc = conn.responseCode;
var response;
var stream;
if (rc == 200) {
stream = conn.getInputStream();
response = new java.util.Scanner(stream).useDelimiter('\\A').next();
}
invokeNow(function () {
callback(rc, response);
});
});
};