Hello. I have a problem with a .name in my bot’s command handler code

const { readdirSync } = require(“fs”)

module.exports = (bot) => {
const load = dirs => {
const commands = readdirSync(./commands/${dirs}/).filter(d => d.endsWith(’.js’));
for (let file of commands) {
let pull = require(../commands/${dirs}/${file});
bot.commands.set(pull.config.name, pull);
if (pull.config.aliases) pull.config.aliases.forEach(a => bot.aliases.set(a, pull.config.name));
};
};
[“sve komande”].forEach(x => load(x));
};

This is the error

/home/runner/150-Commands-Bot-1/handler/command.js:8
bot.commands.set(pull.config.name, pull);
^

TypeError: Cannot read property ‘name’ of undefined
at load (/home/runner/150-Commands-Bot-1/handler/command.js:8:42)
at /home/runner/150-Commands-Bot-1/handler/command.js:12:38
at Array.forEach ()
at module.exports (/home/runner/150-Commands-Bot-1/handler/command.js:12:25)
at /home/runner/150-Commands-Bot-1/index.js:16:71
at Array.forEach ()
at Object. (/home/runner/150-Commands-Bot-1/index.js:16:33)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)

You clearly got that code from somewhere right? I assume they didn’t present it exactly like you have it written there. My guess is that you have mistakenly written pull.config.name rather than pull.name.

That being said I typically warn people away from this automated loading of .js files by folder convention. It can work but it doesn’t turn out to be any faster, clearer or easier to use as far as I can see.

Hi @HereIsOlly, welcome to the community!

I found this tutorial-bot repo that you seem to share some code with. Each file in your commands directory needs to be structured like this:

const { MessageEmbed } = require('discord.js');

module.exports = {
    config: {
        name: `info`,
        aliases: [`information`, `info`]
    },
    run: async (bot, message, args) => {
        // Do stuff
    }
}

(Source: https://github.com/BradyBots/tutorial-bot/blob/master/commands/user/info.js)

Since your error says that config is undefined, I wonder whether you forgot to use this structure?

Anyway hopefully the repo above is helpful for some more pointers.

Let us know how it goes!

1 Like