messageReactionAdd仅检测缓存的消息

时间:2020-08-01 20:21:06

标签: javascript node.js caching discord discord.js

不幸的是,我发现事件client.on("messageReactionAdd")仅触发对消息的反应,这些消息是在discord.js机器人上线之后编写的。 然后,我应该如何编写反应角色功能或其他内容?

我真的很困。经过研究,我发现了https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/coding-guides/raw-events.md,但仍然在将其复制粘贴到主文件中之后,甚至没有触发缓存的反应:

client.on('raw', packet => {
    // 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);
    // There's no need to emit if the message is cached, because the event will fire anyway for that
    if (channel.messages.has(packet.d.message_id)) return;
    // Since we have confirmed the message is not cached, let's fetch it
    channel.fetchMessage(packet.d.message_id).then(message => {
        // 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;
        // This gives us the reaction we need to emit the event properly, in top of the message object
        const reaction = message.reactions.get(emoji);
        // Adds the currently reacting user to the reaction's users collection.
        if (reaction) reaction.users.set(packet.d.user_id, client.users.get(packet.d.user_id));
        // Check which type of event it is before emitting
        if (packet.t === 'MESSAGE_REACTION_ADD') {
            client.emit('messageReactionAdd', reaction, client.users.get(packet.d.user_id));
        }
        if (packet.t === 'MESSAGE_REACTION_REMOVE') {
            client.emit('messageReactionRemove', reaction, client.users.get(packet.d.user_id));
        }
    });
});

1 个答案:

答案 0 :(得分:3)

您可以使用部分消息来查看在将僵尸程序联机之前发送的消息。

const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });

Source(我建议您阅读)