Search code examples
javascriptdiscord.jsargs

Use client in event with event handler and ...args | Discord.js@13


I would like to use the client in the event, but I don't know how to hand it over without removing ... args

the event handler:

const client = new Client({ intents: [Intents.FLAGS.GUILDS] });



const eventFiles = fs.readdirSync('./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));
    }
}

and the event modul:

module.exports = {
    name: 'interactionCreate',
    async execute(client,interaction) {
       
   //... need client from code before here

    },
};

Solution

  • Simply add client to the args!

    if (event.once) {
      client.once(event.name, (...args) => event.execute(client, ...args));
    } else {
      client.on(event.name, (...args) => event.execute(client, ...args));
    }