Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda