TypeError: Cannot read properties of undefined (reading 'user')

// userCommand.js
const { EmbedBuilder, ApplicationCommandOptionType } = require('discord.js');

module.exports = {
name: 'user',
description: 'Get user information.',
options: [{
name: 'username',
description: 'Just put the username of the person you want to know those information',
type: ApplicationCommandOptionType.User, // Corrected property name to 'type'
required: false,
}],

execute: async (interaction) => {
const userOption = interaction.options.get("username") || interaction?.user;
console.log(interaction.user.username)

const embed = new EmbedBuilder()
.setTitle(`${userOption.user.username}'s Information`)
.setColor(0x00a6ed)
.setThumbnail(userOption.user.displayAvatarURL())
.addFields(
{ name: "General", value: `>>> **User ID:** ${userOption.user.id}\n`+
`**Global Name:** ${userOption.user.globalName || 'None'}\n` +
`**Username:** ${userOption.user.username}\n` +
`**Bot:** ${userOption.user.bot ? "Bot" : "Human"}\n`, inline: false },
{ name: "Server", value: `>>> **Nick Name:** ${userOption.member?.nickname || "undefined"}\n` +
`**Highest Role:** ${userOption.member?.roles.highest || "undefined"}\n`, inline: true },
{ name: "\u200b", value: `>>> **Created At:** <t:${Math.floor(userOption.user.createdTimestamp / 1000)}:R>\n` +
`${userOption.member && userOption.member.joinedTimestamp ? `**Joined At:** <t:${Math.floor(userOption.member.joinedTimestamp / 1000)}:R>` : ''}`, inline: true }
);

try {
console.log(userOption);
interaction.reply({ embeds: [embed], ephemeral: true });
} catch (error) {
interaction.reply({ content: 'There was an error while executing the command.', ephemeral: true });
console.error(error);
}
}
};
// userCommand.js
const { EmbedBuilder, ApplicationCommandOptionType } = require('discord.js');

module.exports = {
name: 'user',
description: 'Get user information.',
options: [{
name: 'username',
description: 'Just put the username of the person you want to know those information',
type: ApplicationCommandOptionType.User, // Corrected property name to 'type'
required: false,
}],

execute: async (interaction) => {
const userOption = interaction.options.get("username") || interaction?.user;
console.log(interaction.user.username)

const embed = new EmbedBuilder()
.setTitle(`${userOption.user.username}'s Information`)
.setColor(0x00a6ed)
.setThumbnail(userOption.user.displayAvatarURL())
.addFields(
{ name: "General", value: `>>> **User ID:** ${userOption.user.id}\n`+
`**Global Name:** ${userOption.user.globalName || 'None'}\n` +
`**Username:** ${userOption.user.username}\n` +
`**Bot:** ${userOption.user.bot ? "Bot" : "Human"}\n`, inline: false },
{ name: "Server", value: `>>> **Nick Name:** ${userOption.member?.nickname || "undefined"}\n` +
`**Highest Role:** ${userOption.member?.roles.highest || "undefined"}\n`, inline: true },
{ name: "\u200b", value: `>>> **Created At:** <t:${Math.floor(userOption.user.createdTimestamp / 1000)}:R>\n` +
`${userOption.member && userOption.member.joinedTimestamp ? `**Joined At:** <t:${Math.floor(userOption.member.joinedTimestamp / 1000)}:R>` : ''}`, inline: true }
);

try {
console.log(userOption);
interaction.reply({ embeds: [embed], ephemeral: true });
} catch (error) {
interaction.reply({ content: 'There was an error while executing the command.', ephemeral: true });
console.error(error);
}
}
};
hi I'm trying to get some info on the user tag, but doesnt work on myself (just typing the command and passing myself as a user)
No description
3 Replies
d.js toolkit
d.js toolkit12mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by staff
treble/luna
treble/luna12mo ago
use getUser
K's
K'sOP12mo ago
i'll test that it worked thanks
Want results from more Discord servers?
Add your server