简体   繁体   中英

Discord.js Add Once Reaction Collector for ReactionRole

I'm trying to add a reaction role function to my bot. That means people join my server and have to respond to a message with emojis to get appropriate roles for channels.

I added the bot to the server with permission = 8 (administrator).

Here is the log:

Ready!
Emoji is 👍
Try to add Role Online to User *User*
Added *User*
(node:18801) UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions
    at item.request.gen.end (/home/ubuntu/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15)
    at then (/home/ubuntu/node_modules/snekfetch/src/index.js:215:21)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:18801) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
(node:18801) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

And here is my code:

client.once('ready', () => {

    let messageID = '669575028605583390'
    let guild = client.guilds.first()
    let welcomeChannel = guild.channels.find(c => c.name === 'rulesNroles')
    if (!welcomeChannel) return console.log("Couldn't find welcome channel.");
    welcomeChannel.fetchMessage(messageID).then( message => {

        const filter = (reaction) => {
            return reaction.emoji.name === '👍';
        };

        const collector = message.createReactionCollector(filter, { });

        collector.on('collect', (reaction, reactionCollector) => {
            try {
                    console.log(`Emoji is ${reaction.emoji.name}`) 
                    let role = guild.roles.find(r => r.name === 'Online');
                    reaction.users.forEach(u => {
                        if (u != client.user) {
                            console.log(`Try to add Role ${role.name} to User ${u.username}`) 
                            try {
                                let member = guild.members.find(gm => gm.user.id === u.id);
                                let addedMember = member.addRole(role);
                                if (typeof addedMember != 'undefined') console.log(`Added ${member.user.username}`) 
                            } catch(e) {
                                //console.log(e.stack);
                            }
                        }
                    });
            } catch(e) {
                console.log(e.stack);
            }
        });

        collector.on('end', () => console.log('ended'));

    });

    console.log('Ready!');

    return;
});

What am I doing wrong here? Hope you can help me :)

Your bot has not the permission to add Roles. You can verify this by adding a catch block like this :

let addedMember = member.addRole(role).catch(e => {
    console.log(e);
});

Also undefined is a falsy object so

if(addedMember) {

Is the same thing as

if(typeof addedMember === undefined) {

And as well the same thing as

if(addedMember === undefined) {

Edit :

Your bot cannot add roles that are higher than his higher role in the roles list so check that also.

Hope this helps you !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM