node.js - 如何修复 NodeJS 中的“TypeError:无法读取未定义的属性“标签”错误
问题描述
我是编码新手,我正在尝试在 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
);
}
});
我希望代码发送嵌入的直接消息,相反,机器人会崩溃并给出该错误。任何帮助都将不胜感激,因为我浏览了谷歌并试图向其他人寻求帮助,但我基本上被告知,“弄清楚”。
解决方案
在这条线上
let memberTag = member.user.tag;
属性用户丢失。
您应该添加一些防御性代码来处理这种情况,或者确定它不存在的原因。
就像是
if (member.user && member.user.tag) {
// access tag here
} else {
// doesn’t exist
}
推荐阅读
- javascript - 如何将数据发送到另一个页面?
- javascript - 猫鼬不验证更新的部分
- swift - 无法将“MobileNetV2”类型的值转换为预期的参数类型“VNCoreMLModel”
- json - 从 Pyomo 输出解析 JSON 变量数据
- node.js - 更改与其他文档相关的文档中的字段的方法
- c# - 无法统一引用游戏对象的 2D 数组
- html - 我是 html 的初学者,我的代码不起作用
- javascript - 兄弟组件不会在状态更改时重新渲染(使用 useEffect、useState 和 Context)
- swift - 当输入的值大于某个值时如何限制用户输入 Swift 5.1
- php - 在 MYSQL 中搜索拼写错误的单词的最佳方法