我是编码的新手,我想在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并试图向其他人寻求帮助,但是基本上我被告知要“弄清楚”。
答案 0 :(得分:3)
在此行
let memberTag = member.user.tag;
缺少属性用户。
您应该添加一些防御性代码来处理这种情况,或者确定为什么它不存在。
类似
if (member.user && member.user.tag) {
// access tag here
} else {
// doesn’t exist
}
答案 1 :(得分:0)
如果我们要讨论的是第二段代码,则消息事件中没有member
参数。它应该是message
,并且邮件没有user
属性。这就是错误的根源。
client.on('message', message => {
let guild = message.guild;
let memberTag = message.author.tag;
// rest of code
});
Discord.js docs可能会有所帮助。