Description
Which package is this bug report for?
ws
Issue description
Using the Bun Javascript runtime, a bot that loses internet connection will crash with a cryptic error. This can be reproduced by a simple Discord bot like the below code, listening to debug events.
Run this with bun yourtestbot.js
, and after it says "Heartbeat acknowledged" for the first time, switch off your internet connection. You will need to wait a bit, but eventually, when the shard attempts to reconnect, it will fail with these logs:
Preparing to connect to the gateway...
[WS => Manager] Fetched Gateway Information
URL: wss://gateway.discord.gg
Recommended Shards: 1
[WS => Manager] Session Limit Information
Total: 1000
Remaining: 981
[WS => Shard 0] Connecting to wss://gateway.discord.gg?v=10&encoding=json
[WS => Shard 0] Waiting for event hello for 60000ms
[WS => Shard 0] Preparing first heartbeat of the connection with a jitter of 0.4422302722907062; waiting 18241ms
[WS => Shard 0] Waiting for identify throttle
[WS => Shard 0] Identifying
shard id: 0
shard count: 1
intents: 1
compression: none
[WS => Shard 0] Waiting for event ready for 15000ms
[WS => Shard 0] Shard received all its guilds. Marking as fully ready.
[WS => Shard 0] First heartbeat sent, starting to beat every 41250ms
[WS => Shard 0] Heartbeat acknowledged, latency of 118ms.
[WS => Shard 0] Destroying shard
Reason: Zombie connection
Code: 4200
Recover: Resume
[WS => Shard 0] Connection status during destroy
Needs closing: true
Ready state: 1
[WS => Shard 0] Connecting to wss://gateway-us-east1-d.discord.gg?v=10&encoding=json
[WS => Shard 0] Waiting for event hello for 60000ms
1062 | break;
1063 | }
1064 | }
1065 | }
1066 | onError(error) {
1067 | if ("code" in error && KnownNetworkErrorCodes.has(error.code)) {
^
TypeError: error is not an Object. (evaluating '"code" in error')
at onError (/home/oirnoir/Desktop/test/node_modules/@discordjs/ws/dist/index.js:1067:19)
at <anonymous> (/home/oirnoir/Desktop/test/node_modules/@discordjs/ws/dist/index.js:685:12)
[WS => Shard 0] The gateway closed with an unexpected code 1006, attempting to resume.
[WS => Shard 0] Destroying shard
Reason: none
Code: 1006
Recover: Resume
[WS => Shard 0] Connection status during destroy
Needs closing: false
Ready state: 3
Bun v1.2.10 (Linux x64)
This error seems to stem from @discordjs/ws using Bun's built-in WebSocket implementation despite it seemingly not being fully ready yet (see bun #19176). Using node, it doesn't fail with this cryptic error, instead gracefully waiting until a connection is reestablished, as it should.
Perhaps disabling Bun's WebSocket implementation for Bun users or allowing an option in ClientOptions to switch WebSocket packages would be a workaround in the meantime. Alternatively, a check could be added to the @discordjs/ws error handling code to check whether event.error is null to avoid this nasty runtime error.
I've set the priority to low because bun use of discord.js is a minority, even though this is a bad crash for those of us using bun. If bun were more popular, this would earn a higher priority.
Code sample
const Discord = require('discord.js');
const client = new Discord.Client({intents: [Discord.GatewayIntentBits.Guilds]});
client.on('debug', console.log);
client.login("YOUR_DISCORD_TOKEN");
Versions
discord.js: v14.18.0 (via npm)
bun: 1.2.10
node (version I compared it to, works as intended here): v22.14.0
Operating System: Fedora Linux 42
KDE Plasma Version: 6.3.4
KDE Frameworks Version: 6.13.0
Qt Version: 6.9.0
Kernel Version: 6.14.2-300.fc42.x86_64 (64-bit)
Graphics Platform: Wayland
Processors: 22 × Intel® Core™ Ultra 7 155H
Memory: 46.7 GiB of RAM
Graphics Processor: Intel® Arc
Manufacturer: Notebook
Product Name: V54x_6x_TU
System Version: V540TU
Issue priority
Low (slightly annoying)
Which partials do you have configured?
No Partials
Which gateway intents are you subscribing to?
Guilds
I have tested this issue on a development release
No response