TypeError Cannot read properties of undefined (reading 'cache')

I'm trying to make a user info command that returns the user's roles, however I seem to be running into that error when I try to run the command.

This is my code:

const {SlashCommandBuilder} = require('discord.js');
const { EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
    .setName ('userinfo')
    .setDescription ('Get information about a user.')
    .addUserOption((option) => 
      option
        .setName('user')
        .setDescription('The user you want to get info about')
        .setRequired(true)

  ),
    async execute(interaction) {
    const user = interaction.options.getUser('user');
    const roles = user.roles.cache;

    const infoEmbed = new EmbedBuilder()
    .setColor(0xd66fe1)
    .setTitle('User Info')
    .setDescription('Display info about a selected user')
    .addFields( [
        { 
            name: 'Username', 
            value: `${interaction.user.username}`
    },

    
    {
        name: 'Roles',
        value: `${roles}`
    }

    ],
      
    )

     await interaction.reply({
        embeds: [infoEmbed]

    })

}
}
Was this page helpful?