Skip to content

Instantly share code, notes, and snippets.

@Lewdcario
Last active June 9, 2019 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lewdcario/52e1c66433c994c5c3c272284b9ab29c to your computer and use it in GitHub Desktop.
Save Lewdcario/52e1c66433c994c5c3c272284b9ab29c to your computer and use it in GitHub Desktop.
Raw event for master branch
// Taken from: https://raw.githubusercontent.com/discordjs/guide/master/code_samples/popular-topics/reactions/raw-event.js
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('Ready!');
});
const events = {
MESSAGE_REACTION_ADD: 'messageReactionAdd',
MESSAGE_REACTION_REMOVE: 'messageReactionRemove',
};
client.on('raw', async event => {
if (!events.hasOwnProperty(event.t)) return;
const { d: data } = event;
const user = client.users.get(data.user_id);
const channel = client.channels.get(data.channel_id) || await user.createDM();
if (channel.messages.has(data.message_id)) return;
const message = await channel.messages.fetch(data.message_id);
const emojiKey = data.emoji.id || data.emoji.name;
const reaction = message.reactions.get(emojiKey) || message.reactions.add(data);
client.emit(events[event.t], reaction, user);
if (message.reactions.size === 1) message.reactions.delete(emojiKey);
});
client.on('messageReactionAdd', (reaction, user) => {
console.log(`${user.username} reacted with "${reaction.emoji.name}".`);
});
client.on('messageReactionRemove', (reaction, user) => {
console.log(`${user.username} removed their "${reaction.emoji.name}" reaction.`);
});
client.login('your-token-goes-here');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment