-
-
Save Lewdcario/52e1c66433c994c5c3c272284b9ab29c to your computer and use it in GitHub Desktop.
Raw event for master branch
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
// Taken from: https://raw.githubusercontent.com/discordjs/guide/master/code_samples/popular-topics/reactions/raw-event.js | |
const Discord = require('discord.js'); | |
const client = new Discord.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.messages.fetch(data.message_id); | |
const emojiKey = data.emoji.id || data.emoji.name; | |
const reaction = message.reactions.get(emojiKey) || message.reactions.add(data); | |
client.emit(events[event.t], reaction, user); | |
if (message.reactions.size === 1) message.reactions.delete(emojiKey); | |
}); | |
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('your-token-goes-here'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment