#Command and Event Handler

1 messages · Page 1 of 1 (latest)

fluid sundial
#

Hello. Im trying to have both event handler and command handler. both work but only one at the time. Did someone had the same issue? (new to programming)

#

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);

paper barn
#

wdym by "both work but only one at the time."

fluid sundial
#

this way, events work, but when when i try a command, nothing happen

#

but on vscode:

paper barn
#

show your ping command real quick ?

fluid sundial
#

if i hardcode (the commented block), the event handler on index.js, it works

earnest cave
#

fs.readdirSync('./src/commands')
and
require(./commands/${file})

#

you are not in the same folder

paper barn
#

^

fluid sundial
#

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

paper barn
#

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)}```
fluid sundial
#

how do you do those neat boxes btw?

paper barn
#

3 backtick

fluid sundial
#

how do you reach the file without ./commands/${file});

#

?

#

js ./commands/${file});

#

${file}

paper barn
#

for me is because im using glob package , its not the same