Support with using client in separate files.
I'm having a bit of a struggle with my project and could use some assistance. What I've gotten into the habit of doing is exporting the client from my index.js as a module and then importing it into another file. I know it's a really bad habit. For instance, in my verify.js, I need to use the client to send a message to a specific channel.
Here's the mentioned snippet. However...
The entire file has this appearance.
Verify.js
Index.js
I know I shouldn't be doing it like this, but I'm not sure how to use the client in other files. I've attached my index.js to show you how I'm loading the other files into the index job/runtime thing. I've also linked an image of my entire folder structure.
7 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!
- ✅
Marked as resolved by OPMost djs structs have a client property
If you dont have access to it, call a function in your ready event where you initialize the client
Yeah, I don't think i set it up to have it. I didn't really think of it when i first started it. And also thank you so much for helping me recently pff. <3
So, i just wanna make sure i understand it correctly. In the index.js
I'd have a function say like
createAlternativeClient()
client = client(token)
and then i'd export that function as a module. import it as a module in well whatever file i need to refrence the lcient in and then rather than using one singular client (interaction? i can't figure out what the right word is. It's like the job/process that you've created)
At the moment I thought you could only use that client process you originally made in the index, I didn’t really clock it that you can make more
you cant make more
But the ready event fires with a fully ready client
so you can call a function in your ready event where you pass in your client, and then access it in the file you want
is there an example you can send me or a gist where someone has done this <3
sorry luna, im trying to understand but, it does confuse me a bit haha xD
Because none of my functions are actually in my index.js / ready event because im kinda throwing them around like modules
thanks for helping me though, i aint the smartest bean wanna just make sure i do it the right way, try to learn the standard ish ^^
As thats what i have right now as my ready event
you'd just make the file you need the client in export a function that takes a client as parameter and store it, import that in your ready event and call it with your client
Ohh, i understand now!
What i've been trying to do is to the flip opposite!!
I was trying to export the client to the file! okay
thanks luna, I really appreciate you helping me try that way and mess around with it for a bit :D