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]

})

}
}
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]

})

}
}
7 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Haize
HaizeOP2y ago
djs- [email protected] node - v16.17.1
CanineData
CanineData2y ago
Users don't have roles, members do
d.js docs
d.js docs2y ago
Despite sounding similar there is a distinct difference between users and members in Discord: • User: global Discord user data (global avatar, username, tag, id) • GuildMember: user data associated to a guild (guild, nickname, roles, voice, guild avatar, etc.) • Conversion: User ➞ GuildMember | GuildMember ➞ User
Haize
HaizeOP2y ago
const user = interaction.options.getUser('user'); so this is where I replace?
CanineData
CanineData2y ago
.getMember instead of .getUser
Haize
HaizeOP2y ago
thanks a lot!
Want results from more Discord servers?
Add your server