Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

javascript - Getting an error in discord.js when I try to add roles through reactions

I am trying to add roles from reactions. I am getting this error. Cannot read property 'add' of undefined. Here is my code below.

client.on('message', message => {
    const listen = message.content; 
    if(message.author.bot) {
        if(message.embeds) {
            const embedMsg = message.embeds.find(msg => msg.title === 'Choose your favorite team!');
            if(embedMsg) {
                message.react('797575748307255316')
                .catch(err => console.error)
            }

        }
        return; 
    }

    if(listen.toLowerCase() === '!claim roles1') {
        const embed = new Discord.MessageEmbed()
            .setTitle("Choose your favorite team!")
            .setColor("BLUE")
            .setDescription("<:76ers:797575748307255316> - Philadeplhia 76ers
"); +
          
            
        message.channel.send(embed)

        return; 
    }
})

    client.on('messageReactionAdd', (reaction, user) => {
    if(user.bot) return; 
    var roleName = reaction.emoji.name; 
    var role = reaction.message.guild.roles.cache.find(role => role.name.toLowerCase() === roleName.toLowerCase());
    var member = reaction.message.guild.members.cache.find(member => member.id === user.id);
    reaction.message.guild.members.fetch(user).then(m => m.roles.add(role).then(member => {
        console.log('Added ' + member.user.username + ' to the ' + role.name + ' role.');
    })) 
    if(member.guild.roles.cache.has(role.id)) {
        reaction.message.guild.members.fetch(user).then(m => m.roles.remove(role).then(member => {
            console.log('Removed ' + member.user.username + ' from the ' + role.name + ' role.');
        })) 
    }

})

I use Discord.MessageEmbed in order to display it nicely and it is supposed to make the user get a role by them reacting to the reaction.

question from:https://stackoverflow.com/questions/65649528/getting-an-error-in-discord-js-when-i-try-to-add-roles-through-reactions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think it's because you are using .some() which only returns true or false. Use .find() instead in both cases (role and member).

Edit:
Try this:

client.on('messageReactionAdd', (reaction, user) => {
    if(user.bot) return; 
    var roleName = reaction.emoji.name; 
    var role = reaction.message.guild.roles.cache.find(role => role.name.toLowerCase() === roleName.toLowerCase());
    reaction.message.guild.members.fetch(user).then(m => m.roles.add(role).then(member => {
        console.log('Added ' + member.user.username + ' to a role.');
    }))
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...