简体   繁体   English

Discord.js 为 ReactionRole 添加一次反应收集器

[英]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).我以权限 = 8(管理员)将机器人添加到服务器。

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 :您可以通过添加像这样的catch块来验证这一点:

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

Also undefined is a falsy object so同样undefined是一个虚假的对象,所以

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 !希望这对你有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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