Discord.js: messageReactionRemove event does not trigger

Created on 1 Nov 2018  Â·  8Comments  Â·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:
If you put reaction on a message and then log in with the bot, the messageReactionRemove event not gonna happen when you remove your reaction from this message even if you cache this message.
While trying 'raw' event I found out that message.reactions is empty after fetching message from there and thus event emitter sends undefined reaction in this case.
However if you add reaction with the bot online it all works as expected.

Include a reproducible code sample here, if possible:

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('messageReactionRemove', (reaction, user) => {
    console.log(`${user.username} removed their "${reaction.emoji.name}" reaction.`);
});

client.on('ready', async () => {
    console.log('Ready!');

    let channel = client.channels.get('channel-with-message-id');
    let msgId = 'message-with-pre-added-reaction-id';

    await channel.fetchMessage(msgId);
    if (channel.messages.has(msgId)) {
        console.log('Cached!');
    }
});

client.login(token);

Further details:

  • discord.js version: 11.4.2
  • Node.js version: 10.4.1
  • Operating system: Windows 10 x64
  • Priority this issue should have – please be realistic and elaborate if possible: low.
caching gateway

Most helpful comment

Seems like this should be reopened because it still happens. Even if you cache the message manually, it doesn't emit the event unless you unreact, react, and unreact again.

Even in the example of looking at raw packets that @pimhakkert posted, it says "// There's no need to emit if the message is cached, because the event will fire anyway for that", yet clearly it wont. I have confirmed that the code in that works, but you MUST remove that line that checks if it's cached, otherwise it will skip "cached" messages, and not emit the event, just like it does without this script.

All 8 comments

Though this is being addressed in #1376, discord.js currently only emits the messageReaction events on cached messages, meaning they need to be sent while the client is online, or manually cached. (For now,) This is working as intended

You are wrong. Just take a look at the code sample: I cache the message manually in order to show it's not working as intended.

This has been addressed in https://github.com/discordjs/discord.js/pull/3070, please try out the PR and tell me how it works for you!

Closing due to inactivity

@amishshah
messageReactionRemove still doesn't trigger on fetched messages if you put reactions on them before bot restarting

@amishshah
messageReactionRemove still doesn't trigger on fetched messages if you put reactions on them before bot restarting

You can fix this by manually listening for the raw events and then emitting messageReactionAdd and messageReactionRemove events.

I used the template code at this link and it works perfectly.

Seems like this should be reopened because it still happens. Even if you cache the message manually, it doesn't emit the event unless you unreact, react, and unreact again.

Even in the example of looking at raw packets that @pimhakkert posted, it says "// There's no need to emit if the message is cached, because the event will fire anyway for that", yet clearly it wont. I have confirmed that the code in that works, but you MUST remove that line that checks if it's cached, otherwise it will skip "cached" messages, and not emit the event, just like it does without this script.

You should have something like that in your code

bot.on('raw', async event => {
    if (!events.hasOwnProperty(event.t)) return;

    const { d: data } = event;
    const user = bot.users.get(data.user_id);
    const channel = bot.channels.get(data.channel_id) || await user.createDM();

    if (channel.messages.has(data.message_id)) return;

    const message = await channel.fetchMessage(data.message_id);
    const emojiKey = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name;
    let reaction = message.reactions.get(emojiKey);

    if (!reaction) {
        const emoji = new Discord.Emoji(bot.guilds.get(data.guild_id), data.emoji);
        reaction = new Discord.MessageReaction(message, emoji, 1, data.user_id === bot.user.id);
    }

    bot.emit(events[event.t], reaction, user);
});

however this line

if (channel.messages.has(data.message_id)) return;

prevent event like MESSAGE_REACTION_REMOVE
from being emit
in my case i just comment this line and hope for the best

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BrandonCookeDev picture BrandonCookeDev  Â·  3Comments

Brawaru picture Brawaru  Â·  3Comments

Acaretia picture Acaretia  Â·  3Comments

Alipoodle picture Alipoodle  Â·  3Comments

Dmitry221060 picture Dmitry221060  Â·  3Comments