revoltchat / revolt.js

Modern Typescript library for interacting with Revolt.
https://revolt.js.org
MIT License
216 stars 54 forks source link

bug: MessageReact event crashes with: TypeError: reactions.get is not a function #78

Open face-hh opened 1 year ago

face-hh commented 1 year ago

What happened?

This snippet of code causes the above error:

        case 'MessageReact': {
            const message = client.messages.getOrPartial(event.id);
            if (message) {
                const reactions = message.reactions;

                const set = reactions.get(event.emoji_id);

... when a messageReactionAdd event listener exists:

// ...
this.options.client.on('messageReactionAdd', this.listener);
// ...

This code was used to trigger the error:

                await msg.clearReactions();
                await msg.react(encodeURIComponent('❌'));

Explanation: The event tries to get which reaction was added (?), but, since clearReactions ran, message.reactions is {}, causing the reactions.get is not a function error, since reactions is not a Map anymore.

My temporary fix:

        case 'MessageReact': {
            const message = client.messages.getOrPartial(event.id);
            if (message) {
                let reactions = message.reactions;

                if (!(reactions instanceof Map)) reactions = new Map();

                const set = reactions.get(event.emoji_id);
face-hh commented 1 year ago

In case it helps, this happened on 7.0.0-beta.7.

The same issue seems to occur on the "MessageUnreact" event:

        case 'MessageUnreact': {
            const message = client.messages.getOrPartial(event.id);
            if (message) {
                const set = message.reactions.get(event.emoji_id);

where "message.reactions" is {}, resulting in TypeError: message.reactions.get is not a function, when there are 2 reactions on a message (me & the bot), and I unreact.

edit: fixed it with:

        case 'MessageUnreact': {
            const message = client.messages.getOrPartial(event.id);
            if (message) {
                let reactions = message.reactions;

                if (!(reactions instanceof Map)) reactions = new Map();

                const set = reactions.get(event.emoji_id);