-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-manager.js
executable file
·139 lines (122 loc) · 3.16 KB
/
api-manager.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
/**
* @file Manages api interactions
* @author jojos38
*/
// ------------------------ SOME FUNCTIONS ------------------------ //
/**
* This is used by the delayChecking function to wait x ms
*/
async function delay(ms) {
return await new Promise(resolve => setTimeout(resolve, ms));
}
// ------------------------ SOME FUNCTIONS ------------------------ //
// --------- SOME VARIABLES --------- //
const logger = require('./logger.js');
const https = require('https');
// --------- SOME VARIABLES --------- //
class ApiManager {
#tokens;
#selfID;
#client;
#running;
#postDelay;
#initialized;
constructor(client, selfID, tokens, delay) {
this.#client = client;
this.#selfID = selfID;
this.#tokens = tokens;
this.#initialized = false;
this.#postDelay = delay || 60 * 60 * 1000;
}
/**
* Do a post request for a given api
* @param {string} hostname - The hostname of the api without path
* @param {string} path - The path for the post request (with a / at beginning)
* @param {object} data - An object containing the data to post
* @param {string} token - The authorization token for the api
*/
#post(hostname, path, data, token) {
if (!token) { logger.error("Error: no token provided for " + hostname); return; }
data = JSON.stringify(data);
const options = {
hostname: hostname,
port: 443,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
'Authorization': token
}
};
const req = https.request(options, res => {
if (res.statusCode == 200 || res.statusCode == 204) logger.info("Successfully posted guilds count for " + hostname);
else logger.error("Error while posting guilds count for " + hostname + ": " + res.statusCode);
});
req.on('error', error => { console.error(error) });
req.write(data)
req.end()
}
/**
* Gets the bot's data and send it to the api
*/
queryAndSend() {
const guilds = this.#client.guilds.cache;
let userCount = 0;
let guildCount = 0;
guilds.forEach(g => {
userCount += g.memberCount;
guildCount++;
})
this.#post(
'discordbotlist.com',
'/api/v1/bots/' + this.#selfID + '/stats',
{guilds: guildCount, users: userCount},
this.#tokens.discordbotlist
);
this.#post(
'top.gg',
'/api/bots/' + this.#selfID + '/stats',
{server_count: guildCount},
this.#tokens.topgg
);
this.#post(
'discord.bots.gg',
'/api/v1/bots/' + this.#selfID + '/stats',
{guildCount: guildCount},
this.#tokens.discordbots
);
this.#post(
'bots.ondiscord.xyz',
'/bot-api/bots/' + this.#selfID + '/guilds',
{guildCount: guildCount},
this.#tokens.botsondiscord
);
/*this.#post(
'discord.boats',
'/api/bot/' + this.#selfID,
{server_count: guildCount},
this.#tokens.discordboats
);*/
}
/**
* Stop the api from sending data
*/
stop() {
this.#initialized = false;
}
/**
* Starts the sending of the data at a given rate
*/
async init() {
if (!this.#initialized) {
this.#initialized = true;
logger.success("Api Manager initialized");
while (this.#initialized) {
await delay(this.#postDelay);
this.queryAndSend();
}
}
}
}
module.exports = ApiManager;