Attempting to remake my bot the "Correct" way

I made my entire bot in one file, and I'm looking to recreate it and section off to folders in a layout like this.. (Image below) I figured out how to export & import my commands. However, I'm getting issues where it's trying to parse the .js files themselves before loading them into my bot.js? Is anyone aware how I can get around this "client is not defined" issue? My commands look like this.
// commands/ping.js
export const ping = {
name: 'ping',
description: 'Replies with Pong!',
execute(interaction) {
interaction.reply({ content: 'Pong! 🏓', ephemeral: true });
},
};
// commands/ping.js
export const ping = {
name: 'ping',
description: 'Replies with Pong!',
execute(interaction) {
interaction.reply({ content: 'Pong! 🏓', ephemeral: true });
},
};
& they are imported like this
import { ping } from './commands/ping.js';
const commands = new Collection();
commands.set(ping.name, ping);
// Then do client.on blah blah blah
import { ping } from './commands/ping.js';
const commands = new Collection();
commands.set(ping.name, ping);
// Then do client.on blah blah blah
No description
No description
10 Replies
d.js toolkit
d.js toolkit3mo 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!
Mandi
MandiOP3mo ago
[email protected] v20.14.0 Please ping me if anyone knows
TÆMBØ
TÆMBØ3mo ago
Take a look at your logging util. You don't seem to have access to your client in that function, so the easiest way is to pass your client as another parameter of that function
Mandi
MandiOP3mo ago
// utils/logging.js

export function log(message) {
console.log(`[${new Date().toLocaleString()}] ${message}`);

const embed = {
color: 0xCC5490,
description: `**${message}**`,
timestamp: new Date(),
footer: {
text: "REDACTED",
},
}
const logid = 'REDACTED';
client.channels.cache.get(logid).send({embeds: [embed]});
}
// utils/logging.js

export function log(message) {
console.log(`[${new Date().toLocaleString()}] ${message}`);

const embed = {
color: 0xCC5490,
description: `**${message}**`,
timestamp: new Date(),
footer: {
text: "REDACTED",
},
}
const logid = 'REDACTED';
client.channels.cache.get(logid).send({embeds: [embed]});
}
could log not just retrieve that from bot.js? im not informed on multi-file anything lol, for all my projects i end up working with 1 file for the most part
TÆMBØ
TÆMBØ3mo ago
From an import? That's not ideal, you'd very likely be creating circular dependencies by importing your client instead of passing it as a parameter instead
Mandi
MandiOP3mo ago
Couldn't I just throw
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.MessageContent] });
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.MessageContent] });
Inside of the logging.js at the top?
client.channels.cache.get(logid).send({embeds: [embed]});
^
TypeError: Cannot read properties of undefined (reading 'send')
client.channels.cache.get(logid).send({embeds: [embed]});
^
TypeError: Cannot read properties of undefined (reading 'send')
TÆMBØ
TÆMBØ3mo ago
No, because that's not your logged-in client, that's a separate client you're creating
Mandi
MandiOP3mo ago
Oh
d.js docs
d.js docs3mo ago
:guide: Getting Started: Introduction read more
PPX005
PPX0053mo ago
@Mandi I would take a look at this guide Especially at the command and event handling It teaches what you’re looking for
Want results from more Discord servers?
Add your server