Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

joinedAtTimeStamp is showing Nan discord.js

I was coding a userInfo command but when I use the command, the joinedAtTimeStamp is showing <t:NaN:R> in the embed. It’s the only problem in this code.

My code:

const { MessageEmbed, ContextMenuInteraction } = require("discord.js");

module.exports = {
    name: "userInfo",
    aliases: ["user"],
    permissions: ["SEND_MESSAGES", "ATTACH_FILES"],
    description: "user",

    async execute(message, args, cmd, client, Discord, profileData) {
        const target = message.mentions.users.first();
        if(!args[0]) {
            const response2 = new MessageEmbed()
            .setColor("RANDOM")
            .setAuthor({name: message.author.tag, iconURL: message.author.displayAvatarURL({dynamic: true})})
            .setThumbnail(message.author.displayAvatarURL({dynamic: true}))
            .addFields(
                {name: "ID", value: message.author.id},
                {name: "Joined Server", value: `<t:${parseInt(message.author.joinedTimestamp / 1000)}:R>`, inline: true},
                {name: "Account Created", value: `<t:${parseInt(message.author.createdTimestamp / 1000)}:R>`, inline: true},
                
            );
            message.reply({embeds:[response2]});
        }
        const response = new MessageEmbed()
            .setColor("RANDOM")
            .setAuthor({name: target.tag, iconURL: target.displayAvatarURL({dynamic: true})})
            .setThumbnail(target.displayAvatarURL({dynamic: true}))
            .addFields(
                {name: "ID", value: target.id},
                {name: "Joined Server", value: `<t:${parseInt(target.joinedTimestamp / 1000)}:R>`, inline: true},
                {name: "Account Created", value: `<t:${parseInt(target.createdTimestamp / 1000)}:R>`, inline: true},
            );
            
        message.reply({embeds: [response], ephemeral: true})
    }
}

I am using discord.js v13 and node 16.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

message.author is a User and it doesn’t have a joinedTimestamp property, only GuildMembers have. message.member represents the author of the message as a guild member, so you can use that as it will have a joinedTimestamp property.

The reason you see NaN instead of the correct value is because parseInt will return NaN if you try to parse undefined:

console.log('undefined:', parseInt(undefined / 1000, 10));
console.log('3459192421512:', parseInt(3459192421512 / 1000, 10));

The following code should work as expected

.addFields(
  { name: 'ID', value: message.author.id },
  {
    name: 'Joined Server',
    value: `<t:${parseInt(message.member.joinedTimestamp / 1000, 10)}:R>`,
    inline: true,
  },
  {
    name: 'Account Created',
    value: `<t:${parseInt(message.author.createdTimestamp / 1000, 10)}:R>`,
    inline: true,
  },
);

PS: It’s a good idea to use the radix in parseInt. That’s why I added 10 as the second parameter in parseInt.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading