Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- client.on('raw', packet => {
- // We don't want this to run on unrelated packets
- if (!['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)) return;
- // Grab the channel to check the message from
- const channel = client.channels.get(packet.d.channel_id);
- // There's no need to emit if the message is cached, because the event will fire anyway for that
- if (channel.messages.has(packet.d.message_id)) return;
- // Since we have confirmed the message is not cached, let's fetch it
- channel.fetchMessage(packet.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 = packet.d.emoji.id ? `${packet.d.emoji.name}:${packet.d.emoji.id}` : packet.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(packet.d.user_id, client.users.get(packet.d.user_id));
- // Check which type of event it is before emitting
- if (packet.t === 'MESSAGE_REACTION_ADD') {
- client.emit('messageReactionAdd', reaction, client.users.get(packet.d.user_id));
- }
- if (packet.t === 'MESSAGE_REACTION_REMOVE') {
- client.emit('messageReactionRemove', reaction, client.users.get(packet.d.user_id));
- }
- });
- });
- // You can also try to upgrade partials to full instances:
- client.on('messageReactionAdd', async (reaction, user) => {
- if(reaction.message.id != '623729013197766686') return;
- // If a message gains a reaction and it is uncached, fetch and cache the message
- // You should account for any errors while fetching, it could return API errors if the resource is missing
- if (reaction.message.partial) await reaction.message.fetch();
- // Now the message has been cached and is fully available:
- console.log(`${reaction.message.author}'s message "${reaction.message.content}" gained a reaction!`);
- console.log(reaction.message.reactions.size)
- });
- client.on('messageReactionRemove', async (reaction, user) => {
- if(reaction.message.id != '623729013197766686') return;
- // If a message gains a reaction and it is uncached, fetch and cache the message
- // You should account for any errors while fetching, it could return API errors if the resource is missing
- //if (reaction.message.partial) await reaction.message.fetch();
- // Now the message has been cached and is fully available:
- console.log(`${reaction.message.author}'s message "${reaction.message.content}" lost a reaction!`);
- console.log(reaction.message.reactions.size)
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement