Skip to content
Advertisement

TypeError: path.join is not a function (got the error in my handleEvents.js file)

I am trying to make a discord bot and i got this error in my handleEvents.js file

The code:

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

module.exports = (client) => {
    client.handleEvents = async (eventFiles, path) => {
        for (const file of eventFiles) {
            const filePath = path.join(`${path}/${file}`);
            const event = require(`../events/${file}`);
            if (event.once) {
                client.once(event.name, (...args) => event.execute(...args, client));
            } else {
                client.on(event.name, (...args) => event.execute(...args, client));
            }
        }
}
}

Advertisement

Answer

You are missing a path module import

const path = require('path');

and by using the name path as property of your callback, you would be overwriting the path module.

const { Client, Intents } = require('discord.js');
const path = require('path');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

module.exports = (client) => {
    client.handleEvents = async (eventFiles, pathString) => {
        for (const file of eventFiles) {
            const filePath = path.join(`${pathString}/${file}`);
            const event = require(`../events/${file}`);
            if (event.once) {
                client.once(event.name, (...args) => event.execute(...args, client));
            } else {
                client.on(event.name, (...args) => event.execute(...args, client));
            }
        }
    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement