I’m facing a problem with the captcha system in my Discord bot. The captcha functionality is properly implemented; however, when attempting to assign a role after successful verification, I receive the following error:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘add’ of undefined
Here’s the code I am currently using:
const Discord = require('discord.js');
const bot = new Discord.Client();
const prefix = '!';
const CaptchaGenerator = require('@haileybot/captcha-generator');
bot.once('ready', () => {
console.log('Bot is ready!');
});
let captchaInstance = new CaptchaGenerator();
bot.on('message', async msg => {
if (!msg.content.startsWith(prefix) || msg.author.bot) return;
const arguments = msg.content.slice(prefix.length).trim().split(/ +/);
const cmd = arguments.shift().toLowerCase();
if (cmd === 'verify') {
let captcha = new CaptchaGenerator();
msg.channel.send(
'**Please type the text shown in the image:**',
new Discord.MessageAttachment(captcha.JPEGStream, 'captcha.png')
);
let collector = msg.channel.createMessageCollector(m => m.author.id === msg.author.id);
collector.on('collect', m => {
if (m.content.toUpperCase() === captcha.value) {
msg.channel.send('Verification successful!');
let verifiedRole = msg.guild.roles.cache.find(role => role.name === 'Verified');
msg.member.roles.add(verifiedRole);
} else {
msg.channel.send('Verification failed!');
}
collector.stop();
});
}
});
bot.login('your-token-here');
Any guidance would be greatly appreciated!