0

I am getting an error and I have tried everything but nothing works.

TypeError: Cannot read properties of undefined (reading 'name')
    at Object.<anonymous> (/home/runner/NauticalAgonizingRegisters/index.js:12:35)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)

Code at index.js:12:35

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.data.name, command);
}
2
  • 1
    Hi, welcome to Stack Overflow! It looks like command.data is undefined. Have a look at the files in the commands folder — are they all exporting a data object? Commented Apr 3, 2022 at 10:22
  • Hello, I only have 3 commands and only one one then are exporting a data object. The other two are not application commands. Commented Apr 3, 2022 at 18:52

2 Answers 2

1

As you said in your comment, not all of your commands export a data object because not all of them are application commands. This means that command.data would be undefined for these commands.

You can either

  • get the name of the non-application commands some other way. For example:

    client.commands.set(command.data?.name ?? command.name, command)
    
    // commands/nonApplicationCommand.js
    module.exports = {
      name: 'nonApplicationCommand'
      // ...
    }
    
  • separate your commands into separate folders for each type (this is what I would do).

    const {readdir} = require('node:fs/promises')
    
    const applicationCommandFiles = await readdir('./commands/application')
    for (const file of applicationCommandFiles) {
      const command = require(`./commands/application/${file}`);
      client.applicationCommands.set(command.data.name, command);
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Hello, I tried what you said but it didn't work. Don't worry though I fixed it.
0

Looking at the comments, it seems like you don't have a command.data.name in most of your command files. What you can do is this:

  for (const file of commandFiles) {
  const commandName = file.split(".")[0]
  const command = require(`./commands/${file}`);
  client.commands.set(commandName, command);
  }

And/or declaring a command.name in each of your command file like this:

module.exports.name = "foobar";

You can use command.name instead of commandName. It doesn't matter if those other 2 files aren't application commands, as long as they have an execute (or run) function and a declaration of an exportable name variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.