forked from ReloadedDevs/CloneBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster.js
43 lines (32 loc) · 1.37 KB
/
master.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
const botHandler = require("./worker_handling/bot");
const superagent = require("superagent");
const fs = require("fs");
var shardAmount = null;
function handleWorker(worker) {
if(worker.type === "bot") botHandler(worker);
}
Object.defineProperty(cluster, "onlineWorkers", {
get: () => Object.keys(cluster.workers)
.map(id => cluster.workers[id])
.filter(work => work.isConnected())
});
const config = JSON.parse(fs.readFileSync("bot/json/config.json", "utf8"));
async function init() {
const { body: { shards: totalShards } } = await superagent.get("https://discordapp.com/api/gateway/bot").set("Authorization", config.TOKEN);
process.totalShards = totalShards;
shardAmount = totalShards;
let shardsPerWorker;
const coreCount = require("os").cpus().length;
if(coreCount >= totalShards) shardsPerWorker = 1;
else shardsPerWorker = Math.ceil(totalShards / coreCount);
const workerCount = Math.ceil(totalShards / shardsPerWorker);
for(let i = 0; i < workerCount; i++) {
let shardStart = i * shardsPerWorker, shardEnd = ((i + 1) * shardsPerWorker) - 1;
if(shardEnd > totalShards - 1) shardEnd = totalShards - 1;
let shardRange = shardStart === shardEnd ? `shard ${shardStart}` : `shards ${shardStart}-${shardEnd}`;
const worker = cluster.fork();
Object.assign(worker, { type: "bot", shardStart, shardEnd, shardRange, totalShards });
handleWorker(worker);
}
}
init();