Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. client.on('raw', packet => {
  2. // We don't want this to run on unrelated packets
  3. if (!['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)) return;
  4. // Grab the channel to check the message from
  5. const channel = client.channels.get(packet.d.channel_id);
  6. // There's no need to emit if the message is cached, because the event will fire anyway for that
  7. if (channel.messages.has(packet.d.message_id)) return;
  8. // Since we have confirmed the message is not cached, let's fetch it
  9. channel.fetchMessage(packet.d.message_id).then(message => {
  10. // Emojis can have identifiers of name:id format, so we have to account for that case as well
  11. const emoji = packet.d.emoji.id ? `${packet.d.emoji.name}:${packet.d.emoji.id}` : packet.d.emoji.name;
  12. // This gives us the reaction we need to emit the event properly, in top of the message object
  13. const reaction = message.reactions.get(emoji);
  14. // Adds the currently reacting user to the reaction's users collection.
  15. if (reaction) reaction.users.set(packet.d.user_id, client.users.get(packet.d.user_id));
  16. // Check which type of event it is before emitting
  17. if (packet.t === 'MESSAGE_REACTION_ADD') {
  18. client.emit('messageReactionAdd', reaction, client.users.get(packet.d.user_id));
  19. }
  20. if (packet.t === 'MESSAGE_REACTION_REMOVE') {
  21. client.emit('messageReactionRemove', reaction, client.users.get(packet.d.user_id));
  22. }
  23. });
  24. });
  25. // You can also try to upgrade partials to full instances:
  26. client.on('messageReactionAdd', async (reaction, user) => {
  27. if(reaction.message.id != '623729013197766686') return;
  28. // If a message gains a reaction and it is uncached, fetch and cache the message
  29. // You should account for any errors while fetching, it could return API errors if the resource is missing
  30. if (reaction.message.partial) await reaction.message.fetch();
  31. // Now the message has been cached and is fully available:
  32. console.log(`${reaction.message.author}'s message "${reaction.message.content}" gained a reaction!`);
  33. console.log(reaction.message.reactions.size)
  34. });
  35.  
  36. client.on('messageReactionRemove', async (reaction, user) => {
  37. if(reaction.message.id != '623729013197766686') return;
  38. // If a message gains a reaction and it is uncached, fetch and cache the message
  39. // You should account for any errors while fetching, it could return API errors if the resource is missing
  40. //if (reaction.message.partial) await reaction.message.fetch();
  41. // Now the message has been cached and is fully available:
  42. console.log(`${reaction.message.author}'s message "${reaction.message.content}" lost a reaction!`);
  43. console.log(reaction.message.reactions.size)
  44. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement