Search code examples
javascriptnode.jsdiscorddiscord.js

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.


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,
      },
    );
    

    As for target, it's the same issue; message.mentions.users.first() is a User. You could create a new variable, e.g. targetMember and assign message.mentions.members.first(), so it will be a GuildMember:

    const target = message.mentions.users.first();
    const targetMember = message.mentions.members.first();
    

    And then, just replace target:

    .addFields(
      { name: 'ID', value: target.id },
      {
        name: 'Joined Server',
        value: `<t:${parseInt(targetMember.joinedTimestamp / 1000, 10)}:R>`,
        inline: true,
      },
      {
        name: 'Account Created',
        value: `<t:${parseInt(target.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.