-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsharding.js
85 lines (85 loc) · 2.6 KB
/
sharding.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
/**
* Created by julia on 19.09.2016.
*/
const Discord = require('discord.js');
const config = require('./config/main.json');
var winston = require('winston');
var request = require("request");
let guilds = 0;
let users = 0;
if (!config.beta) {
var StatsD = require('node-dogstatsd').StatsD;
var dogstatsd = new StatsD();
}
let ShardManager = new Discord.ShardingManager('./index.js', {}, config.shards, true);
ShardManager.spawn(config.shards, 8000).then(shards => {
winston.info('Spawned Shards!');
timerFetchGuilds();
}).catch(winston.error);
function timerFetchGuilds() {
winston.info('Fetching Guilds - timer!');
setTimeout(() => {
fetchGuilds();
setInterval(() => {
fetchGuilds();
}, 1000 * 60 * 5);
}, 1000 * 10);
}
function fetchGuilds() {
winston.info('Fetching Guilds!');
ShardManager.fetchClientValues('guilds.size').then(results => {
ShardManager.broadcastEval('var x=0;this.guilds.map(g => {x += g.memberCount});x;').then(res => {
winston.info(res);
res = res.reduce((a, b) => a + b);
users = res;
winston.info('loaded guilds!');
guilds = results.reduce((prev, val) => prev + val, 0);
winston.info(`${results.reduce((prev, val) => prev + val, 0)} total guilds`);
updateStats();
}).catch(err => {
winston.error(err);
});
}).catch(err => {
winston.error(err);
});
}
function updateStats() {
if (!config.beta) {
dogstatsd.gauge('musicbot.guilds', guilds);
dogstatsd.gauge('musicbot.users', users);
}
let requestOptions = {
headers: {
Authorization: config.discord_bots_token
},
url: `https://bots.discord.pw/api/bots/${config.bot_id}/stats`,
method: 'POST',
json: {
"server_count": guilds
}
};
request(requestOptions, function (err, response, body) {
if (err) {
return winston.error(err);
}
winston.info('Stats Updated!');
winston.info(body);
});
if (!config.beta) {
let requestOptionsCarbon = {
url: `https://www.carbonitex.net/discord/data/botdata.php`,
method: 'POST',
json: {
"server_count": guilds,
"key": config.carbon_token
}
};
request(requestOptionsCarbon, function (err, response, body) {
if (err) {
return winston.error(err);
}
winston.info('Stats Updated Carbon!');
winston.info(body);
});
}
}