Discord.js Role Assignment Error: Cannot read property 'add' of undefined in Verification System

I’m working on a Discord bot verification system and running into a frustrating problem. My bot generates captcha images correctly and users can solve them, but when I try to assign a role after successful verification, I get this error:

TypeError: Cannot read property 'add' of undefined

Here’s my code:

const Discord = require('discord.js');
const bot = new Discord.Client();
const cmdPrefix = '!';
const CaptchaGen = require("@haileybot/captcha-generator");

bot.once('ready', () => {
    console.log('Bot is online!');
});

bot.on('message', async msg => {
    if (!msg.content.startsWith(cmdPrefix) || msg.author.bot) return;

    const arguments = msg.content.slice(cmdPrefix.length).trim().split(/ +/);
    const cmd = arguments.shift().toLowerCase();

    if (cmd === 'verify') {
        let puzzle = new CaptchaGen();
        msg.channel.send(
            "**Please type the characters you see in the image:**",
            new Discord.MessageAttachment(puzzle.JPEGStream, "verification.jpeg")
        );
        
        let msgCollector = msg.channel.createMessageCollector(m => m.author.id === msg.author.id);
        
        msgCollector.on("collect", m => {
            if (m.content.toUpperCase() === puzzle.value) {
                msg.channel.send("Success! You are now verified!");
                let memberRole = msg.guild.roles.cache.find(r => r.id === "VerifiedUser");
                msg.author.roles.add(memberRole);
            } else {
                msg.channel.send("Incorrect! Please try again.");
            }
            msgCollector.stop();
        });
    }
});

bot.login('token_here');

The captcha generation works perfectly but the role assignment keeps failing. Can someone help me figure out what’s wrong?

You’re using msg.author instead of msg.member - that’s the problem. Author objects don’t have role properties. Also, double-check that your role search is actually finding something. Throw in a console.log(memberRole) before the add() call to see if it’s coming back undefined.

You’re calling roles.add() on a User object when you need a GuildMember object. msg.author gives you the User, but role management needs the GuildMember instance. Change msg.author.roles.add(memberRole) to msg.member.roles.add(memberRole) and that’ll fix your error. Also, you’re searching for a role with ID “VerifiedUser” but that looks like a role name, not a Discord ID. Role IDs are numeric strings, so either use the correct ID or change your search to find(r => r.name === "VerifiedUser") if that’s the actual role name you want.

You’re trying to access roles on a User object instead of a GuildMember object. msg.author gives you a User instance, which doesn’t have role management. Use msg.member instead - that’s the GuildMember object with the roles property. Also, your role finding logic has an issue. You’re searching with r.id === "VerifiedUser" but “VerifiedUser” looks like a role name, not a Discord role ID. Role IDs are long numeric strings like “123456789012345678”. If “VerifiedUser” is the role name, use r.name === "VerifiedUser" instead. If you meant to use the role ID, swap “VerifiedUser” for the actual numeric ID from your server. Your corrected line should be: msg.member.roles.add(memberRole);

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.