Cache message discord.js
11,192
What you have to do is first fetch the guild where the channel is in and then get the channel from the guild (so it returns a GuildChannel). Then fetch messages from there.
Full code would be:
client.on('ready', async () => {
const guild = await client.guilds.fetch('guild-id-here');
const channel = guild.channels.cache.get('channel-id-here');
const message = await channel.messages.fetch('message-id-here');
});
client.on('message', message => {
//rest of your code
});
Author by
Mr Brickstar
Updated on June 04, 2022Comments
-
Mr Brickstar almost 2 years
I would like to make some reaction roles. But for that, I have to cache messages which were sent before the bot started. I tried it with
channel.messages.fetch
, but that hasn't worked so far.My current code:
client.on('messageReactionAdd', async(reaction, user) => { client.channels.cache.get("689034237672030230"); channel.messages.fetch('708428887612194979'); // When we receive a reaction we check if the reaction is partial or not if (reaction.partial) { // If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle try { await message.reaction.fetch(); } catch (error) { console.log('Something went wrong when fetching the message: ', error); // Return as `reaction.message.author` may be undefined/null return; } } // Now the message has been cached and is fully available console.log(`${reaction.message.author}'s message "${reaction.message.id}" gained a reaction!`); // The reaction is now also fully available and the properties will be reflected accurately: console.log(`${reaction.count} user(s) have given the same reaction to this message!`); });