[英]TypeError: Cannot read property 'cache' of undefined in Discord.js
So, I am coding a Discord bot using Discord.js.所以,我正在使用 Discord.js 编写 Discord 机器人。 One of the things I made is a user info command.我做的其中一件事是用户信息命令。 I want it to show the roles of a member and the game that they are playing (if they are in one).我希望它显示成员的角色和他们正在玩的游戏(如果他们在一个游戏中)。 For the roles, I am getting TypeError: Cannot read property 'roles' of undefined
.对于角色,我收到TypeError: Cannot read property 'roles' of undefined
。 Please help.请帮忙。 You may use the following code for reference.您可以使用以下代码作为参考。
case 'whois':
const embed = new Discord.MessageEmbed()
var user = message.mentions.users.first()
if(!args[1]) return message.reply('Apologies! Please specify a particular member!');
embed.setTitle('User Information')
embed.addField('Username', user.username)
embed.addField('User ID', user.id)
embed.addField('User Tag', user.tag)
embed.addField('Roles:', member.roles.cache.map(r => `${r}`).join(' | '))
embed.addField('Created at:', user.createdAt)
embed.setColor(0x00FF93)
embed.setThumbnail(user.avatarURL())
embed.setFooter('Generated by Salty!')
message.channel.send(embed);
break;
First of all, where you opened a case with case "whois"
, there you wouldn't need a colon (:).首先,当您使用case "whois"
打开一个案例时,您不需要冒号 (:)。 Second, It happened due to you naming the member as user
instead of a member or vice-versa.其次,这是因为您将成员命名为user
而不是成员,反之亦然。 It cannot read the property of roles because of the member
before, it is Undefined.由于之前的member
,它无法读取角色的属性,它是未定义的。 If you change the Roles part to embed.addField('Roles:', user.roles.cache.map(r =>
${r} ).join(' | '))
Then it might work, It was all about a const naming problem.如果您将 Roles 部分更改为embed.addField('Roles:', user.roles.cache.map(r =>
${r} ).join(' | '))
那么它可能会起作用,这都是关于常量命名问题。 I hope it helps.我希望它有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.