Use `client` in multiple files?
Hi! I'm pretty new to d.js but have a question regarding whats common practice. I define my
client
in index.js
, but now I need to interact with the client
from a different file. What's the best way to do this? I assume I shouldn't/can't define client
two times29 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
you can look at the example of handler
do pass in the client in your ready event though
how can you pass it to events and commands
let me grab the link
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
that exists too
quit new for these bot xD
regardless, if you do not have access to a djs structure, do what godder says but do it in your ready event as the client in your index might not be logged in
I realize that I need to read up more on the library, as I don't understand what you mean by "djs structure" xD
is not djs structure at all
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
Yeah
i will explain a little:
what i use is a handler like these on:
you you understand a little js, on the
event.execute(...args)
you can pass any args you want
including the clientIn my different file I want to do this:
hum
you can, use
interaction.client
to acess the client
by interaction
Ready event emits with a client param
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
depending it they are using in the main file and these main file including yes
So I would pass that client param to my second file?
yes
Thanks a ton for your help and everyone elses. Do you know of any examples of how above is done? I'm petty new, sorry ... : /
pass it in in your callback of your ready event
(client) =>
But how would I do the ready event in my other file since I can't access
client
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
Creating Your Bot: Event handling
Currently, the event listeners are in the index.js file. Client#event:readyopen in new window emits once when the Client becomes ready for use, and Client#event:interactionCreateopen in new window emits whenever an interaction is received. Moving the event listener code into individual files is a...
read more
Ahh!
I added event handling.
My question now is, can I have 2 events in one file? I.e 2 different
module.exports
?
Doesn't seem to work for me. I tried having two events in one file but only one of them executenot really
just create a file per event
👍
Now I'm having a really weird issue,
So I made file specifially to listen for a button click. This worked nicely when I had all the code in
index.js
Now it works ONCE, and then the interaction fails when clicked. Any obvious reason for this?
MY BAD!!!
It had once: true