First, for what reason do you want to manipulate these reactions?
Actually there are several reasons why you want to use reaction manipulation, I’ll list some:
- Create a record with reactions (This is surely one of the most sought after, and most used motives).
- Opens a range of options for making confirmations (In reaction triggers you can nest any function, either confirmation or execution of some function).
What You Need to Know?
- I’m taking into account that you already have one
bot
functional using Discord.js, if not before using this answer read the guide of Discord.js (This guide there are all the tutorials for you to have a bot at least basic and functional).
Partials
- It is a recently added function, in version 12 of Discord.js, before to create this type of interaction with reactions was used the method raw, this method still exists, but how reactions in old messages are not cached and the raw is an event susceptible to many errors is not ideal to use this method. (It is probably no longer functional in reaction manipulation).
How a partial works:
- First of all it is necessary to activate this partial, since she nay is enabled by default. In your file main replace the
require
of the Discord.js for:
const { Client } = require('discord.js')
const client = new Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] })
There are various structures in the partials
, but for now these are more than necessary. If you want to know more about partials
use the partials guide of Discord.js!
Creating Reactionadd event
- First we have to create the event that will observe these reactions, this event is called:
messageReactionAdd
and we’ll use it that way:
client.on('messageReactionAdd', async (reaction, user) => {
//Código de reação
})
What he’s doing is observing the event of Add Reaction and passing to reaction and the user so we can use for confirmation and future commands.
Using the Partial
- Within this event we must declare
partial
as follows:
if(reaction.partial) {
try {
await reaction.fetch()
} catch (error) {
console.error('Alguma coisa está errada quando tento puxara a reação!')
return
}
}
Using a model of trycatch standard use partial and capture error if necessary.
Declaring the object guild
:
- You also need to have the
guild
set, it will take your server id and turn into an object you can use, you can do this by declaring a constant also within the event:
const guild = client.guilds.cache.get("<ID DO SEU SERVIDOR>")
Declaring the Roles
- As we are doing a registration function by reaction we also have to declare the roles that we will add to the users in this way:
const <nome da role> = guild.roles.cache.get("<ID da role>")
We declare that the role
has this id, taking all properties linked to role
in question. You can do this with an object or an array as well, and you can do as many roles as necessary.
Finding the member who reacted:
- Now we’re going to have to take whoever reacted to the message, it seems simple, just take the
user
of the event and use it right? Not!
The user
brings a Discord user not a guild member, so we should make it a member that way:
const member = reaction.message.guild.member(user)
Here we pass the user id to the function member
identifying the member within the declared guild!
Creating identification of reaction
if(reaction.message.id !== '<ID da Mensagem>') return
else {
}
We used a if else
basic to filter the message we want by id.
- Now we also need to filter the
emoji
that we want, here enter also some addendums:
- The emojis
normais
, the ones that can be used on any example site: "" Discord treats them as names.
- Emojis created within a guild Discord treats them as id’s.
- Inside This is how we’re gonna do it:
//...
else {
if(reaction.emoji.name === "") {
//...
}
}
If in case you are using a custom emoji, do so:
//...
else {
if(reaction.emoji.id === "<ID do Emoji>") {
//...
}
}
- You can get this id this way:
- Go to the server chat and use
\<Emoji>
- He’ll bring you that code:
<a:sintonia_emoji96:665606434595274762>
, use only the numbers of this code. Voilà, you have your id.
- For the final part we will check if the member already has the role and add it, at that point your code should already be like this:
client.on('messageReactionAdd', async (reaction, user) => {
if(reaction.partial) {
try {
await reaction.fetch()
} catch (error) {
console.error('Alguma coisa está errada quando tento puxara a reação!')
return
}
}
const guild = client.guilds.cache.get("796513382060392451");
const role = guild.roles.cache.get("797336187874312232")
const member = reaction.message.guild.member(user)
if(reaction.message.id !== '797336187874312156') return
else {
if(reaction.emoji.name === "") {
}
}
})
//...
if(reaction.emoji.name === "") {
if(member.roles.cache.has(fem)) return
else {
}
}
Now we are checking if the member already has this role, if he owns it responds an empty Return, doing nothing!
//...
else {
member.roles.add(role)
}
- So we finish the code, that being said, if the person reacts to that message with this emoji set, will be added to scroll. To do the reverse, change the event to:
'messageReactionRemove'
, use the same structure and instead of:
member.roles.add(role)
- Use in role confirmation if:
member.roles.remove(role)
- At the end your code should look like this:
Pastebin