【问题标题】:discord.js check if message.author has rolediscord.js 检查 message.author 是否有角色
【发布时间】:2021-04-06 12:15:07
【问题描述】:
所以我有这段代码,它一直说属性“缓存”是未定义的。有没有办法来解决这个问题?这是我的代码:
const doggorole = message.guild.roles.fetch("role-id");
if (message.author.roles.cache.get(doggorole.id)) {
// Code Here
}
【问题讨论】:
标签:
javascript
node.js
discord
discord.js
【解决方案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.