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 toolkit10mo 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 User10mo ago
Message Not Public
Sign In & Join Server To View
GuikiPT
GuikiPT10mo ago
you can look at the example of handler
treble/luna
treble/luna10mo ago
do pass in the client in your ready event though
GuikiPT
GuikiPT10mo ago
how can you pass it to events and commands let me grab the link
GuikiPT
GuikiPT10mo ago
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
d.js docs
d.js docs10mo ago
guide Creating Your Bot: Event handling read more
treble/luna
treble/luna10mo ago
that exists too
GuikiPT
GuikiPT10mo ago
quit new for these bot xD
treble/luna
treble/luna10mo 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
Bossk10mo 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
GuikiPT10mo ago
is not djs structure at all
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GuikiPT
GuikiPT10mo 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
Bossk10mo 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
GuikiPT10mo ago
hum you can, use interaction.client to acess the client by interaction
treble/luna
treble/luna10mo ago
Ready event emits with a client param
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
GuikiPT
GuikiPT10mo ago
depending it they are using in the main file and these main file including yes
Bossk
Bossk10mo ago
So I would pass that client param to my second file?
treble/luna
treble/luna10mo ago
yes
Bossk
Bossk10mo 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/luna10mo ago
pass it in in your callback of your ready event (client) =>
Bossk
Bossk10mo ago
But how would I do the ready event in my other file since I can't access client
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs10mo 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
Bossk10mo 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/luna10mo ago
not really just create a file per event
Bossk
Bossk10mo 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