node.js - 如何修复 NodeJS 中的 'TypeError: Cannot read property ' tag' of undefined' 错误

标签 node.js discord discord.js

我是编码新手,我正在尝试在 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
    );
}
});

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

最佳答案

在这条线上

let memberTag = member.user.tag;

缺少属性用户。

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

类似的东西

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

关于node.js - 如何修复 NodeJS 中的 'TypeError: Cannot read property ' tag' of undefined' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55602807/

相关文章:

mysql - 为什么我的 sequelize 查询在急切加载与另一个关联的模型时会因 ER_BAD_FIELD_ERROR 而中断?

javascript - 如何在条件语句中结束 emitter.on

javascript - 如何拆分消息并将第二部分重新发送到文本 channel ?

javascript - 如何使用 discord.js 在消息中提及用户?

javascript - 如何清理 Node 中的对象数组?手动遍历它返回 'object Object'

mysql - 使用mysql node.js批量插入时出现Sql语法错误

javascript - Mongoose + lodash 扩展错误地复制对象数组

python - 从消息中获取图片

java - 从 ID 获取用户名 - Discord JDA

javascript - 如何在discord.js 中创建使状态发生变化的命令?