首页 > 解决方案 > 无法读取 null 的属性“角色”。在加入服务器后向用户添加角色时

问题描述

所以基本上我只需要它来添加角色并且机器人具有权限并且角色就在那里。完整的控制台如下

(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.

标签: javascriptnode.jsdiscord.js

解决方案


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

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

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


推荐阅读