简体   繁体   中英

TypeError: Cannot read property 'cache' of undefined in Discord.js

So, I am coding a Discord bot using Discord.js. 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 . 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 (:). Second, It happened due to you naming the member as user instead of a member or vice-versa. It cannot read the property of roles because of the member before, it is Undefined. 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. I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM