Skip to content

Instantly share code, notes, and snippets.

@Danktuary
Last active May 13, 2022 09:43
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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');
@Boxroom
Copy link

Boxroom commented Feb 26, 2018

Hey, there is a TypeError: Cannot read property 'messages' of undefined when reacting with an emoji in a dm that is not cached yet. (There is no cached dm channel after a restart)

Adding this will work:

//...
const { d: data } = event;

if(typeof client.channels.get(data.channel_id) === 'undefined') {
	await client.users.get(data.user_id).createDM();
}

const channel = client.channels.get(data.channel_id);
//...

@hatchwald
Copy link

is const user = client.users.get(data.user_id); deprecated now ? , my bot always return undefined at this moment, moving to v.12 is too hard

@Fabletownn
Copy link

is const user = client.users.get(data.user_id); deprecated now ? , my bot always return undefined at this moment, moving to v.12 is too hard

You can now use const user = client.users.cache.get(data.user_id).

@hatchwald
Copy link

You can now use const user = client.users.cache.get(data.user_id).

is this using discord v.12 ?

@guilhermecapitao
Copy link

You can now use const user = client.users.cache.get(data.user_id).

is this using discord v.12 ?

Yes, exacly

@orwir
Copy link

orwir commented Nov 26, 2020

I guess it is better to use const user = await client.users.fetch(data.user_id).

Btw I have a question regarding reaction object creation. This line const reaction = message.reactions.get(emojiKey);
If the person was the only one who reacted with the specific emoji and if he removed it while you don't have this message in a cache (and it is happens in 100% cases because otherwise you will use appropriate events instead) you won't be able to create a reaction object by resolving it from the message.
Do you know how to fetch or create it in this case?

@orwir
Copy link

orwir commented Nov 26, 2020

Okay, I've checked the sources of discord.js and found out it myself. For whoever faced the same issue as I here is the solution:

const snowflake = event.d.emoji.id ? `${event.d.emoji.name}:${event.d.emoji.id}` : event.d.emoji.name
const reaction = message.reactions.resolve(snowflake) || new MessageReaction(bot, event.d, message)

p.s. discord.js v12.5.0

@bulutgungoren
Copy link

Okay, I've checked the sources of discord.js and found out it myself. For whoever faced the same issue as I here is the solution:

const snowflake = event.d.emoji.id ? `${event.d.emoji.name}:${event.d.emoji.id}` : event.d.emoji.name
const reaction = message.reactions.resolve(snowflake) || new MessageReaction(bot, event.d, message)

p.s. discord.js v12.5.0

What did you define the MessageReaction as?

@Danktuary
Copy link
Author

What did you define the MessageReaction as?

@bulut1905 That would be const { MessageReaction } = require('discord.js')

@bulutgungoren
Copy link

When I restart the bot it doesn't read reaction.message.author.id what could be the reason?

@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