我希望我的机器人向加入我的服务器的用户发送私人消息。
为此,我使用了:
client.on('guildMemberAdd', member => {
const channel = member.guild.channels.find(ch => ch.name === 'benvenuto');
channel.send(`Date il benvenuto a ${member}!`);
member.send('test');
});
用户加入服务器后,就会出现此错误:
TypeError: Cannot read property 'roles' of null
at Client.client.on.message (C:\Users\Kikkiu\Desktop\BotTuttofare\index.js:9:19)
[...]
错误使我进入了代码的这一部分:
if(message.member.roles.find(r => r.name === "Admin") || message.member.roles.find(r => r.name === "Mod")) {
if(!message.author.bot) {
if(message.content == '.del50')
message.channel.bulkDelete(50)
if(message.content == '.del100')
message.channel.bulkDelete(100)
if(message.content == '.del1')
message.channel.bulkDelete(2)
if(message.content == '.del10')
message.channel.bulkDelete(10)
if(message.content == '.del2')
message.channel.bulkDelete(3)
}}
如果我删除发送私人消息的部分,则不会出现此错误。会是什么?
答案 0 :(得分:1)
我认为您无法在这样的成员上找到角色。
您在公会(.find()
)上使用member.guild.roles.find(r => r.name == "Admin")
如果您想检查他们是否获得了corect角色,应该可以执行以下操作:
let adminRole = message.guild.roles.get("ADMIN_ROLE_ID") //same with mod role
if (message.member.roles.has(adminRole.id)) {
//If they have admin & mod role they can do this
} else {
//If they do not have the one of the roles, this happens. Either just do return; or do //something like dm them that they don't have roles/permissions
}
但是我建议您为此单独使用一个参数,即使用args或一些东西来定义要在通道中填充多少,这很令人困惑。