Best practices for using Discord.js throughout different components

I'm working on breaking apart the single multiple thousand line .js source file I've been playing with for a while, I've seen some different advice (people saying to just make a new Discord client for each file, people exporting functions to interact with it, or exporting the client directly) and I'm not sure if my usecase is the same. Right now, I'm just trying to get my server component separated (not related to D.js) but there are a few important events I like to log to a Discord channel (so I don't have to open any additional things to monitor it). The only thing I would like to export for my bot file is the channels I like to long to so I can call .send() on them. I'm running into Warning: Accessing non-existent property 'wgConsoleChannel' of module exports inside circular dependency when loading my bot up, and Cannot read properties of undefined (reading 'send') when calling wgConsoleChannel.send() Should I export my bot, create some kind of interface for this kind of thing, or create a new Discord.Client in every file I need to do something like this? I'd prefer the lightest weight possible option (within reason)
// bot.js
let wgConsoleChannel;

const bot = new Client({
autoReconnect: true,
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_WEBHOOKS, Intents.FLAGS.GUILD_INVITES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_SCHEDULED_EVENTS]
});

bot.on('ready', async () =>
{
wgConsoleChannel = bot.channels.cache.get('547950347814174720')
wgInGameChannel = bot.channels.cache.get('998789095880925194')
wgKillsChannel = bot.channels.cache.get('999125728786395166')
tripInfoChannel = bot.channels.cache.get('1004134753437503580')
});

exports = { wgConsoleChannel, wgInGameChannel, wgKillsChannel, tripInfoChannel }
// bot.js
let wgConsoleChannel;

const bot = new Client({
autoReconnect: true,
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS,
Intents.FLAGS.GUILD_WEBHOOKS, Intents.FLAGS.GUILD_INVITES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_SCHEDULED_EVENTS]
});

bot.on('ready', async () =>
{
wgConsoleChannel = bot.channels.cache.get('547950347814174720')
wgInGameChannel = bot.channels.cache.get('998789095880925194')
wgKillsChannel = bot.channels.cache.get('999125728786395166')
tripInfoChannel = bot.channels.cache.get('1004134753437503580')
});

exports = { wgConsoleChannel, wgInGameChannel, wgKillsChannel, tripInfoChannel }
// server.js
const {wgConsoleChannel, wgInGameChannel, wgKillsChannel, tripInfoChannel } = require('../bot.js')
// server.js
const {wgConsoleChannel, wgInGameChannel, wgKillsChannel, tripInfoChannel } = require('../bot.js')
Thanks for reading :)
2 Replies
d.js toolkit
d.js toolkit7d 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!
Benluka.dev
Benluka.dev7d ago
the warning is because the channels are undefined when exporting ready event isnt fired yet), you can actually export client then get the chanel in that respective file when needed to avoid bugs