【问题标题】:How to fix 'TypeError: Cannot read property 'tag' of undefined' error in NodeJS如何修复 NodeJS 中的“TypeError:无法读取未定义的属性“标签”错误
【发布时间】:2019-08-31 08:05:24
【问题描述】:

我是编码新手,我正在尝试在 Discord 上设置一个机器人...我希望该机器人通过嵌入向用户发送消息,类似于我之前在我的项目中使用的一个,它工作得很好。我稍微更改了代码,它不会注册这个,即使其他代码工作得很好。我究竟做错了什么?错误信息如下:

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)

我已尝试更改部分原始代码以满足我的需要。我已经搜索了与收到的错误代码有关的其他问题,但我找不到任何特定于我的问题的内容......这是我使用的原始代码(这个工作正常):

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

忽略此代码上的“addfield”,我仍在更改它(如果没有添加它,它也有同样的问题,所以这似乎无关紧要)。这是我稍微更改的代码(不起作用):

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

我希望代码发送嵌入的直接消息,但机器人会崩溃并给出该错误。任何帮助都将不胜感激,因为我浏览了谷歌并试图向其他人寻求帮助,但我基本上被告知,“弄清楚。”

【问题讨论】:

  • 您没有从成员中的用户那里获取标签,您可以做的一件事是在分配 tag 之前尝试添加故障安全条件,例如 let memberTag = (member && member.user && member.user.tag) ? member.user.tag : " "

标签: node.js discord discord.js


【解决方案1】:

在这条线上

let memberTag = member.user.tag;

属性用户丢失。

您应该添加一些防御性代码来处理这种情况,或者找出它不存在的原因。

类似

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

【讨论】:

    【解决方案2】:

    如果我们谈论的是您拥有的第二段代码,则消息事件中没有 member 参数。它将是message,并且消息没有user 属性。这就是你的错误的根源。

    client.on('message', message => {
        let guild = message.guild;
        let memberTag = message.author.tag;
    
        // rest of code
    });
    

    Discord.js docs 可能会有所帮助。

    【讨论】:

    • 另外值得注意的是member.user无论如何都应该始终可用。
    猜你喜欢
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2021-10-03
    • 2021-10-28
    • 1970-01-01
    • 2021-05-25
    相关资源
    最近更新 更多