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.
// Access the content and attachments of the collected message
const attachments = collected.first().attachments;

await attachments.forEach(attachment => {
const attachmentURL = attachment.url;

const channelId = "1121329291070808084";
const channel = client.channels.cache.get(channelId);

const verifyEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Verification",
})
.setDescription(`The user <@${interaction.user.id}> has requested verification.`)
.setImage(attachmentURL)

// Send the attachment URL to the specific channel
channel.send({
embeds: [verifyEmbed]
})
// Access the content and attachments of the collected message
const attachments = collected.first().attachments;

await attachments.forEach(attachment => {
const attachmentURL = attachment.url;

const channelId = "1121329291070808084";
const channel = client.channels.cache.get(channelId);

const verifyEmbed = new EmbedBuilder()
.setColor(0x2b2d31)
.setAuthor({
name: "Verification",
})
.setDescription(`The user <@${interaction.user.id}> has requested verification.`)
.setImage(attachmentURL)

// Send the attachment URL to the specific channel
channel.send({
embeds: [verifyEmbed]
})
Here's the mentioned snippet. However... The entire file has this appearance. Verify.js
const { client } = require("../index");

// Code to verify user
const { client } = require("../index");

// Code to verify user
Index.js
// Normal bot creation code

// eof
module.exports = { client };
// Normal bot creation code

// eof
module.exports = { client };
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
d.js toolkit
d.js toolkit6mo 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! - Marked as resolved by OP
treble/luna
treble/luna6mo ago
Most 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
Kiɾʌ Kenjiɾø
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
treble/luna
treble/luna6mo ago
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
Kiɾʌ Kenjiɾø
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 kek wanna just make sure i do it the right way, try to learn the standard ish ^^
// Event listener for the "ready" event
client.on("ready", async () => {
const readyMessage = `Logged in as ${client.user.tag}`;
await client.application.commands.set(Array.from(client.commands.values()));
handlers.logHandler.log(readyMessage, 1);
});
// Event listener for the "ready" event
client.on("ready", async () => {
const readyMessage = `Logged in as ${client.user.tag}`;
await client.application.commands.set(Array.from(client.commands.values()));
handlers.logHandler.log(readyMessage, 1);
});
As thats what i have right now as my ready event
treble/luna
treble/luna6mo ago
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
Kiɾʌ Kenjiɾø
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 novamo9Love try that way and mess around with it for a bit :D