discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

request for users on version 11 #3941

Closed sojohnnysaid closed 4 years ago

sojohnnysaid commented 4 years ago

Is your feature request related to a problem? Please describe. I work with a few people remotely. We share our environment using repl.it This allows us to spin up our workstations and do stuff together FAST.

but repl.it isn't ready for node V12 yet so we're on 10. We aren't able to use the latest version of d.js

Specifically it is really painful dealing with the messageReactionRemoveEmoji because the event doesn't pass the reaction AND the user like messageReactionAdd

Describe the ideal solution patch 11 so updating the state of an embed based on reactions is easier

Describe alternatives you've considered patch 11 so messageReactionRemoveEmoji passes the user as well as the reaction

Additional context This library is amazing thank you for such a great tool =)

RDambrosio016 commented 4 years ago

I am not quite sure what you mean, because the event does in fact pass the reaction (see 11.6.2 docs). As for the users, you can get it from the MessageReaction object passed in.

sojohnnysaid commented 4 years ago

I mean to say, when listening for someone adding a reaction, I can use the user that gets passed to the callback (messageReactionAdd pass both reaction and user)

So in my case I need to know who is coming and going so I can update the embed.

I'm struggling to figure out a way to get the user who is leaving. messageReactionRemoveEmoji only passes the reaction

Hope that makes sense. If there is something I am overlooking I apologize.

RDambrosio016 commented 4 years ago

messageReactionRemoveEmoji means all users were removed lol, the users who left (were removed) are the ones included in the reaction

sojohnnysaid commented 4 years ago

ohhh lol

sojohnnysaid commented 4 years ago

Any idea how best to accomplish what i'm after in version 11? Am I using the wrong event then?

(if someone clicks an emoji to remove themselves, I can get access to that user and do stuff)

RDambrosio016 commented 4 years ago

It sounds to me like you want messageReactionRemove instead, which emits when a user removes their reaction from a cached message

sojohnnysaid commented 4 years ago

Thank you! I will try that. Now I have to nudge repl to update node to V12 =)

sojohnnysaid commented 4 years ago

How long before a message is cached?

RDambrosio016 commented 4 years ago

Its not a question of how long, its a question of what you are doing with channels, new messages will be cached and fetched messages will be cached, but there is a possibility of them being sweeped, if you want to get rid of that behavior you can set messageCacheMaxSize to -1 in the client options, however you will have to sweep yourself or your memory usage will climb. It is overall easier and better to use Partials with v12.

sojohnnysaid commented 4 years ago

Oh I see. I've tried using the messageReactionRemove event before but for some reason it doesn't respond. messageReactionAdd always works so I guess that is a caching issue.

sojohnnysaid commented 4 years ago

relevant: https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/coding-guides/raw-events.md

RDambrosio016 commented 4 years ago

Yes the raw event is a thing, but its not reccomended. it is undocumented and unsupported. You can use it but at your own risk ⚠️

sojohnnysaid commented 4 years ago

I just wanted to add that I got it working by checking for the cached message ONLY in the add event not the remove ( I think there is something weird going on ). I'm on version 11 of discord js and node 10.

Here is what worked:

Client.on('raw', (response) => {
  // guard
  if (!['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(response.t)) return

  const channel = Client.channels.get(response.d.channel_id)

  channel.fetchMessage(response.d.message_id).then(message => {
    // Emojis can have identifiers of name:id format, so we have to account for that case as well
    const emoji = response.d.emoji.id ? `${response.d.emoji.name}:${response.d.emoji.id}` : response.d.emoji.name
    // This gives us the reaction we need to emit the event properly, in top of the message object
    const reaction = message.reactions.get(emoji)
    // Adds the currently reacting user to the reaction's users collection.
    if (reaction) reaction.users.set(response.d.user_id, Client.users.get(response.d.user_id))
    // Check which type of event it is before emitting
    if (response.t == 'MESSAGE_REACTION_ADD') {
        if (channel.messages.has(response.d.message_id)) return
        Client.emit('messageReactionAdd', reaction, Client.users.get(response.d.user_id))
    }
    if (response.t == 'MESSAGE_REACTION_REMOVE') {
        Client.emit('messageReactionRemove', reaction, Client.users.get(response.d.user_id))
    }
  })
})