我试图创建一个 const 成员,但它显示“类型错误:无法读取未定义的属性 'users'”
module.exports = {
name: 'welcome',
description: 'welcome',
execute(message, args, Discord) {
const member = message.mentions.users.first();
if(message.member.permissions.has('ADMINISTRATOR')){
message.channel.send(
new MessageEmbed()
.setTitle('title')
.setImage('imageurl')
.setDescription('desc')
)
}else{
return message.channel.send(`You don't have any permissions to do that!`)
}
}
}
答案 0 :(得分:0)
正如特雷弗·汤普森所说。那是因为你没有提到任何人。我已经测试过它并且它有效。以下是您应该使用的:
module.exports = {
name: 'welcome',
description: 'welcome',
execute(message, args, Discord) {
const member = message.mentions.users.first();
if (!member) { // If a user is not mentioned
message.channel.send(`Hey ${message.author} you need to mention a user to welcome!`)
return false; // < Even after it checks if a user is mentioned or not, use return false; This will prevent you from getting error of `users` being undefined
}
if(message.member.permissions.has('ADMINISTRATOR')){
message.channel.send(
new MessageEmbed()
.setTitle('title')
.setImage('imageurl')
.setDescription('desc')
)
}else{
return message.channel.send(`You don't have any permissions to do that!`)
}
}
}
答案 1 :(得分:-1)
这是因为提及未定义,您可以通过在提及后面添加问号来忽略此错误。
const member = message?.mentions?.users.first();