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);
}}}
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.
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()
}