Search code examples
discorddiscord.jsroles

Verification system in discord.js


Basically when I do console.log(message.author.roles) it just says its undefined and from there im lost please help me

const roleId = config.verify.roleId // 997167483473629324
const channelId = config.verify.channelId // 997167450024067194
const user = message.author
if (message.channel.id === channelId) {
    if (!user.roles.cache.has(roleId)) { // "roles" is undefined??
        message.reply({ embeds: [new Discord.MessageEmbed().setColor('RED').setTitle('Verification').setDescription('Please wait.')] })
        setTimeout(() => {
            user.roles.add(roleId) // would probably be the same error here
        }, 1500)
    }
}

Solution

  • When using message.author, you get a User object which represents the global user. Instead, to access the roles property, you need to use a GuildMember object which represents a user in a guild. So, all you have to do to check all the roles a member has is by using one of these two methods:

    • Fetching the member: If you want to fetch the member, you can get the user id by using message.author.id and then, fetch it from the guild. An example:
    const userId = message.author.id
    const member = message.guild.members.cache.get(userId) // or await message.guild.members.fetch(userId)
    console.log(member.roles)
    
    • message.member: A much easier way is by using this method. By using this, you automatically get the GuildMember details of the user who posted this message. An example:
    const member = message.member
    console.log(member.roles)