Home > Net >  Discord bot (JS) messageReactionAdd not working
Discord bot (JS) messageReactionAdd not working

Time:11-02

I am building a Discord bot using JS. Now I want to add reaction roles.

The event messageReactionAdd does not work for me. This is my code:

const { Client, GatewayIntentBits, EmbedBuilder } = require('discord.js')
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildMessageReactions,
  ]
})

client.on('messageReactionAdd', (reaction, user) => {
  console.log('Message react');
});

It looks very simple but I don't know what I am doing wrong.

CodePudding user response:

The code you posted does work on cached messages. The intents are correct, however, if you don't enable partial structures, your code only works on messages 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,
  EmbedBuilder,
  GatewayIntentBits,
  Partials,
} = require('discord.js');

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