I am working on a user context menu command to get the users info. One of the parts of it is getting their roles in the discord and adding a badge on the embed.. except it’s giving me the error ‘Cannot read properties of undefined (reading ‘cache’s
My code:
const { EmbedBuilder, ApplicationCommandType } = require("discord.js");
const { EMBED_COLORS } = require("@root/config");
/**
* @type {import('@structures/BaseContext')}
*/
module.exports = {
name: "userinfo",
description: "displays information about the user",
type: ApplicationCommandType.User,
enabled: true,
ephemeral: true,
async run(interaction) {
const user = await interaction.client.users.fetch(interaction.targetId);
const res = buildUserEmbed(user);
await interaction.followUp(res);
},
};
function buildUserEmbed(user) {
const embed = new EmbedBuilder()
.setAuthor({ name: user.tag, iconURL: user.displayAvatarURL() })
.setColor(EMBED_COLORS.BOT_EMBED)
.setThumbnail(user.displayAvatarURL())
.addFields(
{ name: "ID", value: user.id, inline: true },
{ name: "Username", value: user.username, inline: true },
{ name: 'Tags', value: `${getTags(user)}`, inline: true },
{ name: "Bot", value: user.bot ? "Yes" : "No", inline: true },
{ name: "Created At", value: user.createdAt.toUTCString(), inline: true }
)
.setFooter({ text: `Requested by ${user.tag}` });
return { embeds: };
}
function getTags(user) {
if (user.roles.cache.has('1256443051656745124')) {
return '<:owner:1257178217803153500>';
}
if (user.roles.cache.has('1256443109286613124') || user.roles.cache.has('1256443109286613124') || user.roles.cache.has('1256443261158162534') || user.roles.cache.has('1256443203754659841')) {
return '<:staff:1257178162584879104>';
}
if (user.roles.cache.has('1256443203754659841')) {
return '<:developer:1257178260689653770>'
}
if (user.roles.cache.has('1257178971863384168') || user.roles.cache.has('1256443120158244977') || user.roles.cache.has('1256443188609159175') || user.role.cache.has('1256443251129450599')) {
return '<:admin:1257176953027432448>'
}
if (user.role.cache.has('1256443308389957685')) {
return '<:member:1257178311587663903>'
}
}
The line-specific code that’s giving me a problem is line 38.
I have attempted to change the code, work around errors, I even used black box AI to help and none of it worked.
TheJoePage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.