This repository was archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
132 lines (107 loc) · 2.94 KB
/
Copy pathindex.mjs
File metadata and controls
132 lines (107 loc) · 2.94 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// @ts-check
import { Client, GatewayDispatchEvents, GatewayOpcodes } from "discord.js";
import WebSocket from "ws";
let readyPayload = {};
let helloPayload = {};
let backfill = [];
const client = new Client({
intents: parseInt(process.env.DISCORD_INTENTS || "37383"),
shards: "auto",
});
const wss = new WebSocket.Server({
port: parseInt(process.env.PORT || "6969"),
});
console.log(`[Park] Listening on ws://localhost:${wss.options.port}`);
function broadcastClients(packet) {
wss.clients.forEach((ws) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(packet));
}
});
}
const DONT_SEND_TO_CLIENT_PARK_OPCODES = [GatewayOpcodes.HeartbeatAck];
const BACKFILL_EVENTS = [GatewayDispatchEvents.GuildCreate];
/**
* Discord -> Park
*/
client.on("raw", (packet) => {
if (packet.op === GatewayOpcodes.Hello) {
console.log("[Discord -> Park] Hello payload received from Discord.");
helloPayload = packet;
return;
}
if (packet.t === GatewayDispatchEvents.Ready) {
console.log(
`[Discord -> Park] Ready payload received from Discord. | ${packet.d.guilds.length} guilds.`
);
readyPayload = packet;
return;
}
if (DONT_SEND_TO_CLIENT_PARK_OPCODES.includes(packet.op)) {
return;
}
if (BACKFILL_EVENTS.includes(packet.t)) {
backfill.push(packet);
}
broadcastClients(packet);
});
/**
* Park -> Discord
*/
const DONT_SEND_TO_DISCORD_OPCODES = [
GatewayOpcodes.Heartbeat,
GatewayOpcodes.HeartbeatAck,
];
let currentHeartbeat = 0;
/**
* @param {WebSocket} ws
*/
function handleHeartbeat(ws) {
ws.send(
JSON.stringify({
op: GatewayOpcodes.HeartbeatAck,
d: currentHeartbeat === 0 ? null : currentHeartbeat,
})
);
console.log(
`[Park -> Client] Ack'd heartbeat | Up to ${currentHeartbeat} beats.`
);
currentHeartbeat++;
}
wss.on("connection", (ws, req) => {
console.log(
`[Client 🤝 Park] New connection from ${req.socket.remoteAddress} sending hello payload.`
);
ws.send(JSON.stringify(helloPayload));
ws.on("message", (message) => {
const buffer = JSON.parse(message.toString());
if (buffer.op === GatewayOpcodes.Identify) {
console.log(
`[Cache -> Client] Sending ready payload! Then backfilling guilds.`
);
ws.send(JSON.stringify(readyPayload));
backfill.forEach((packet) => {
ws.send(JSON.stringify(packet));
});
return;
}
if (buffer.op === GatewayOpcodes.Heartbeat) {
return void handleHeartbeat(ws);
}
if (DONT_SEND_TO_DISCORD_OPCODES.includes(buffer.op)) {
return;
}
if (buffer.d && buffer.d.token) {
buffer.d.token = client.token;
}
console.log(buffer);
console.log(
`[Client -> Discord] Forwarding packet ${
buffer.t || buffer.op || "UNKNOWN"
}`
);
// @ts-ignore - Accessing private property
client.ws.broadcast(JSON.stringify(buffer));
});
});
client.login(process.env.DISCORD_TOKEN);