'creating a help command for my discord bot

I am trying to create a help page that lists all the commands for my discord bot...

currently everything is coming through as Undefined within the discord and I am confused as to why.

Here is my help.js

const fs = require('fs');
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
    data: new SlashCommandBuilder()
    .setName('help')
    .setDescription('Lists all available commands'),
    async execute(interaction) {
        let str = '';
        const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

        for (const file of commandFiles) {
        const command = require(`./${file}`);
        str += `Name: ${command.name}, Description: ${command.description} \n`;
        }

        return void interaction.reply({
        content: str,
        ephemeral: true,
        });
    },
};

I could try to do the v12 way, but I am trying to make a bot that is completely up to date with v13...



Solution 1:[1]

Assuming all of your commands are structured like the code you presented, you were only missing a couple things. First was that the command name and description would be under command.data not just command. Also with let you can leave it empty (as I have) and fill it with anything.

const fs = require('fs');
const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
    data: new SlashCommandBuilder()
    .setName('help')
    .setDescription('Lists all available commands'),
    async execute(interaction) {
        let str
        const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

        for (const file of commandFiles) {
        const command = require(`./${file}`);
        str += `Name: ${command.data.name}, Description: ${command.data.description} \n`;
        }

        return interaction.reply({
        content: str,
        ephemeral: true,
        });
    },
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gh0st