-
Notifications
You must be signed in to change notification settings - Fork 93
/
index.js
executable file
·56 lines (49 loc) · 1.42 KB
/
index.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
'use strict';
import 'dotenv/config';
import { ClusterManager, HeartbeatManager, ReClusterManager, fetchRecommendedShards } from 'discord-hybrid-sharding';
import { beaver } from './functions/consoleLogging.js';
const shardsPerClusters = parseInt(process.env.SPC);
let manager = new ClusterManager('./bot.js', {
shardsPerClusters: shardsPerClusters,
token: process.env.TOKEN,
mode: 'process',
restarts: {
max: 5,
interval: 24 * 60 * 60 * 1000
}
});
manager.extend(new ReClusterManager());
manager.extend(
new HeartbeatManager({
interval: 12000,
maxMissedHeartbeats: 5
})
);
manager.on('clusterCreate', (cluster) => beaver.log('sharding', `Created cluster ${cluster.id}`));
async function spawnShards() {
beaver.log('sharding', `Starting bot!`);
await manager.spawn();
setInterval(reclusterShards, 24 * 60 * 60 * 1000);
}
async function reclusterShards() {
try {
const recommendedShards = await fetchRecommendedShards(process.env.TOKEN);
if (recommendedShards != manager.totalShards) {
beaver.log('sharding', `Reclustering from ${manager.totalShards} to ${recommendedShards} shards`);
manager.recluster.start({
restartMode: 'gracefulSwitch',
totalShards: recommendedShards,
shardsPerClusters: shardsPerClusters,
shardList: null,
shardClusterList: null
});
}
} catch (error) {
beaver.log('sharding', error);
}
}
try {
spawnShards();
} catch (error) {
beaver.log('sharding', error);
}