KaizOffical
KaizOffical

Reputation: 75

Why bot doesn't fire messageReactionAdd event when the message was sent before the bot was online

This is my event to check user add react:

const client = require("../index");

client.on('messageReactionAdd', async (reaction, user) => {
    if (user.bot) return;
    console.log(reaction.emoji.name);
});

It's working, but when the bot restarts, it can't fetch any message sent before?

Why and how to fix it?

Upvotes: 3

Views: 1158

Answers (2)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23160

If you don't enable partial structures, your code only works on cached messages; ones posted after the bot is connected. Reacting on older messages won't fire the messageReactionAdd event.

If you also want to listen to reactions on old messages you need to enable partial structures for MESSAGE, CHANNEL and REACTION when instantiating your client, like this:

const { Client, Intents } = require('discord.js');

const client = new Discord.Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MEMBERS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
  ],
  partials: ['CHANNEL', 'GUILD_MEMBER', 'MESSAGE', 'REACTION', 'USER'],
});

If you're using discord.js v14, you can use enums like this:

const { Client, GatewayIntentBits, Partials } = require('discord.js');

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMember,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.GuildMessageReactions,
    GatewayIntentBits.MessageContent,
  ],
  partials: [Partials.Channel, Partials.Message, Partials.Reaction],
});

Upvotes: 4

Kaspr
Kaspr

Reputation: 1616

You need partials:

const client = new Client({
    intents: ‘’, // your intents here
    partials: ['CHANNEL', 'GUILD_MEMBER', 'GUILD_SCHEDULED_EVENT', 'MESSAGE', 'REACTION', 'USER'],
});

Upvotes: 1

Related Questions