#What’s your event handler

1 messages · Page 1 of 1 (latest)

raven goblet
#
// 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);```
slim charm
#

You only pass client to your events and none of the args
client.on(event.name, (...args) => event.execute(client));

rancid aurora
#

Spotted that quicker then I did 😂

raven goblet
#

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

rancid aurora
#

Is that the same error from before

#

What did you change in your event handler

raven goblet
#

so i added this

rancid aurora
#

Events and config would be undefined

slim charm
#

So you added more things that arent the event parameters

#

args still arent being passed

rancid aurora
#

monbrey would
execute(client, …args) work?

#

I don’t use this style so kinda confused lol

slim charm
#

yeah

rancid aurora
#

An alright

#

Ah*

raven goblet
#
for (const file of eventFiles) {
  const event = require(`./events/${file}`);
    client.on(event.name, () => event.execute(client));
};
slim charm
#

...nowhere? I dont know what theyre supposed to be

#

...what are yoiu doing, honestly

raven goblet
#

i don't know!!!

slim charm
#

You've now completely removed ...args

rancid aurora
raven goblet
#

im tryna make it so when i react to a message it adds a role and im failing lol

nimble gardenBOT
rancid aurora
#

Yes please read 😭

raven goblet
slim charm
#

Nobody told you to do that

rancid aurora
#

client.on(event.name, (…args) => event.execute(…args))

This is just what the guide says

#

Can’t type