discord.js 检查 message.author 是否有角色

时间:2020-12-29 22:06:29

标签: javascript node.js discord discord.js

所以我有这段代码,它一直说属性“缓存”未定义。有没有办法来解决这个问题?这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

message.author 返回一个 User,它没有 roles 属性。您正在寻找的是 message.member,它返回一个 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.