是否有可能获得一组对消息做出特定表情符号反应的用户? (不和谐 JS)

Is it possible to get a collection of users that reacted with a specific emoji to a message? (Discord JS)

我正在开发高级 Discord Bot。一些功能通过 cron 作业工作。我正在开发的一项新功能要求机器人获取一组用户,这些用户对使用特定表情符号的特定消息做出反应。基于此,用户将获得临时角色(很可能拥有相同类型的问题,因为它的文档与当前问题相同)。

我拥有提取特定消息及其内容所需的所有数据,但我不知道如何通过 cron 访问它。

我有 Discord 服务器 ID、Discord 频道 ID、Discord 消息 ID 和表情符号雪花。所以理论上我应该能够通过 API 访问它的所有内容。但是,我注意到大多数功能都是通过缓存数据实现的,并且仅在事件发生时才实现。

它所做的一项类似的 cron 工作是将每日概览发送到启用它的特定频道。 client.channels.get([channel id]).send([formatted data]);

使用此功能,我需要获取一条消息,以及对表情符号雪花做出反应(理想情况下)的所有用户的集合。

它必须通过 cron,使用 discord 命令是不行的,因为自动化是关键。它也应该在没有任何缓存数据的情况下工作(真正的问题所在)。再一次,我拥有拉取消息和查找基于雪花的表情符号所需的所有数据,我只是不知道如何通过 cron 来完成。该机器人还对其运行的服务器拥有所有必需的权限。

如有任何指点或解决方案,我们将不胜感激。

cronjob 中的当前代码:

    (async () => {
        // walk through channels in DB
        var channels = await query([query]);
        for (var i = 0; i < channels.length; i++) {
            var serv_channel_id = channels[i].server_channel_id;
            var disc_channel_id = channels[i].discord_channel_id;
            var disc_server_id = channels[i].discord_server_id;
            
            // walk through messages in DB
            var messages = await query([query]);
            for (var j = 0; j < messages.length; j++) {
                var serv_message_id = messages[i].server_message_id;
                var disc_message_id = messages[i].discord_message_id;

                // walk through reactions in DB
                var reactions = await query([query]);
                for (var k = 0; k < reactions.legnth; k++) {
                    var serv_reaction_id = reactions[i].server_react_id;
                    var r_to_give = reactions[i].role_to_give;

                    // get discord list of users that reacted with emoji
                    // check per user if they have the role, if they dont then assign role
                }
            }
        }
    })();

目前工作比效率更重要。 出于显而易见的原因,我省略了查询,它们不是这个问题的必要信息。

通过原始事件我能够解决这个问题。

我的旧代码已被完全删除,因此可以忽略作为参考 material。

相反,我深入研究了 API 中一个我以前从未见过的领域;原始事件。

如果您想了解更多信息,我强烈建议您从这里开始阅读: https://anidiots.guide/coding-guides/raw-events

为了达到我想要的目的而使用的基本代码

client.on('raw', packet => {
    (async () => {
        // We don't want this to run on unrelated packets
        if (!['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)) return;

        // Grab the channel to check the message from
        const channel = client.channels.get(packet.d.channel_id);

        // make sure channel id is in DB otherwise no need to listen
        const check_channel = await query([query]);
        if (!Array.isArray(check_channel) || !check_channel.length) return;

        // make sure the message is in the DB otherwise no need to listen
        const check_message = await query([query]);
        if (!Array.isArray(check_message) || !check_message.length) return;

        channel.fetchMessage(packet.d.message_id).then(message => {
            (async () => {
                // Emojis can have identifiers of name:id format, so we have to account for that case as well
                const emoji = packet.d.emoji.id ? `${packet.d.emoji.name}:${packet.d.emoji.id}` : packet.d.emoji.name;

                // make sure emoji id is in db
                const check_reaction = await query([query]);
                if (!Array.isArray(check_reaction) || !check_reaction.length) return;

                // add and track user
                if (packet.t === 'MESSAGE_REACTION_ADD') {
                    await query([query]);
                }

                // remove user from tracking
                if (packet.t === 'MESSAGE_REACTION_REMOVE') {
                    await query([query]);
                }
            })();
        });
    })();
});

该代码并不完美或干净,但它可以作为那些陷入类似问题的人的基本起点。对于我的特定问题,这就是解决方案。