#What’s your event handler
1 messages · Page 1 of 1 (latest)
// Imports
const discord = require('discord.js');
const fileSystem = require('fs');
const { Client, Partials, Collection, GatewayIntentBits } = require('discord.js');
const config = require('./config.json');
const events = require("events");
const client = new Client({
intents: [
GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessageReactions,
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction]
});
// Setting up Client
client.discord = discord;
client.config = config;
client.commands = new Collection();
// Reading Directory
const commandFiles = fileSystem.readdirSync('./commands').filter(file => file.endsWith('.js'));
const eventFiles = fileSystem.readdirSync('./events').filter(file => file.endsWith('.js'));
// Looping Event & Command Files
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
};
for (const file of eventFiles) {
const event = require(`./events/${file}`);
client.on(event.name, (...args) => event.execute(client));
};
// Command Handler
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction, client, config, events);
} catch (error) {
console.error(error);
return interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
});
};
});
// Discord Login
client.login(require('./config.json').token);```
You only pass client to your events and none of the args
client.on(event.name, (...args) => event.execute(client));
Spotted that quicker then I did 😂
i don't even know where to look lol imma be honest my mate coded this for me im just tryna add commands and events lol
oh found
node:events:491
throw er; // Unhandled 'error' event
^
TypeError: Cannot read properties of undefined (reading 'guild')
why am i getting this now ffs
Events and config would be undefined
So you added more things that arent the event parameters
args still arent being passed
monbrey would
execute(client, …args) work?
I don’t use this style so kinda confused lol
yeah
where do i added events and config etc then?
for (const file of eventFiles) {
const event = require(`./events/${file}`);
client.on(event.name, () => event.execute(client));
};
i don't know!!!
You've now completely removed ...args
I basically gave you the answer 😭
im tryna make it so when i react to a message it adds a role and im failing lol
Yes please read 😭
but i did this and then you told me not too?
Nobody told you to do that
client.on(event.name, (…args) => event.execute(…args))
This is just what the guide says
Can’t type
Creating Your Bot: Event handling