Discord Bot Command Error: Undefined Property

Hey everyone, I’m having trouble with my Discord bot. One of the commands isn’t working as expected. When I try using it, the console shows this error:

TypeError: Cannot read properties of undefined (reading 'extracustomdesc')
at allotherembeds_eachcategory (C:\Users\bot\Desktop\MyBot\commands\Info\assist.js:521:97)
at Object.run (C:\Users\bot\Desktop\MyBot\commands\Info\assist.js:302:35)
at processTicksAndRejections (node:internal/process/task_queues:96:5)

I suspect the issue might be in this section of my code:

let infoEmbed = new MessageEmbed()
  .setTitle(`[${bot.commands.filter(c => c.group === 'Special Features').first().extraInfo.length}] Special Features | ${config.SPECIAL_FEATURES ? 'Enabled' : 'Disabled'}`)
  .setDescription(`${bot.commands.filter(c => c.group === 'Special Features').first().extraInfo.split(',').map(i => i?.trim()).join(' | ')}`)
  .addField('How to use', bot.commands.filter(c => c.group === 'Special Features').first().usage)

if (!hideDisabled || config.SPECIAL_FEATURES || config.showAll) embeds.push(infoEmbed)

I’m new to Discord bots and a bit lost on where to start troubleshooting. Any suggestions would be greatly appreciated. Thanks for your help!

Looks like the issue’s with accessing a property that doesn’t exist. Try checking if ‘bot.commands.filter(c => c.group === ‘Special Features’).first()’ returns a valid command. if not, it’ll cause that error. Maybe add a null check before accessing extraInfo? just a thought!

I’ve encountered similar issues before, and it can be frustrating. From my experience, the error you’re seeing often occurs when trying to access properties of an undefined object. In your case, it seems the ‘extracustomdesc’ property is causing trouble.

One approach that’s worked for me is to implement optional chaining. It’s a lifesaver when dealing with potentially undefined properties. Try modifying your code like this:

let infoEmbed = new MessageEmbed()
  .setTitle(`[${bot.commands.filter(c => c.group === 'Special Features').first()?.extraInfo?.length || 0}] Special Features | ${config.SPECIAL_FEATURES ? 'Enabled' : 'Disabled'}`)
  .setDescription(`${bot.commands.filter(c => c.group === 'Special Features').first()?.extraInfo?.split(',').map(i => i?.trim()).join(' | ') || 'No description available'}`)
  .addField('How to use', bot.commands.filter(c => c.group === 'Special Features').first()?.usage || 'Usage information not available')

This way, if any part of the chain is undefined, it won’t throw an error. It’s been a game-changer for me in handling these types of issues. Give it a shot and see if it resolves your problem!

The error you’re encountering suggests a problem with accessing properties that might not exist. From my experience developing Discord bots, this often happens when the command structure isn’t exactly as expected.

I’d recommend double-checking your command definitions. Make sure all ‘Special Features’ commands have the necessary properties like ‘extraInfo’ and ‘usage’. Additionally, consider adding some safety checks in your code.

Here’s a safer approach you could try:

const specialCommand = bot.commands.find(c => c.group === 'Special Features');
if (specialCommand && specialCommand.extraInfo) {
    let infoEmbed = new MessageEmbed()
        .setTitle(`[${specialCommand.extraInfo.length}] Special Features | ${config.SPECIAL_FEATURES ? 'Enabled' : 'Disabled'}`)
        .setDescription(specialCommand.extraInfo.split(',').map(i => i.trim()).join(' | '))
        .addField('How to use', specialCommand.usage || 'Usage not specified');

    if (!hideDisabled || config.SPECIAL_FEATURES || config.showAll) embeds.push(infoEmbed);
}

This should prevent errors if the command or its properties are undefined. Hope this helps!