-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.js
276 lines (215 loc) · 7.37 KB
/
status.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var readline = require('readline');
const yargs = require('yargs');
var logToken = false;
function setDiscordToken(token) {
process.env["DISCORD_TOKEN"] = token;
console.log("Token successfully set.");
if (logToken) {
console.log("Token: ", token);
}
}
async function promptMFA() {
return new Promise(function (resolve, reject) {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
rl.question('Auth code: ', function(authCode) {
rl.close();
resolve(authCode);
});
});
};
async function handleMFA(ticket, authCode) {
var xhr = new XMLHttpRequest();
const authUrl = "https://discord.com/api/v9/auth/mfa/totp";
xhr.open("POST", authUrl);
xhr.setRequestHeader("Content-Type", "application/json");
authData = {code: authCode, ticket: ticket}
return new Promise(function (resolve, reject) {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status == 200) {
console.log("MFA successful.");
var jsonResponse = JSON.parse(xhr.responseText);
setDiscordToken(jsonResponse["token"]);
resolve(true)
}
if (xhr.status == 400) {
console.log("Invalid authentication code.");
resolve(false)
}
}
}
xhr.send(JSON.stringify(authData));
})
}
function checkForMFA(returnCode) {
if (returnCode == 1) {
return true;
}
return false;
}
async function login(email, password) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
const authUrl = "https://discord.com/api/v9/auth/login";
xhr.open("POST", authUrl);
xhr.setRequestHeader("Content-Type", "application/json");
loginData ={login:email, password:password}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status == 200) {
var jsonResponse = JSON.parse(xhr.responseText);
if ("mfa" in jsonResponse && jsonResponse["mfa"] == true) {
console.log("MFA required to login.");
resolve({success: true, mfa: true, ticket: jsonResponse["ticket"]})
} else {
console.log("Login successful.");
setDiscordToken(jsonResponse["token"]);
resolve({success: true, mfa: false, ticket: null});
}
}
if (xhr.status == 400) {
console.log("Login failed, try again.");
reject({success: false, mfa: false, ticket: null});
}
}
}
xhr.send(JSON.stringify(loginData));
});
};
async function getCredentials() {
return new Promise(function (resolve, reject) {
// new Interface to handle console in-/output
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
rl.stdoutMuted = false;
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted && stringToWrite != '\r\n') {
writeLen = stringToWrite.length
if (writeLen > 1) {
rl.output.write(query + "*".repeat(writeLen - query.length));
} else {
rl.output.write("*");
}
} else {
rl.output.write(stringToWrite);
}
};
var query = 'Email: '
rl.question(query, function(email) {
query = 'Password: '
rl.question(query, function(password) {
rl.close();
resolve([email, password])
});
rl.stdoutMuted = true;
});
});
};
async function setStatus(newStatus, afterLoginCallback) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
const url = "https://discord.com/api/v9/users/@me/settings";
xhr.open("PATCH", url);
if (!("DISCORD_TOKEN" in process.env)) {
console.log("No token provided, please login.");
resolve(false); // TODO: resolve or reject? reject might throw unwanted errors
}
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("authorization", process.env["DISCORD_TOKEN"]);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.responseText);
if (xhr.status == 400) {
console.log("Token invalid.");
console.log("Please login.");
resolve(false); // TODO: resolve or reject? reject might throw unwanted errors
}
resolve(true);
}
};
var data = {status: newStatus};
xhr.send(JSON.stringify(data));
});
};
async function tryAgain() {
return new Promise(function (resolve, reject) {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true
});
rl.question('Retry (Y/n): ', function(answer) {
rl.close();
if (answer.toLowerCase() == 'y') {
resolve(true);
} else if (answer.toLowerCase() == 'n'){
resolve(false);
} else {
console.log("Invalid answer.");
resolve(tryAgain());
}
});
});
}
module.exports = {
setStatus,
login,
handleMFA
}
async function handleLoginProcess() {
var [email, password] = await getCredentials();
var result = await login(email, password);
// mfa required
if (result.mfa) {
var authCode = await promptMFA()
while (!(await handleMFA(result.ticket, authCode))) {
if (!(await tryAgain())) {
process.exit(0);
}
authCode = await promptMFA()
}
}
};
if (require.main === module) {
// run directly from command line
const argv = yargs
.option('status', {
alias: 's',
description: 'Sets the given status in Discord',
type: 'string',
})
.demandOption(['status'], 'Please a the status to set')
.boolean('token')
.alias('token', ['t'])
.describe('token', 'Whether to log the token to the console')
.help()
.alias('help', 'h')
.argv;
const newStatus = argv.status;
logToken = argv.token;
if (!(["online", "dnd", "idle", "invisible"].includes(newStatus))) {
console.log(`${newStatus} is no valid status. Use one of online, dnd, idle or invisible.`);
process.exit(1);
}
(async () => {
var tokenInEnv = "DISCORD_TOKEN" in process.env;
while (!(tokenInEnv)) {
console.log("DISCORD_TOKEN not set.")
console.log("Starting authentication process.")
await handleLoginProcess()
tokenInEnv = "DISCORD_TOKEN" in process.env;
if (!(tokenInEnv) && !(await tryAgain())) {
process.exit(0);
}
}
await setStatus(newStatus);
})();
}