-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
69 lines (61 loc) · 1.77 KB
/
index.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
const request = require("request");
const config = require("./config.json");
const STATUS_URL = "https://discord.com/api/v9/users/@me/settings";
const headers = { Authorization: config.token };
async function loop() {
if (config.animation.length === 0) {
return;
}
let i = 0;
const interval = setInterval(async () => {
const anim = config.animation[i % config.animation.length];
try {
const res = await doRequest(anim.text, anim.emojiID, anim.emojiName);
if (res === false) {
clearInterval(interval);
}
} catch (err) {
console.error(err);
} finally {
i++;
}
}, 5000);
console.log("Running...");
}
async function doRequest(text, emojiID = null, emojiName = null) {
return new Promise((resolve, reject) => {
request({
method: "PATCH",
uri: STATUS_URL,
headers: headers,
json: {
custom_status: {
text: text,
emoji_id: emojiID,
emoji_name: emojiName
}
}
}, (err, res, body) => {
if (err) {
reject(err);
return;
}
if (res.statusCode === 200) {
resolve(true);
return;
}
const remaining = res.headers["x-rate-limit-remaining"] || 0;
const resetAfter = res.headers["x-rate-limit-reset-after"] || 0;
if (remaining <= 0 && resetAfter > 0 && config.handleRatelimit) {
const ratelimit = resetAfter * 1000;
console.log("Hit ratelimit: " + ratelimit + "ms");
setTimeout(() => {
doRequest(text, emojiID, emojiName).then(resolve).catch(reject);
}, ratelimit);
return;
}
reject(new Error("Invalid Status Code: " + res.statusCode));
});
});
}
loop();