简体   繁体   English

TypeError:无法读取 Discord.js 中未定义的属性“缓存”

[英]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.

相关问题 未捕获的 TypeError TypeError:无法读取未定义 Discord.js 的属性“缓存” - Uncaught TypeError TypeError: Cannot read property 'cache' of undefined Discord.js 类型错误:无法读取未定义的属性(读取“缓存”)(discord.js) - TypeError: Cannot read properties of undefined (reading 'cache') (discord.js) 无法读取 discord.js 中未定义的属性“缓存” - Cannot read property 'cache' of undefined in discord.js Discord.js:无法读取未定义的属性“缓存” - Discord.js: Cannot read property “cache” of undefined 类型错误:无法读取未定义 Discord.js javascript 的属性“添加” - TypeError: Cannot read property 'add' of undefined Discord.js javascript 类型错误:无法读取未定义的属性 'has' // Discord.js - TypeError: Cannot read property 'has' of undefined // Discord.js 遇到 TypeError:无法读取 Discord.JS 中未定义的属性“0” - Encountering TypeError: Cannot read property '0' of undefined in Discord.JS Discord.JS UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“startsWith” - Discord.JS UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined TypeError:无法读取未定义的属性“数据”| discord.js - TypeError: Cannot read property 'data' of undefined | discord.js 类型错误:无法读取未定义的属性“角色”|| Discord.js - TypeError: Cannot read property 'roles' of undefined || Discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM