Home > Enterprise >  Why is this module in discord.js returning undefined?
Why is this module in discord.js returning undefined?

Time:03-30

So I'm working on a discord bot, and this command is supposed loop thru all commands send their names and descriptions. But whenever I run it gives me undefined. Here's my code:

module.exports = {
    name: 'help',
    aliases: ['commands'],
    permissions: ["EXAMPLE"],
    description: 'Gives you a list of all commands.',
    async execute(client, message, cmd, args, Discord){
        //create a function that gets all commands
        //get all commands
        const commands = client.commands
        //console.log(commands)
        //make a loop that goes through all commands
        //make a message embed
        //add all commands to the embed
        //send the embed
        const embed = new Discord.MessageEmbed()
        .setTitle('Commands')
        .setColor('#0099ff')
        .setDescription('These are all the commands you can use!')
        //make a loop that goes through all objects in the commands array and adds them to the embed
        for(const command of commands){
            embed.addField(`${command.name}`, `${command.description}`, true)
        }
        /*commands.forEach(command => {
            embed.addField(`${command.name}`, `${command.description}`)
        });
        
        for (i = 0; i < commands.length; i  ){
            let command = commands[i]
            embed.addField(`${command.name}`, `${command.description}`)
        }*/
        
        await message.channel.send( {embeds: [embed]} )
        
        
            
    }
}

If your wondering why there are so many comments, it's because I use github copilot

This is what I get when I run it:

embed with field names and values showing undefined

Also I tried logging it in the console and that returned this:

'8ball' => {
    name: '8ball',
    aliases: [ 'alias' ],
    permissions: [ 'EXAMPLE' ],
    description: '',
    execute: [Function: execute]
  },
  'clear' => {
    name: 'clear',
    aliases: [ 'purge' ],
    permissions: [ 'EXAMPLE' ],
    description: '',
    execute: [AsyncFunction: execute]
  },
  'exile' => {
    name: 'exile',
    aliases: [ 'alias' ],
    permissions: [ 'EXAMPLE' ],
    description: '',
    execute: [AsyncFunction: execute]
  },
  'gban' => {
    name: 'gban',
    aliases: [ 'globalban' ],
    permissions: [ 'EXAMPLE' ],
    description: 'Global Bans the User from all servers their in',
    execute: [AsyncFunction: execute]
  },
  'help' => {
    name: 'help',
    aliases: [ 'commands' ],
    permissions: [ 'EXAMPLE' ],
    description: 'Gives you a list of all commands.',
    execute: [AsyncFunction: execute]
  },
  'jointhread' => {
    name: 'jointhread',
    aliases: [ 'alias' ],
    permissions: [ 'EXAMPLE' ],
    description: '',
    execute: [AsyncFunction: execute]
  },
  'lockdown' => {
    name: 'lockdown',
    aliases: [ 'alias' ],
    permissions: [ 'EXAMPLE' ],
    description: '',
    execute: [Function: execute]
  },
  'ping' => {
    name: 'ping',
    aliases: [ 'pong', 'ping' ],
    permissions: [ 'SEND_MESSAGES' ],
    description: 'Ping command',
    execute: [Function: execute]
  },
  'slowmode' => {
    name: 'slowmode',
    aliases: [ 'sm', 'slowm', 'smode' ],
    permissions: [ 'MANAGE_MESSAGES' ],
    description: 'Changes the slowmode of the current channel',
    execute: [Function: execute]
  },
  'cmd' => {
    name: 'cmd',
    aliases: [ 'alias' ],
    permissions: [ 'EXAMPLE' ],
    description: '',
    execute: [Function: execute]
  },
  'timeout' => {
    name: 'timeout',
    aliases: [ 'alias' ],
    permissions: [ 'EXAMPLE' ],
    description: '',
    execute: [AsyncFunction: execute]
  },
  'unban' => {
    name: 'unban',
    aliases: [ 'alias' ],
    permissions: [ 'EXAMPLE' ],
    description: 'Unbans a user',
    execute: [AsyncFunction: execute]
  },
  'verify' => {
    name: 'verify',
    aliases: [ 'alias' ],
    permissions: [ 'EXAMPLE' ],
    description: '',
    execute: [Function: execute]
  },
  'warn' => {
    name: 'warn',
    aliases: [],
    description: 'Warn command',
    permissions: [ 'KICK_MEMBERS' ],
    execute: [Function: execute]
  }
}

CodePudding user response:

That's because it's a Map instance, so command is actually an array with the key and value. Iterate over the values instead:

for (const command of commands.values()) {
    embed.addField(`${command.name}`, `${command.description}`, true)
}
  • Related