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 times
29 Replies
d.js toolkit
d.js toolkit16mo ago
- 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
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
GuikiPT
GuikiPT16mo ago
you can look at the example of handler
treble/luna
treble/luna16mo ago
do pass in the client in your ready event though
GuikiPT
GuikiPT16mo ago
how can you pass it to events and commands let me grab the link
GuikiPT
GuikiPT16mo ago
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
d.js docs
d.js docs16mo ago
guide Creating Your Bot: Event handling read more
treble/luna
treble/luna16mo ago
that exists too
GuikiPT
GuikiPT16mo ago
quit new for these bot xD
treble/luna
treble/luna16mo ago
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
Bossk
BosskOP16mo ago
I realize that I need to read up more on the library, as I don't understand what you mean by "djs structure" xD
GuikiPT
GuikiPT16mo ago
is not djs structure at all
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
GuikiPT
GuikiPT16mo ago
Yeah i will explain a little: what i use is a handler like these on:
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
you you understand a little js, on the event.execute(...args) you can pass any args you want including the client
Bossk
BosskOP16mo ago
In my different file I want to do this:
client.once(Events.ClientReady, () => {
client.channels.cache.get("myChannelID").send({ content: 'Test', components: [buyNowRow] })
});

client.on(Events.InteractionCreate, async interaction => {
if (interaction.customId === 'buynowbtn') {
await interaction.reply({
content: `THIS IS A TEST! `,
components: [testRow],
ephemeral: true
});
}
})
client.once(Events.ClientReady, () => {
client.channels.cache.get("myChannelID").send({ content: 'Test', components: [buyNowRow] })
});

client.on(Events.InteractionCreate, async interaction => {
if (interaction.customId === 'buynowbtn') {
await interaction.reply({
content: `THIS IS A TEST! `,
components: [testRow],
ephemeral: true
});
}
})
GuikiPT
GuikiPT16mo ago
hum you can, use interaction.client to acess the client by interaction
treble/luna
treble/luna16mo ago
Ready event emits with a client param
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
GuikiPT
GuikiPT16mo ago
depending it they are using in the main file and these main file including yes
Bossk
BosskOP16mo ago
So I would pass that client param to my second file?
treble/luna
treble/luna16mo ago
yes
Bossk
BosskOP16mo ago
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 ... : /
treble/luna
treble/luna16mo ago
pass it in in your callback of your ready event (client) =>
Bossk
BosskOP16mo ago
But how would I do the ready event in my other file since I can't access client
Unknown User
Unknown User16mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs16mo ago
guide 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
Bossk
BosskOP16mo ago
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 execute
treble/luna
treble/luna16mo ago
not really just create a file per event
Bossk
BosskOP16mo ago
👍 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: trueFeelsBadMan
Want results from more Discord servers?
Add your server