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'); |
@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
Don't work