Search code examples
discorddiscord.js

Discord.JS bot, TypeError: Cannot read properties of undefined (reading 'name')


client.commands.set(command.data.name, command); ^

TypeError: Cannot read properties of undefined (reading 'name')

Node.js v18.13.0

const commands = [];
client.commands = new Collection();
client.commands.set("help", require("./commands/help"));
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
  .readdirSync(commandsPath)
  .filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
  const filePath = path.join(commandsPath, file);
  const command = require(filePath);

  client.commands.set(command.data.name, command);
  commands.push(command.data.toJSON());
}

I have tried google, but i didnt found answer.


Solution

  • The error means (at least) one of your commands isn't exporting any data, which you try to access with command.data.

    Here's an example command as shown in the guide

    const { SlashCommandBuilder } = require('discord.js');
    
    module.exports = {
        data: new SlashCommandBuilder()
            .setName('ping')
            .setDescription('Replies with Pong!'),
        async execute(interaction) {
            await interaction.reply('Pong!');
        },
    };
    

    If you have this in all your commands, the issue might be is that there's a non-command js file in your folders. Make sure to check that too.