-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstart.js
More file actions
40 lines (31 loc) · 1.02 KB
/
start.js
File metadata and controls
40 lines (31 loc) · 1.02 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
const {ShardingManager} = require('discord.js');
const config = require('./src/utils/config');
const manager = new ShardingManager('./src/bot.js', {
token: config.token
});
function spawnShards() {
// This variable is a JSON-encoded object that gets passed to the shard as args
// The values are then de-serialized and attached to the client as client.startArgs
// In the future, it may allow the manager to pass data to the shards at start
// But it is currently unused
const startArgs = JSON.stringify({});
manager.shardArgs = [startArgs];
return manager.spawn();
}
const shardsLaunched = new Set();
manager.on('launch', shard => {
shardsLaunched.add(shard.id);
console.log(`Launched Shard ${shard.id} (${shardsLaunched.size}/${manager.totalShards})`);
});
manager.on('message', (shard, message) => {
if (message === 'exit') {
process.exit();
}
});
process.on('exit', () => {
manager.respawn = false;
for (const shard of manager.shards.values()) {
shard.process.kill();
}
});
spawnShards().catch(console.error);