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?