How can I make a Reaction Registration Bot Discord.js

Asked

Viewed 5,055 times

1

I was wondering how can I make a function on my bot with Discord.js registration by reactions added in a certain message?

I saw some tutorials using the object raw but none currently works. My code currently looks like this:

client.on('raw', async dados => {
    if(dados.t !== "MESSAGE_REACTION_ADD" && dados.t !== "MESSAGE_REACTION_REMOVE") return
    if(dados.d.message_id != "566982078625873931") return

    let servidor = client.guilds.get("558703169903788057")
    let membro = servidor.members.get(dados.d.user_id)

    let cargo = servidor.roles.get('566986334242340864'),


    if(dados.t === "MESSAGE_REACTION_ADD"){
        if(dados.d.emoji.id === "566966275578789888"){
            if(membro.roles.has(cargo)) return
            membro.addRole(cargo)
        }
    }
    if(dados.t === "MESSAGE_REACTION_REMOVE"){
        if(dados.d.emoji.id === "566966275578789888"){
            if(membro.roles.has(cargo)) return
            membro.removeRole(cargo)
        }
    }

})

*Levando em conta que estou usando a versão 12 do Discord.js, e esse código foi feito na versão 11*

2 answers

0


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

  • Now we can use these declared attributes!

  • Within the event we need to identify the message that we want this event to work this way:

    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:
  1. The emojis normais, the ones that can be used on any example site: "" Discord treats them as names.
  2. 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:
  1. Go to the server chat and use \<Emoji>
  2. 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 === "") {

            }
        }

})

  • Within that last if add:
//...
            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!

  • Within that else:
//...
                    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

-2

you took the PR code?

you must cache before the get. ex: Let server = client.guilds.cache.get("558703169903788057")

in Discord.js v12 is no longer used addRole but roles.add. ex: member.roles.add(position) or member.roles.remove(position)

and that if(member.roles.has(post)) Return I took and it worked smoothly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.