forked from MegaVasiliy007/sdc-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
82 lines (73 loc) · 2.21 KB
/
request.js
File metadata and controls
82 lines (73 loc) · 2.21 KB
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
/* jshint esversion: 6 */
const botsPath = "https://bots.server-discord.com";
const { stringify } = require('querystring');
const { 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: async (client, opt) => {
let data = { servers: 0, shards: 0 };
if (client.shard) {
data.shards = client.shard.count;
data.servers = await client.shard.fetchClientValues('guilds.cache.size')
.then((results) => results.reduce((acc, guildCount) => acc + guildCount, 0));
} else if (client.shards) {
data.shards = client.shards.size;
data.servers = client.guilds.cache.size;
} else {
data.servers = client.guilds.cache.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)
);
},
};