无法读取null的属性“角色”。加入服务器时向用户添加角色

时间:2020-09-11 18:41:21

标签: javascript node.js discord.js

因此,基本上我只需要它即可添加角色,并且bot具有权限并且角色就在那里。完整的控制台如下

(c) 2020 Microsoft Corporation. All rights reserved.

C:\Users\thato\Desktop\Deku>node .
sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules\sequelize\lib\sequelize.js:245:13
═[Discord-Leveling Database Loaded -V1.1.0]═[Support server: https://discord.gg/eBFKDbx]=
Loading...
D3ku is online!
New member joined server, successfully sent a message!
New member joined server, successfully sent a message!
C:\Users\thato\Desktop\Deku\index.js:358
    let DaRole = message.guild.roles.cache.find(r => r.name === 'Human')
                               ^

TypeError: Cannot read property 'roles' of null
    at Client.<anonymous> (C:\Users\thato\Desktop\Deku\index.js:358:32)
    at Client.emit (events.js:326:22)
    at MessageCreateAction.handle (C:\Users\thato\Desktop\Deku\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\thato\Desktop\Deku\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)

(HERE WAS A BUNCH OF RANDOM ERRORS NO ONE NEEDS)

C:\Users\thato\Desktop\Deku>```

I really can\'t get it to send immediately either. It only sends the welcome message when the user sends a message in the server. That\'s also when the error finally appears.

2 个答案:

答案 0 :(得分:1)

该错误表明message.guildnull。这可能发生在部分消息或来自DM的消息上。

如果邮件是部分邮件,则message.partial将是true。要获取其余消息,请运行message.fetch().then(fullmessage => {...})

message.channel.type === 'dm'可以告诉您消息是否来自DM。

答案 1 :(得分:1)

我建议您添加

if(message.channel.type === 'dm') return message.reply('This command does not work in DMs')

到命令开头,它应该防止出现这种情况。