Home > Back-end >  Check if user has a role in the new discord.js version
Check if user has a role in the new discord.js version

Time:12-16

Hey I wanted to check if a user had a role, although all other methods are quite outdated and I've tried a couple of things, none of them work.

My attempt:

var guild = client.guilds.cache.get("Guild ID")
var buyerRole = guild.roles.cache.get("Role ID")
await guild.members.fetch()
const guildMember =  guild.members.cache.get(message.author.id)
if(guildMember.roles.find(buyerRole.id)){...

CodePudding user response:

Okay, so after a bit of debugging, turns out that you need to do message.author._roles and not message.author.roles in order to get the complete list array.

CodePudding user response:

Well, you're going to want to use member.roles.cache.some() to achieve this.

var guild = client.guilds.cache.get("Guild ID")
var buyerRole = guild.roles.cache.get("Role ID")
const guildMember = await guild.members.fetch('User ID');
if(guildMember.roles.cache.some(x => x.id == buyerRole.id)){...

.some will always return a boolean, so it's better for you to use it like that.

  • Related