-
Notifications
You must be signed in to change notification settings - Fork 94
/
sharding.js
52 lines (43 loc) · 1.61 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
import {ShardingManager} from "discord.js";
import {loadConfig} from "./misc/config.js";
const config = loadConfig();
const manager = new ShardingManager('./SkinPeek.js', {
token: config.token,
mode: "worker"
});
let allShardsReady = false;
const sendAllShardsReady = () => {
manager.broadcastEval((client) => client.skinPeekShardMessageReceived({type: "shardsReady"}));
}
console.log("[Shards] Starting spawn");
manager.on("shardCreate", (shard) => {
console.log(`[Shard ${shard.id}] Spawned`);
shard.on("death", () => {
console.log(`[Shard ${shard.id}] Died`);
});
shard.on("disconnect", (error, id) => {
console.log(`[Shard ${id}] Discord Websocket Disconnected`);
process.exit(1);
});
if(allShardsReady) {
// this shard was respawned, tell it that all shards are ready
console.log("[Shards] Sending shardsReady to respawned shard (waiting for it to be ready)");
// shard.on("ready", sendAllShardsReady);
shard.on("ready", () => {
console.log(`[Shard ${shard.id}] Ready`);
sendAllShardsReady();
});
}
shard.on("disconnect", () => console.log(`[Shard ${shard.id}] Disconnected`));
shard.on("reconnecting", () => console.log(`[Shard ${shard.id}] Reconnecting`));
shard.on("message", (message) => {
// console.log(`[Shard ${shard.id}] Message: ${JSON.stringify(message)}`);
if(message === "shardReady" && allShardsReady) sendAllShardsReady();
});
});
manager.spawn({
timeout: config.shardReadyTimeout,
}).then(() => {
allShardsReady = true;
sendAllShardsReady();
});