-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.js
65 lines (55 loc) · 1.84 KB
/
request.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
/* jshint esversion: 6 */
const botsPath = "https://bots.server-discord.com";
const { stringify } = require('querystring')
, { request } = require('https')
;
/**
* @function
* @param {Object} params
* @param {string} [postData]
* @returns {Promise<Object>}
*/
function send(params, postData) {
return new Promise((resolve, reject) => {
let req = request(params, (res) => {
res.setEncoding('utf8');
res.on('data', (data) => resolve( JSON.parse(data) ));
});
req.on('error', reject);
if(postData) req.write(postData);
req.end();
});
}
module.exports = {
/**
* @function
* @param {Object} params
* @returns {Promise<Object>}
*/
request: (params) => {
if(params.body) {
let postData = stringify( params.body );
params.headers['Content-Type'] = 'application/x-www-form-urlencoded';
params.headers['Content-Length'] = Buffer.byteLength(postData);
return send(params, postData);
} else return send(params);
},
/**
* @function
* @param client
* @param {Object} opt
*/
sendStat: (client, opt) => {
let data = { servers: 0, shards: 0 };
if (client.shard && client.shard.count !== 1) data.shards = client.shard.count;
else if (client.shards && client.shards.size !== 1) data.shards = client.shards.size;
if (client.guilds.cache) data.servers = client.guilds.cache.size;
else data.servers = client.guilds.size;
opt.body = data;
module.exports.request(opt)
.then((r) => {
if(r.error) return console.error("[sdc-api | Авто-пост] Ошибка в работе\n" + r.error.message);
else return console.info("[sdc-api | Авто-пост] Статистика для " + client.user.tag + " опубликована на мониторинг.\n" + encodeURI(botsPath + "/" + client.user.id));
}, (e) => console.error("[sdc-api | Авто-пост] Ошибка в работе | ", e));
}
};