简体   繁体   中英

discord.js check if message.author has role

So I have this code and it keeps saying that property 'cache' is undefined. Is there a way to fix this? Here is my code:

const doggorole = message.guild.roles.fetch("role-id");
if (message.author.roles.cache.get(doggorole.id)) {
    // Code Here
}

message.author returns a User , which has no roles property. What you are looking for is message.member which returns a GuildMember .


const doggorole = await message.guild.roles.fetch("role-id");
// Since RoleManager.fetch returns a Promise you should wait for the response.

if (message.member.roles.cache.has(doggorole)) {
    // Code Here
}

// You should use Collection.has() to see if the role is in GuildMemberRoleManager.cache.

// I suggest using the cache instead of manually fetching the role from the API.

// Note that if you do GuildMemberRoleManager.cache.has("ROLE-ID") you don't need to fetch the role at all.

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