I am currently working on a bot discord developed with Node.js.
I have correctly split my project by separating the different parts in associated folders.
My project folder
I have a problem though, because I need to use the client I created in the index in my guildMemberAdd.js and other files, but I don’t know how to pass it correctly.
index.js
I tried to put my client variable in global variable but it creates an error.
index.js
cmd
2
Answers
you can add this code to your index.js file to export the Client variable
And then, you need to call the variable from the files that you want to use the variable in them using a destructor like this:
You don’t need to pass down the instantiated
client
to every file as it’s available from the callback functions.For example, if you want to get the
client
in theguildMemberAdd
event, you can grab it from theGuildMember
(which is the first parameter):It will work with other events too. Some examples below:
If you still want to pass the
client
, you can pass it as the last parameter:This way
client
is the last argument:However, this way you will have to include every argument. For example, in
roleUpdate
: