Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!