Last active
May 13, 2022 09:43
-
-
Save Danktuary/27b3cef7ef6c42e2d3f5aff4779db8ba to your computer and use it in GitHub Desktop.
Raw event reaction add example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
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?
What did you define the MessageReaction as?
@bulut1905 That would be const { MessageReaction } = require('discord.js')
When I restart the bot it doesn't read reaction.message.author.id what could be the reason?
@bulut1905, did you fetch the message?
const message = await channel.messages.fetch(event.d.message_id)
client.on('messageReactionAdd', (reaction, user) => {
console.log(`${user.username} reacted with "${reaction.emoji.name}".`);
});
user.username it's undefined
Don't work
@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
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:
p.s. discord.js v12.5.0