how do you get access to your instantiated `discordjs.Client` from imported files?

I can't do export client and import my main file, because that would create a circular dependency. how is it usually done? should i just make client global / attach it to process?
9 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Syjalo
Syjalo2y ago
Usually you always have an instance of the class in each file because you provide it or d.js structures. eg message.clinet, interaction.client, etc
skeddles
skeddlesOP2y ago
but it's not the same instance that i create / login with in my main file bot.js (my main file)
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once('ready', async c => {
console.log(`Logged in as ${c.user.tag}`);
await loadCommands();
});

client.login(process.env.TOKEN);
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once('ready', async c => {
console.log(`Logged in as ${c.user.tag}`);
await loadCommands();
});

client.login(process.env.TOKEN);
MrMythical
MrMythical2y ago
But it is Well the loadCommands is kind of weird, what does it do? It doesn't have any arguments
skeddles
skeddlesOP2y ago
just trying to load all my commands from separate files load-commands.js:
async function loadCommands(interaction) {
let commandsList = [];
let commandFileList = glob.sync('./commands/*.js');

for (let commandPath of commandFileList) {
let command = await import('./'+commandPath);
commandsList.push(command.config);
//COMMANDS[command.config.name] = command.execute;

console.log('loaded command:', '/'+command.config.name);
}
console.log('all commands loaded')

rest.put(DiscordRestRoutes.applicationGuildCommands(client.user.id,MonsterGameConfig.get('serverGuildId')), {body: commandsList} )
.then(e => console.log('loaded ',commandsList.length,' commands'))
.catch(err=> console.error('failed to load commands:',err))
};
async function loadCommands(interaction) {
let commandsList = [];
let commandFileList = glob.sync('./commands/*.js');

for (let commandPath of commandFileList) {
let command = await import('./'+commandPath);
commandsList.push(command.config);
//COMMANDS[command.config.name] = command.execute;

console.log('loaded command:', '/'+command.config.name);
}
console.log('all commands loaded')

rest.put(DiscordRestRoutes.applicationGuildCommands(client.user.id,MonsterGameConfig.get('serverGuildId')), {body: commandsList} )
.then(e => console.log('loaded ',commandsList.length,' commands'))
.catch(err=> console.error('failed to load commands:',err))
};
Syjalo
Syjalo2y ago
Well, you're run your process in main file and you're creating an instance in that file. Therefore you can provide it everywhere you want.
skeddles
skeddlesOP2y ago
i dont understand
Syjalo
Syjalo2y ago
You can provide your client to your loadCommands function parameter
skeddles
skeddlesOP2y ago
i guess that would work. seems clunky though think i found a better solution, i moved everything from my main file into client.js that exports the client, now my main file is only import commands from './commands.js';, and the commands file can import the client
Want results from more Discord servers?
Add your server