Discord.js: messageReactionAdd doesn't work as expected in the master branch

Created on 14 Mar 2017  路  2Comments  路  Source: discordjs/discord.js

forewarning: okay, i spent like 2 hours figuring out why my reaction abstraction for my chat bot didn't work, and my head is aching

I'll keep it as concise as possible:
problem: the "messageReactionAdd" isn't called
probable cause: (as a list of events that leads to the bug)

  1. I send a message via the bot
  2. I add three reactions to the message as the bot
  3. I add a reaction to the bot using my own discord user account, which results in the bot editing the message
  4. the "messageUpdate" is called because of the message edit
  5. the message object is cloned, and Message.patch is called
  6. Two fuck-ups here:
  7. I remove my reaction
  8. message._removeReaction is called
  9. The two fuck ups from before result in:

    • discord.js not being able to find the reaction and failing at if (this.reactions.has(emojiID)) {

    • discord.js falsely concluding that a user hasn't previously reacted to a message and thus failing at if (reaction.users.has(user.id)) {

  10. "messageReactionAdd" is no longer called.

propsed solution: can't make a pull request atm, but you'd have to somehow preserve the list of users of all reactions AND fix up the encodeURIComponent calls

medium already fixed utility bug

Most helpful comment

This should be fixed in my pr #1236 , if you have a moment can you try npm i bdistin/discord.js to insure that pr fixed all issues listed here? If it doesnt let me know, so i can fix any remaining issues.

All 2 comments

This should be fixed in my pr #1236 , if you have a moment can you try npm i bdistin/discord.js to insure that pr fixed all issues listed here? If it doesnt let me know, so i can fix any remaining issues.

after removing my detour of Message.patch and installing your fork of discord.js, the issue is still unresolved.

I'll go debug later today for you and see what's going wrong

for reference, here's my hacky runtime detour to solve the problem

// !!! UNSAFE !!!
try {
    let oldPatch:Function = Discord.Message.prototype["patch"];

    let detouredPatch:(this: Discord.Message,data: any) => void = function(data) {
        let self = this;

        this.reactions["oldClear"] = this.reactions["oldClear"] || this.reactions.clear;

        let reactUsers = {};

        this.reactions.clear = function() {
            this.forEach((reaction,key) => {
                reactUsers[key] = reaction.users;
            });

            self.reactions["oldClear"].apply(this,[]);
        } as (this: Discord.Collection<string,Discord.MessageReaction>) => void;

        if(data.reactions) {
            for(let reaction of data.reactions) {
                reaction.emoji._name = reaction.emoji.name;
                reaction.emoji.name = encodeURIComponent(reaction.emoji.name);
            }
        }

        oldPatch.apply(this,[data]);

        if(data.reactions) {
            for(let reaction of data.reactions) {
                reaction.emoji.name = reaction.emoji._name;
            }
        }

        this.reactions.clear = this.reactions["oldClear"];

        this.reactions.forEach((reaction,key) => {
            if(reactUsers[key]) {
                reaction.users = reactUsers[key];
            }
        });
    }

    Discord.Message.prototype["patch"] = detouredPatch;
}
catch(e) {
    io.error("discord detours failed: " + e.toString());
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

PassTheMayo picture PassTheMayo  路  3Comments

smchase picture smchase  路  3Comments

DatMayo picture DatMayo  路  3Comments

shukriadams picture shukriadams  路  3Comments

ghost picture ghost  路  3Comments