简体   繁体   English

提及检测discord bot javascript的问题

[英]Problem with mention detection discord bot javascript

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

I think you should check the members element before getting its id我认为您应该在获取其id之前检查 members 元素

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

2 things in advance:提前2件事:

1. Use always === / !== and not == / != ! 1.始终使用=== / !==而不是== / != !

2. channel.sendMessage() is deprecated! 2. channel.sendMessage()已弃用! Use channel.send() instead.改用channel.send()


Cleaned up code and problem fixed (much like @ThanhPhan does)清理代码并修复问题(很像@ThanhPhan)

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!**")

  }

 }

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM