-
-
Save Danktuary/27b3cef7ef6c42e2d3f5aff4779db8ba to your computer and use it in GitHub Desktop.
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'); |
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
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)
.
You can now use
const user = client.users.cache.get(data.user_id)
.
is this using discord v.12 ?
You can now use
const user = client.users.cache.get(data.user_id)
.is this using discord v.12 ?
Yes, exacly
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?
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
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
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: