#Command and Event Handler
1 messages · Page 1 of 1 (latest)
const fs = require('fs');
// Require the necessary discord.js classes
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('../config.json');
// Create a new client instance
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS],
partials: ['MESSAGE', 'USER', 'CHANNEL', 'REACTION'],
});
// Commands handler
client.commands = new Collection();
const commandFiles = fs.readdirSync('./src/commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(./commands/${file});
client.commands.set(command.data.name, command);
}
// Event handler
const eventFiles = fs.readdirSync('./src/events').filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const event = require(./events/${file});
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
}
else {
client.on(event.name, (...args) => event.execute(...args));
}
}
/*
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
}
catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}); */
// Login to Discord with your client's token
client.login(token);
wdym by "both work but only one at the time."
this way, events work, but when when i try a command, nothing happen
but on vscode:
show your ping command real quick ?
fs.readdirSync('./src/commands')
and
require(./commands/${file})
you are not in the same folder
^
yes, because fs works the path from the package location path
i get this error if i put the same path on both
it works with this project
thats why im so confused
tbh i cant help much because im not using fs but i can see the only difference between you and me is
you do : ```js
for (const file of commandFiles) {const command = require(./commands/${file});}`
and i do :```js
for (const commandFile of commands) {
const command = require(commandFile)}```
how do you do those neat boxes btw?
3 backtick
how do you reach the file without ./commands/${file});
?
js ./commands/${file});
${file}
for me is because im using glob package , its not the same