简体   繁体   中英

Someone who can explain me how ReactionCollector function?

Hey i am new with js and discord js and got a problem, i dont understand how reactioncollector works maybe someone here can explain and help me with that code. Its for a Ticket System if the other code is needed ill send it:

.then(async (channel, message) => {
        
          let sent = await channel.send(`<@${user.id}>`, new Discord.MessageEmbed()
            .setTitle("Welcome to your ticket!")
            .setDescription("We will be with you shortly")
            .setColor("00ff00")

          );
          sent.react('📪');
          
            const filter = (reaction, user) => {

              if (reaction.emoji.name === "📪"){
                
              
                reaction.users.remove(user);
                reaction.message.channel.delete();
              }


            return reaction.emoji.name === '📪' && user.id === message.author.id;  
          }
          const collector = message.createReactionCollector(filter, { time: 15000 });

          collector.on('collect', (reaction, user) => {
            console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
          });

          collector.on('end', collected => {
            console.log(`Collected ${collected.size} items`);
          });
              


          });
  
        
        }
      }
    }
)

You need to use the right filter. As I can see, you want to only detect reaction from the message author. So put these restrictions into the filter and then every reaction collected by the collector will respect these conditions.

Now that you have this, just execute whatever you want and stop the collector once everything is done so you will not listen to any other reactions.

let sent = await channel.send(`<@${user.id}>`, new Discord.MessageEmbed()
  .setTitle("Welcome to your ticket!")
  .setDescription("We will be with you shortly")
  .setColor("00ff00")
);
sent.react('📪');

const filter = (reaction, user) => {
  //We filter the collector to only listen to 📪 reaction added by the author of the message.
  return reaction.emoji.name === '📪' && user.id === message.author.id;  
}
const collector = message.createReactionCollector(filter, { time: 15000 });

collector.on('collect', (reaction, user) => {
  console.log(`Collected ${reaction.emoji.name} from ${user.tag}`);
  reaction.users.remove(user);
  reaction.message.channel.delete();
  //Then we stop the collector.
  collector.stop()
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM