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

时间:2020-01-22 21:58:03

标签: javascript bots discord discord.js

我正在尝试向我的机器人添加反应角色函数。 这意味着人们必须加入我的服务器,并且必须使用表情符号来响应消息,才能获得适当的渠道角色。

我以权限= 8(管理员)将漫游器添加到服务器。

这是日志:

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.

这是我的代码:

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;
});

我在这里做错了什么? 希望你能帮助我:)

1 个答案:

答案 0 :(得分:1)

您的漫游器无权添加角色。 您可以通过添加一个catch块来验证这一点:

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

undefined也是一个伪造的对象,所以

if(addedMember) {

相同
if(typeof addedMember === undefined) {

相同
if(addedMember === undefined) {

编辑:

您的漫游器无法在角色列表中添加高于其较高角色的角色,因此也请进行检查。

希望这对您有帮助!