我想将用户添加到角色时遇到问题。
我的代码:
console.log(client.guild.roles.cache.get('720257839259451393'));
我从机器人那里得到了
TypeError: Cannot read property 'roles' of undefined
at Request._callback (/home/DiscordBot/main.js:127:62)
at Request.self.callback (/home/DiscordBot/node_modules/request/request.js:185:22)
at Request.emit (events.js:315:20)
at Request.<anonymous> (/home/DiscordBot/node_modules/request/request.js:1154:10)
at Request.emit (events.js:315:20)
at IncomingMessage.<anonymous> (/home/DiscordBot/node_modules/request/request.js:1076:12)
at Object.onceWrapper (events.js:421:28)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1224:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
答案 0 :(得分:1)
该错误表示该行会未定义。有人在bot的dms中使用该命令。您只需要一个简单的if语句
if(message.guild)
{
//The command is used in server
}else
{
//The command is used in dms
}