我设置了addRole命令,该命令有效,但是如果有人仅键入“!rbu”但没有成员,则机器人会出错。
也许强迫一个人使代码起作用,他们必须输入这个人,但是这对我来说是多么新的
let args = message.content.substring(PREFIX.length).split(" ");
switch(args[0]){
case'rbu':
case'rbu':
if(!message.member.roles.find(r => r.name === "?GroupI?")) return message.channel.send('You cant do that')
let memberToModify = message.mentions.members.first();
var roleToAdd = message.guild.roles.find(role => role.name === "?GroupII?");
memberToModify.addRole(roleToAdd);
break;
}
}
)
I expect this to do nothing or message that member must type !rbu and user
memberToModify.addRole(roleToAdd)
^
TypeError: Cannot read property 'addRole' of undefined```
答案 0 :(得分:0)
首先,您有2个case'rbu':
,这很奇怪。然后,您需要确保该消息实际上以message.mentions
的形式提及,例如:
let args = message.content.substring(PREFIX.length).split(" ");
switch(args[0]){
case'rbu':
if(!message.member.roles.find(r => r.name === "?GroupI?") || !message.mentions) return
message.channel.send('You cant do that')
let memberToModify = message.mentions.members.first();
var roleToAdd = message.guild.roles.find(role => role.name === "?GroupII?");
memberToModify.addRole(roleToAdd);
break;
}