hyrulian.prince
hyrulian.prince

Reputation: 35

How to fix 'TypeError: Cannot read property 'tag' of undefined' error in NodeJS

I'm new to coding and I'm trying to set up a bot on Discord... I want the bot to message the user with an embed, similar to one I used earlier in my project that works just fine. I changed the code a bit and it won't register this one, even though the other code works just fine. What am I doing wrong? The error message reads:

TypeError: Cannot read property 'tag' of undefined
    at Client.client.on (C:\Users\Tristan\my-bot\pokegrove\index.js:112:29)
    at Client.emit (events.js:194:15)
    at MessageCreateHandler.handle (C:\Users\Tristan\my-bot\pokegrove\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\Tristan\my-bot\pokegrove\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (C:\Users\Tristan\my-bot\pokegrove\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\Tristan\my-bot\pokegrove\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\Tristan\my-bot\pokegrove\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:189:13)
    at Receiver._receiver.onmessage (C:\Users\Tristan\my-bot\pokegrove\node_modules\ws\lib\websocket.js:137:47)
    at Receiver.dataMessage (C:\Users\Tristan\my-bot\pokegrove\node_modules\ws\lib\receiver.js:409:14)

I've tried changing parts of the original code around to fit my needs. I've searched for other issues pertaining to the error code I've received, but I can't find anything that's specific to my issue... Here's the original code I was using (this one works fine):

client.on("guildMemberAdd", (member) => { // Check out previous chapter for information about this event
let guild = member.guild; 
let memberTag = member.user.tag; 
if(guild.systemChannel){
    guild.systemChannel.send(new Discord.RichEmbed() // Creating instance of Discord.RichEmbed
    .setTitle("A new user joined") // Calling method setTitle on constructor. 
    .setDescription(memberTag + " has joined the adventure") // Setting embed description
    .setThumbnail(member.user.displayAvatarURL) // The image on the top right; method requires an url, not a path to file!
    .addField("Members now", member.guild.memberCount) // Adds a field; First parameter is the title and the second is the value.
    .setTimestamp() // Sets a timestamp at the end of the embed
    );
}
});

Ignore the "addfield" on this code, I'm still changing that (it also has the same issue without this added, so it doesn't seem to matter). Here is the code I changed a bit (that won't work):

client.on('message', (member) => { // Check out previous chapter for information about this event
let guild = member.guild; 
let memberTag = member.user.tag; 
if(prefix + "donate"){
    message.author.send(new Discord.RichEmbed() // Creating instance of Discord.RichEmbed
    .setTitle("Thank you for your support!") // Calling method setTitle on constructor. 
    .setDescription(memberTag + ", with your [donation](https://www.paypal.me/pokegroveofficial), we can continue to better PokéGrove (and buy some more Poké treats!)") // Setting embed description
    .setThumbnail("http://i68.tinypic.com/2ltq9nt.jpg") // The image on the top right; method requires an url, not a path to file!
    .setImage("http://i68.tinypic.com/2ltq9nt.jpg")
    .addField("Members now", member.guild.memberCount) // Adds a field; First parameter is the title and the second is the value.
    .setTimestamp() // Sets a timestamp at the end of the embed
    );
}
});

I expect the code to send an embedded direct message, and instead, the bot crashes and gives that error. Any help would be really appreciated because I've looked through Google and tried to ask other people for help, but I basically got told, "figure it out."

Upvotes: 2

Views: 2946

Answers (2)

slothiful
slothiful

Reputation: 5623

If we're talking about the second piece of code you have, there's no member parameter in a message event. It would be message, and a message doesn't have a user property. That's the source of your error.

client.on('message', message => {
    let guild = message.guild;
    let memberTag = message.author.tag;

    // rest of code
});

Discord.js docs may be helpful.

Upvotes: 0

Matt
Matt

Reputation: 2213

On this line

let memberTag = member.user.tag;

The property user is missing.

You should add some defensive code to handle this happening, or identify why it’s not there.

Something like

if (member.user && member.user.tag) {
 // access tag here
} else {
 // doesn’t exist
}

Upvotes: 2

Related Questions