#Discord.js is not working in —watch or —hot mode
1 messages · Page 1 of 1 (latest)
it works fine for me
could you share more details?
I just load all of the events and register them using this code.
it breaks at client.on() / once()
after you create the client, could you put a client.on("debug", console.log)
I've noticed that sometimes it'd freeze when trying to connect to the WebSocket, so maybe that's why it's not firing the events for you
if it works fine, you should see a bunch of messages about the WebSocket connection
otherwise it'd just be stuck on connecting
Unfortunately, I don't see any message
sounds similar to https://github.com/oven-sh/bun/issues/2077#issuecomment-1574909943
then the client probably can't connect to the Discord gateway
I solved that issue by just restarting the bot a billion times until it worked
yeah, looks related
I guess you just need to wait until full support
In my case it connects
then that makes everything 1000x weirder
what are the event na,es you are using. i have a feeling you may be using the wrong event names or missing certain intent bits
Events.InteractionCreate
The basic one
And I tested it with node.js and it works (I'm currently using node js for development but I'd like to use bun in the future)
I forgot to mention that client.once works…
My guess is that it is related to some web socket stuff
Yeah that sounds plausible. Maybe something to do with TLS
@serene shell what exact version of discord.js are you on
i cant reproduce the bug with client.on interactionCreate
@hasty socket thanks for sharing your bot code. this is enough to segfault bun
import "lodash/omit.js";
import "lodash/findIndex.js";
import "discord.js";
Bun.gc(true);
"discord.js": "^14.11.0",
I'm also using some lodash utilities
can you share a full reproduction of what youre doing if it isnt a trillion file project
Sadly it is a project with a lot of files...
i can share my events loader amd core config thought
can you try removing any client.on()/.once()
adn removing .login()
and putting at the end Bun.gc(true)
to see if its this same segfault
await CommandsManager.loadSlashCommands();
await EventsManager.registerEventListeners(client);
await client.login(Config.discord.CLIENT_TOKEN);
my code looks like this
yeah like comment all that out and just run bun.gc(true)
does that crash or does it just exit
basically, does it crash with garbage collector related reasons before the bot can even connect
Yes, it crashes
I just upgrade to canary and it crashes even earlier
Alright, thanks!
I'm curious, what do you think is the issue?
something related to commonjs and garbage collection
ok, thank you
also there seems to be
long running type of issue
because one production experiment i ran crashed after a day
or rather this infinite reconnection loop
import "discord.js";
import "lodash/omit.js";
import "lodash/findIndex.js";
Bun.gc(true);
this works
@serene shell you might be able to get djs to work if you import it (just plain statement like that) before any other code in your app. no promises
I'm having some inconsistent behavior of the app. Sometimes it loads i18next and segfaults and sometimes it just segfaults right away.
interesting
maybe the issue is from using namespaces?
Hey,
Any update?
I didn’t check if this still reproduces
I thought this was caused by the JSDOM thing
But scrolling up it appears unrelated
I’ll do that this evening. Today I have one of my last baccalauréat test 🤣
I’ll keep you informed if it works or not. But it could also be related to some other dependency. We’ll see!
i think the specific crash is fixed but i'm still seeing another similar issue in a project with many many dependencies
is the project public?
What version of Bun is running? 0.6.8 What platform is your computer? Linux 6.3.5-200.fc38.x86_64 x86_64 unknown What steps can reproduce the bug? git clone https://github.com/vuejs/vue-hackernews-...
It still segfaults
Is there any tool that I can use to know where the problem comes from?
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: 995
[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.8022308351865927; waiting 33092ms
[WS => Shard 0] Waiting for identify throttle
[WS => Shard 0] Identifying
shard id: 0
shard count: 1
intents: 1675
compression: none
[WS => Shard 0] Waiting for event ready for 15000ms
[1] 3321 segmentation fault (core dumped) bun run index.ts
After a little bit of investigation, I found out that calling the logger.info fun of winston causes a segfault.
The bot receives events now!
But when I uncomment my command loader, the bot crashes after [WS => Shard 0] Waiting for event ready for 15000ms
It's strange since all command related stuff shouldn't get executed at this time.
Problem found!
await readdir(commandsPath)
This line causes the crash. For context, the command path is : '/home/max/vscode_projects/suivix_6/src/discord/commands'
Hmmm looks like the problem is from the command itself
The segfaults comes from this import:
import { Err, Ok, Result } from 'oxide.ts';
In summary, here's what I found:
- Segaults while calling logger.info from winston
- Segaults importing oxide.ts (Probably related to Result)
Looks like it is no longer related to Discord.js
I hope it will help you!
sorry this is still happening
i have been working on it
and haven't been able to fix yet
No problem! Take your time 😄
@serene shell can you give it another try once canary builds
i think i fixed it
module.require was broken
require was fine
just module.require
@sharp wyvern
[0.02ms] ".env"
208 | __exportStar(require('discord-api-types/v10'), exports);
209 | __exportStar(require('@discordjs/builders'), exports);
210 | __exportStar(require('@discordjs/formatters'), exports);
211 | __exportStar(require('@discordjs/rest'), exports);
212 | __exportStar(require('@discordjs/util'), exports);
213 | __exportStar(require('@discordjs/ws'), exports);
^
TypeError: undefined is not an object (evaluating 'this.module.require = this.require')
at (eval) (/home/max/vscode_projects/suivix_6/node_modules/discord.js/src/index.js:213:46)
👀
@sharp wyvern with your lastest cjs commit, there's no longer any segfault.
Yeah I guess since on canary things work now.
Sadly discord.js events are now broken...
well that's nice
are they?
Yep, they don't fire
can you create a new directory
a brand new bot
and try client.on("ready", () => console.log(client.user.tag));
client.once works but client.on('interactionCreate') is broken
intents?
hmmmmmm
interesting
ill look at this when i get home
oh wait what once but on. ok thats odd
also let me confirm youre on linux or mac?
this shouldnt affect it but just in case
I'm on a wsl2 arch linux distro.
wait, as far as I know arch is not officially supported by wsl2
I'll test on arch linux and if not ill test wsl. the specific distro shouldnt change things for the most part
Sometimes, the program hangs at startup, I need to restart it.
though i imagine itll repro on mac
It was already like that before tho.
im on arch and discord.js worked flawelessly as far as i know
i have noticed long startup time
idk if it's just discord gateway taking long to accept it
If you want I can test it on another distro
it's on node.js too
can you try a complete new bot
like new folder
and stuff
with less code
if you have a real linux machine you can test it on that would be cool but no worries if not
Do you have a template or something 🤣 I'm lazy
I don't have one anymore sadly... it was too instable
relatable
Found it!
yeah
and if this doesnt repro the issue but your other project does can you either share the whole thing, or remove parts of it until it is as simple as possible
import { BaseInteraction, Client, Events, GatewayIntentBits } from "discord.js";
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildEmojisAndStickers,
],
});
client.on("ready", (client: Client<true>) => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on(Events.InteractionCreate, (interaction: BaseInteraction) => {
console.debug(interaction);
});
client.login(process.env.TOKEN);
It works, the event is successfully fired. BUT, looks like the console is being filled infinitely with the interaction data 🤣
Events.InteractionCreate?
you sure?
Yeah
try 'interactionCreate'
it is fired when a slash command was used

uh did you do any modifications to discord.js?
try this: rm -rf node_modules bun.lockb ~/.bun/install/cache
Nope, it's a brand new project, as you wanted.
and then bun add
bun still caches libraries
even if you did modficiations only in one project
interesting
Same enormous log size
ill look into this
I didn't modify anything
I can send you the logs if you want.
are you at your "desk" or something?
if you want sure
no ill look into it later
okay
i said that a few times but i havent written down any of what im gonna do lol
I trust you + I don't think there's any sensitive data x)
Good idea
when are you at your desk btw
Well it's a dev bot token anyway
"dev bot token anyway"
the reason why it-security sucks
you know people can steal your bot and then get you banned
cuz your account owns the bot
I know, it happened to me last year ;) They were after by discord bot earl ydev badge
just regenerate your token or censor it
Yeah I know, that's what I'm doing right now
What I'm saying is that this bot doesn't have any permissions & is in private mode.
@serene shell just demoed the code and i think it got worse
Uh
It doesn’t look good indeed
😅 yeah
One day we'll get there
I’d love to help you but I don’t really know how all the stuff that works behind the scene operates… I guess I’ll take a look. It looks really interesting!
Disallowed?
That means you haven't turned the intent on
Which you listed in clients options
oh
good catch
Still, the error is weird.
@pine rover i think I had the same error when discord.js was importing the wrong ws package
Hi, just to keep you updated,
Today I updated to the latest canary version and it segfaults right after launch, nothing works.
Alright, thanks & good luck!
Hey @sharp wyvern, looks like most of the issues are gone now! Amazing work!
Sadly discord.js appears to be still broken:
[WS => Shard 0] Preparing first heartbeat of the connection with a jitter of 0.2632006030628695; waiting 10857ms
[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] The gateway closed with an unexpected code 1002, attempting to resume.
[WS => Shard 0] Destroying shard
Reason: none
Code: 1002
Recover: Resume
[WS => Shard 0] Connection status during destroy
Needs closing: false
Ready state: 3
[WS => Shard 0] Preparing first heartbeat of the connection with a jitter of 0.7774889429330595; waiting 32071ms
[WS => Shard 0] Cancelled initial heartbeat due to #destroy being called
167 | }
168 |
169 | class AbortError extends Error {
170 | constructor() {
171 |
172 | super("The operation was aborted");
^
error: The operation was aborted
code: "ABORT_ERR"
at new AbortError (node:timers/promises:172:4)
at node:timers/promises:46:38
at /home/max/vscode_projects/suivix_6/node_modules/@discordjs/ws/dist/index.js:680:6
at destroy (/home/max/vscode_projects/suivix_6/node_modules/@discordjs/ws/dist/index.js:661:16)
at onClose (/home/max/vscode_projects/suivix_6/node_modules/@discordjs/ws/dist/index.js:1005:16)
at emit (node:child_process:146:4)
at ws:71:45
[WS => Shard 0] Connecting to wss://gateway.discord.gg?v=10&encoding=json
[WS => Shard 0] Waiting for event hello for 60000ms
InvalidStateError: The object is in an invalid state.
at ws:91
please give it another try after the canary builds
It keeps happening.
@sharp wyvern, on latest canary (039bbc68ad1f36ee7a734907ef657e90c7f504ac)
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: 1000
[WS => Shard 0] Connecting to wss://gateway.discord.gg?v=10&encoding=json
[WS => Shard 0] Waiting for event hello for 60000ms
[1] 2742 segmentation fault (core dumped) bun run index.ts
Please give it another try in about 20 minutes
Alright, thanks!
Should work now
Finally! Thank you for the amazing work!
I just hit another error, but the majority of my bot is working now 😁
247 | });
248 | const fetchedMembers = new Collection();
249 | let i = 0;
250 | const handler = (members, _, chunk) => {
251 | if (chunk.nonce !== nonce) return;
252 | timeout.refresh();
^
TypeError: undefined is not an object (evaluating 'timeout.refresh')
at handler (/home/max/vscode_projects/suivix_6/node_modules/discord.js/src/managers/GuildMemberManager.js:252:8)
at emit2 (node:child_process:158:17)
at /home/max/vscode_projects/suivix_6/node_modules/discord.js/src/client/websocket/handlers/GUILD_MEMBERS_CHUNK.js:33:2
at handlePacket (/home/max/vscode_projects/suivix_6/node_modules/discord.js/src/client/websocket/WebSocketManager.js:354:6)
at /home/max/vscode_projects/suivix_6/node_modules/discord.js/src/client/websocket/WebSocketManager.js:238:6
at emit (/home/max/vscode_projects/suivix_6/node_modules/@vladfrangu/async_event_emitter/dist/index.js:282:21)
at emit (/home/max/vscode_projects/suivix_6/node_modules/@vladfrangu/async_event_emitter/dist/index.js:282:21)
at /home/max/vscode_projects/suivix_6/node_modules/@discordjs/ws/dist/index.js:938:8
Our timeout object
Must be missing this function
I don’t remember that existing though
Do you want me to create an issue on github?
"This method refreshes the timer’s start time to the current time and reschedules the timer to its callback where the previously specified duration will be adjusted to the current time. This method helps in refreshing the timer without a new JS object."
yeah
The websocket hell is gone (I hope 🤣 )
Where is it in the repo? Maybe I can add the method and pull request it?
Look for “CallbackJob”
You’ll see some generated code and then zig and the zig one is the real implementation
There's already doRefresh
@sharp wyvern thanks for the fix! Everything works fine now :)
Ooops, just found an error in WATCH MODE:
🚨 error: ┏ Interaction has already been acknowledged. +131ms
🚨 error: ┃ [ 1] { [Error] originalLine: 32, originalColumn: 32 }
🚨 error: ┃ [ 2] { requestBody: {...}
Looks like it is related to discord.js @pine rover
it only happens in watch mode though
watch mode doesn't terminate websocket connections
(it would have to terminate server-side)
Is there a workaround?
How about hot reloading with —hot?
Same error
Have you tried process.on('exit')
Shouldn't there be same event but for watchUpdate?
I don't know
Well, for now you can use no demon or any alternative watcher. Dev team will probably review that
But don't close the channel yet
Let someone else from dev team see it
Discord.js is not working in —watch or —hot mode
Also, looks like bun doesn't really exit when I forcefully exit the process...
That wasn't happening before. (no watch mode)
I need to manually kill all of the processes...
I tried adding a client.destroy on process.on(exit,…) but it doesn’t work
there's not just exit
but SIGINT and etc.
signal events (=> https://nodejs.org/api/process.html#signal-events)
I added SIGINT and it's still not working
I also wanted to report that prisma 5 struggles to start on the latest canary/release:
✨ info: ┏ Starting a postgresql pool with 17 connections. +3s
✨ info: ┗ [1] { target: 'quaint::pooled' }
🐛 debug: ┏ prisma.query +20ms
🐛 debug: ┃ [1] { query: 'BEGIN',
🐛 debug: ┃ [2] params: '[]',
🐛 debug: ┃ [3] duration: 0,
🐛 debug: ┗ [4] target: 'quaint::connector::metrics' }
error: script "start" exited with code 11 (SIGSEGV)
It happens often, around 1 time out of 4.
Yeah, sure
Here you go: https://github.com/oven-sh/bun/issues/3647
Hey, any update on the prisma issue?
is that still happening?
i honestly am not sure how to reproduce it
we have tests that run prisma
can you paste a snippet that reproduces it?
I’ll try to make one but since it crashes randomly… that would be hard to do I guess
It crashes randomly at startup or during a query.
If you use --smol and Bun.gc(true) it’ll prob become more deterministic
Looks like I've stumbled on this same exact issue. Yeah Prisma queries running just can sometimes randomly initiate a segfault. I have like hundreds of queries running in parallel at the same time
I think we need a test for exactly that
@glad ether can you take a look at updating the prisma integration tests to run like 500 queries and then await Promise.all() them?
Earlier today I fixed an async fs bug doing a similar thing
just to add on information it does not seem to happen when i test it locally using my machine. Im using WSL Ubuntu-22.04 with the latest Bun v0.7.3
But when I deploy this to the cloud on Railway.app using Docker (it builds successfully) and let the hundreds of queries run in parallel, it has like a 98% chance of segfault. In rare occassions it does not, but most of the time it does
Hey, I wanted to report that bun's debugger web window crashes on my computer right when I open it.
Firefox displays the following error:
TypeError: this._listItemNode.scrollIntoViewIfNeeded is not a function (at manifest-ed2a1ca9ee54e08a.js:51:699020)
onreveal @ manifest-ed2a1ca9ee54e08a.js:51:699020
reveal @ manifest-ed2a1ca9ee54e08a.js:51:646915
revealAndSelect @ manifest-ed2a1ca9ee54e08a.js:51:647578
_revealAndSelectRepresentedObject @ manifest-ed2a1ca9ee54e08a.js:51:759660
_contentBrowserCurrentContentViewDidChange @ manifest-ed2a1ca9ee54e08a.js:51:759384
$ @ manifest-ed2a1ca9ee54e08a.js:37:10910
dispatchEventToListeners @ manifest-ed2a1ca9ee54e08a.js:37:10950
_currentContentViewDidChange @ manifest-ed2a1ca9ee54e08a.js:51:443815
$ @ manifest-ed2a1ca9ee54e08a.js:37:10910
dispatchEventToListeners @ manifest-ed2a1ca9ee54e08a.js:37:10950
showBackForwardEntryForIndex @ manifest-ed2a1ca9ee54e08a.js:51:1231708
showContentView @ manifest-ed2a1ca9ee54e08a.js:51:1231203
showContentView @ manifest-ed2a1ca9ee54e08a.js:51:436455
_showTimelineViewForType @ manifest-ed2a1ca9ee54e08a.js:51:888271
restoreFromCookie @ manifest-ed2a1ca9ee54e08a.js:51:882117
_recordingSelected @ manifest-ed2a1ca9ee54e08a.js:51:887092
_recordingLoaded @ manifest-ed2a1ca9ee54e08a.js:51:887138
$ @ manifest-ed2a1ca9ee54e08a.js:37:10910
dispatchEventToListeners @ manifest-ed2a1ca9ee54e08a.js:37:10950
_loadNewRecording @ manifest-ed2a1ca9ee54e08a.js:85:32059
reset @ manifest-ed2a1ca9ee54e08a.js:85:12828
enable @ manifest-ed2a1ca9ee54e08a.js:85:14016
Z @ manifest-ed2a1ca9ee54e08a.js:51:873255
WI._createTabContentViewForType @ manifest-ed2a1ca9ee54e08a.js:85:91797
WI.contentLoaded @ manifest-ed2a1ca9ee54e08a.js:85:88121
? @
It instantly exits on Brave, doesn't work on Edge and displays an error on Firefox.
"instantly exits" means it didn't wait for the inspector to start
can you try --inspect-brk
thefirefox error is a missing polyfill
If you refresh
it should no longer instantly exit
and instead go black and white
I'm not sure why it's instantly disconnecting
but if bun was segfaulting it would do that
It instantly exits on Brave.
Alright, I don't know why but it's now working on Firefox.
And on brave.
Problem fixed...
Ok maybe not. Now I can see the interface but no data.
On firefox it works. (not all the time)
because i pushed a polyfill
for the missing function
in firefox
Not at all
It's working on firefox rn, but not in brave.
It works now. I guess the polyfill fixed it.
Nope, it doesn't.
I have projects that run in localhost with https and brave doesn't block it.
Sorry it’s
mixed content due to the website debug.bun.sh being https but the websocket being http/ws and not wss
Hey, I'm getting ERROR [unhandledRejection] Cannot redefine property: __internal__deprecationWarning while trying to run an existing nuxt 3 project.
No idea how to resolve that...
cc @pine rover incase anything comes to mind
interesting
ERROR [unhandledRejection] Cannot redefine property: __internal__deprecationWarning 2:48:51 PM
at Function.defineProperty (<anonymous>)
at Object.<anonymous> (node_modules/@babel/types/lib/index.js:66:8)
at Module._compile (node:internal/modules/cjs/loader:1241:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
at Module.load (node:internal/modules/cjs/loader:1091:32)
at Module._load (node:internal/modules/cjs/loader:938:12)
at Module.require (node:internal/modules/cjs/loader:1115:19)
at require (node:internal/modules/helpers:130:18)
at Object.<anonymous> (node_modules/@babel/types/lib/builders/validateNode.js:8:9)
at Module._compile (node:internal/modules/cjs/loader:1241:14)
It works on node v20.4.0 but not in node v20.6.0 and with bun
okay this is too late now but 5.10.0-25-cloud-amd64
are you still runing into it?
im trying to test it now with the latest build so ill get back later
hmm turns out it is with v0.8.1
hey it worked