Skip to content
Advertisement

Collect reaction from dm message triggered by ‘guildMemberAdd’

My bot sends a message when a new member is added to the guild. The message goes to a specific user.

    client.on('guildMemberAdd', member => {
    const adminDm = client.users.cache.get(Config.get('ADMIN'));
    client.commands.get('novoMembro').execute(member, adminDm);
}); 

Now I need to collect the reaction in order to assign some role to the new member.

const Discord = require('discord.js');

module.exports = {
    name: 'novoMembro',
    description: "Adding a new member to the guild",
    execute(member, adminDm){
        const novoMembroEmbed = new Discord.MessageEmbed()
        .setColor([153,0,76])
        .setTitle('NOVO MEMBRO ADICIONADO')
        .setDescription(`<@!${member.id}> foi adicionado`)
        .addFields( 
            {name: 'Selecione uma opção:', value: 'Reaja com 📨 para enviar mensagem de boas vindas n Reaja com ❌ para cancelar'},
        );

        adminDm.send({ embeds: [novoMembroEmbed] }).then((msg => {
            msg.react('❌');
            msg.react('📨');
        }));
        
       
    }
}

Until this point, the code works fine. But I can’t find a way to collect the reactions, every code I’ve tried doesn’t work. I think I didn’t understand right the concept of collecting reactions. these are some code I’ve tried.

const filter = (reaction) => ['❌', '📨'].includes(reaction.emoji.name);
const reactOptions = {maxEmojis: 1};

.then(() => msg.awaitReactions(filter, reactOptions)).then(collected => {
                if (collected.first().emoji.name === '📨') {
                    console.log('msg de boas vindas');
                }else{
                    console.log('cancelando');
                }

Advertisement

Answer

There are a couple of errors with your code. In discord.js v13 the awaitReactions and createReactionCollector accepts a single parameter and the filter is part of the options object now. (See Changes in v13.) So, you need to update that; pass down a single object with a filter and a max or maxEmojis key.

You’ll also need to update your filter as it currently collects the bot’s reactions too. By checking if the user who reacted is the same as the admin, you can make sure you only collect the reactions you need.

You could also make execute async and use the await keyword to wait for the promises to be resolved.

And one last thing; make sure you enabled the required intents: DIRECT_MESSAGES and DIRECT_MESSAGE_REACTIONS.

Check out the code below:

module.exports = {
  name: 'novoMembro',
  description: 'Adding a new member to the guild',
  async execute(member, admin) {
    const novoMembroEmbed = new Discord.MessageEmbed()
      .setColor([153, 0, 76])
      .setTitle('NOVO MEMBRO ADICIONADO')
      .setDescription(`<@!${member.id}> foi adicionado`)
      .addFields({
        name: 'Selecione uma opção:',
        value:
          'Reaja com 📨 para enviar mensagem de boas vindas n Reaja com ❌ para cancelar',
      });

    try {
      const sentDM = await admin.send({ embeds: [novoMembroEmbed] });
      // make sure you don't collect the bot's reactions
      const filter = (reaction, user) =>
        ['❌', '📨'].includes(reaction.emoji.name) && user.id === admin.id;

      sentDM.react('❌');
      sentDM.react('📨');

      // add a single options object only
      const collected = await sentDM.awaitReactions({ filter, maxEmojis: 1 });

      if (collected.first().emoji.name === '📨') {
        admin.send('msg de boas vindas');
      } else {
        admin.send('cancelando');
      }
    } catch (err) {
      console.log(err);
    }
  },
};

enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement