Is there a better way to cache a message indefinitely?

Viewed 45

I have a message that people can add/remove reactions to. Once the user adds a reaction, it updates an embed in a separate channel. My bot will detect these reactions for the first ~30 min or so after the message is sent before completely ignoring all further reactions to that message. To solve that, I created a function that runs every 15 mins to cache the message using channel.messages.fetch(). I’m dying to know if there is a better way to achieve an indefinitely cached message, preferably without the use of a setInterval() function. Thanks!

1 Answers

You shouldn't try and cache the message indefinitely - the bot still gets informed of all message reaction events - it's just discord.js by default only does it for cached messages, you can listen to them by enabling partial events.

You need to enable the partial events for Message, Channel and Reaction - all three are needed to get the reaction events.

import { Client, GatewayIntentBits, Partials } from 'discord.js';
const client = new Client({
    intents: [...],
    partials: [Partials.Message, Partials.Channel, Partials.Reaction]

})

This will result in MessageReactionEvent being ran when there is sometimes insuffucient data and it could result in errors - to check if the event is a Partial event (not all data is guaranteed) or a Full Event (all data for the structure will be present) you can just check the .partial property on the reaction.

However, doing this will allow you to listen for Message Reaction Events on uncached messages.

Related