Reputation: 21
I want my bot to answer something when I mention it, it works once then the bot crashes and i get an error in this code:
bot.on('message', message =>{
if(message.mentions.members.first().id == '602929944779292682'){
message.channel.sendMessage('**A votre service!**')
}
})
Error message:
TypeError: Cannot read property 'id' of undefined
Upvotes: 2
Views: 1253
Reputation: 450
2 things in advance:
1. Use always ===
/!==
and not ==
/!=
!
2. channel.sendMessage()
is deprecated! Use channel.send()
instead.
Cleaned up code and problem fixed (much like @ThanhPhan does)
bot.on("message", message => {
if (message.mentions.members.first() !== undefined) {
if (message.mentions.members.first().id === bot.user.id) { # This is the bots user id
message.channel.send("**A votre service!**")
}
}
})
Upvotes: 0
Reputation: 1339
I think you should check the members element before getting its id
bot.on('message', message =>{
if (message.mentions.members.first() !== undefined) {
if(message.mentions.members.first().id == '602929944779292682'){
message.channel.sendMessage('**A votre service!**')
}
} else {
// Handle members.first() is undefined
}
})
Upvotes: 1