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

TypeError: Cannot read properties of undefined (reading 'createdTimestamp') discord.js

Basically i am trying to create a userinfo command for discord.js, i followed some tutorials and i seemed to make one, but i get this error saying:

TypeError: Cannot read properties of undefined (reading ‘createdTimestamp’)

The code is here: (userinfo.js)

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

const moment = require('moment');

module.exports = {
    name: 'userinfo',
    description: 'fetch the info of the user!',
    async execute(message, args, Discord, prefix, client){
        const target = message.mentions.users.first() || message.author;
        const member = message.guild.members.cache.get(target.id);

        const embed = new Discord.MessageEmbed()
        .setAuthor(`${target.username}`, target.displayAvatarURL({dynamic: true}))
        .setThumbnail(target.displayAvatarURL({dynamic: true}))
        .setColor('#0afc17')
        .addField('User ID', `${target.id}`)
        .addField('Roles', `${member.roles.cache.map(r => r).join(' ').replace('@everyone', '')}` || 'None')
        // .addField('Server Member Since', `${moment(member.joinedAt).format('MMMM Do YYYY, h:mm')}\n**-** ${moment(member.joinedAt).startOf('day').fromNow()}`)
        .addField('Server Member Since', `<t:${parseInt(target.joinedTimestamp / 1000)}:R>`, true)
        .addField('Discord User Since', `<t:${parseInt(target.user.createdTimestamp / 1000)}:R>`, true)

        message.channel.send({ embeds:  })
    }
}

main.js

const Discord = require('discord.js');
const client = new Discord.Client({
    intents: ['GUILDS', 'GUILD_MESSAGES']
});

const prefix = '!';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}


client.once('ready', () => {
    console.log('Bot is online!');
});

client.on('messageCreate', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if (!client.commands.get(command)) return;

    client.commands.get(command).execute(message, args, Discord, prefix, client);
})

// client.on('guildMemberAdd', guildMember =>{
//     let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'xxx');
 
//     guildMember.roles.add(welcomeRole);
//     guildMember.guild.channels.cache.get('xxx').send(`Welcome <@${guildMember.user.id}> to the server!`)
// });

client.login('xxx');

Here is the full error:

(node:19418) DeprecationWarning: Passing strings for MessageEmbed#setAuthor is deprecated. Pass a sole object instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
/Users/xxx/Desktop/DiscordBot/commands/userinfo.js:19
        .addField('Discord User Since', `<t:${parseInt(target.user.createdTimestamp / 1000)}:R>`, true)
                                                                   ^

TypeError: Cannot read properties of undefined (reading 'createdTimestamp')
    at Object.execute (/Users/xxx/Desktop/DiscordBot/commands/userinfo.js:19:68)
    at Client.<anonymous> (/Users/xxx/Desktop/DiscordBot/main.js:32:34)
    at Client.emit (node:events:527:28)
    at MessageCreateAction.handle (/Users/xxx/Desktop/DiscordBot/node_modules/discord.js/src/client/actions/MessageCreate.js:26:14)
    at module.exports [as MESSAGE_CREATE] (/Users/xxx/Desktop/DiscordBot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/Users/xxx/Desktop/DiscordBot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (/Users/xxx/Desktop/DiscordBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/Users/xxx/Desktop/DiscordBot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/Users/xxx/Desktop/DiscordBot/node_modules/ws/lib/event-target.js:199:18)
    at WebSocket.emit (node:events:527:28)

Node.js v18.0.0

and here are how my files are setup

Any help would be appreciated!

>Solution :

.addField('Discord User Since', `<t:${parseInt(target.user.createdTimestamp / 1000)}:R>`, true)

instead of

.addField('Discord User Since', `<t:${parseInt(target.createdTimestamp / 1000)}:R>`, true)
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