Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
Error with arguments in commands in Discord.JS

so stumbled upon another issue and can't really wrap my head around it. Using the following code it keeps returning that "cannot read property "username" of undefined, however with the command "!stats @USER" the argument should have been set to the user, right?

Any help would be greatly appreciated!

    const args = message.content.trim().split(' ');
    const command = args.shift().toLowerCase();

if(command === '!stats'){
  if (args.length){
  var userMentioned = args[0].id
  message.delete()
} else{
  var userMentioned = message.author.id
  message.delete()
} 

if (message.channel.id === "876597275856601168") {{
  setTimeout(function (){
    message.delete();
     const statsEmbed = new MessageEmbed()
    .setColor('#8F00FF')
    .setAuthor(userMentioned.user.username, userMentioned.user.displayAvatarURL())
    .addFields(
        { name: '<:logoas2:876840515465797632>Current Level:', value: 'Current: **' + (userMentioned.userStats.level) + '**', inline: true },
        { name: '<:xp2:874732251571707966>Current XP:', value: 'Amount: **' + userMentioned.userStats.xp + '**', inline: true },
        { name: '<:xp3:876841199456092161>To Next Level:', value: 'Amount: **' + (userMentioned.xpToNextLevel - userMentioned.userStats.xp) + '**', inline: true },
        { name: '<:etherite:874730607106748457>Etherite:', value: 'Amount: **' + userMentioned.userStats.etherite + '**', inline: true },
        { name: '<:time:876866100774707290>Member Since:', value: 'Date: **' + userMentioned.userStats.dateofJoin + '**', inline: true },
    )
    .setTimestamp()
    .setFooter('Avesong', client.user.displayAvatarURL());
  client.channels.cache.get("876597275856601168").send({ embeds: [statsEmbed] });
}, 500); 
}}} 
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I would suggest using:

var userMentioned = Client.users.cache.get(args[0]);

Just like it's been observed before - right now you're inputting a string value which obviously does not have .id property and so on.

Why would I opt for the above? If I recall correctly - this is the most elegant way to prevent errors about the bot not being able to fetch data from users not listed in the guild. While this might not be the case, it's still wise to prevent such errors.

Also, you will probably need to check your bot's intents in the dev portal - you will most definitely need GUILD_MEMBERS flag active.

Docs about cache.get()

about 4 years ago · Juan Pablo Isaza Denunciar

0

Splitting the message content does not return the user mentioned. It instead returns the string like this: "<@UserId>". You can use Message.mentions instead.

if (message.mentions.users.size){
  var userMentioned = message.mentions.users.first()
  message.delete()
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda