【问题标题】:Discord.js Add Once Reaction Collector for ReactionRoleDiscord.js 为 ReactionRole 添加一次反应收集器
【发布时间】:2020-05-09 03:38:42
【问题描述】:

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

我以权限 = 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;
});

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

【问题讨论】:

    标签: javascript bots discord discord.js


    【解决方案1】:

    您的机器人没有添加角色的权限。 您可以通过像这样添加catch 块来验证这一点:

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

    另外undefined 是一个假对象,所以

    if(addedMember) {
    

    是一样的

    if(typeof addedMember === undefined) {
    

    if(addedMember === undefined) {
    

    编辑:

    您的机器人无法在角色列表中添加高于其更高角色的角色,因此请同时检查。

    希望对你有帮助!

    【讨论】:

    • 如前所述,我添加了具有权限管理员的机器人。这些是最高权利。你认为我应该使用什么许可?并感谢您的代码!搜索类似的东西:)
    • 您需要检查角色位置。如果给定角色的位置高于您的机器人最高角色,您将收到错误消息。您也可以使用role.editable 进行检查
    猜你喜欢
    • 2021-06-16
    • 2023-03-18
    • 2020-11-30
    • 1970-01-01
    • 2021-08-05
    • 2020-07-06
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多