Skip to content

Instantly share code, notes, and snippets.

@Danktuary
Last active May 13, 2022 09:43
Show Gist options
  • Save Danktuary/27b3cef7ef6c42e2d3f5aff4779db8ba to your computer and use it in GitHub Desktop.
Save Danktuary/27b3cef7ef6c42e2d3f5aff4779db8ba to your computer and use it in GitHub Desktop.
Raw event reaction add example
const { Client } = require('discord.js');
const client = new Client();
client.on('ready', () => {
console.log('Ready!');
});
const events = {
MESSAGE_REACTION_ADD: 'messageReactionAdd',
MESSAGE_REACTION_REMOVE: 'messageReactionRemove',
};
client.on('raw', async event => {
// `event.t` is the raw event name
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 the message is already in the cache, don't re-emit the event
if (channel.messages.has(data.message_id)) return;
// if you're on the master/v12 branch, use `channel.messages.fetch()`
const message = await channel.fetchMessage(data.message_id);
// custom emojis reactions are keyed in a `name:ID` format, while unicode emojis are keyed by names
// if you're on the master/v12 branch, custom emojis reactions are keyed by their ID
const emojiKey = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name;
const reaction = message.reactions.get(emojiKey);
client.emit(events[event.t], reaction, user);
});
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('pleasedonthackme');
const { Client } = require('discord.js');
const client = new 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.fetchMessage(data.message_id);
const emojiKey = (data.emoji.id) ? `${data.emoji.name}:${data.emoji.id}` : data.emoji.name;
const reaction = message.reactions.get(emojiKey);
client.emit(events[event.t], reaction, user);
});
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('pleasedonthackme');
@orwir
Copy link

orwir commented Dec 22, 2020

@bulut1905, did you fetch the message?
const message = await channel.messages.fetch(event.d.message_id)

@Ladvace
Copy link

Ladvace commented Dec 25, 2020

client.on('messageReactionAdd', (reaction, user) => {
	console.log(`${user.username} reacted with "${reaction.emoji.name}".`);
});

user.username it's undefined

@RisedSky
Copy link

Don't work

@Danktuary
Copy link
Author

@RisedSky This gist was made for discord.js v11, but should no longer be used. Please upgrade to discord.js v12 and use partial structures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment