简体   繁体   中英

React to a reaction to get a role (Discord.js)

So, I have this code that supposes to give roles when reacting to the message with specific emoji.

message.channel.send('Message')
.then(function (message) {
  message.react("0️⃣")
});
const filter = (reaction, user) => {
  return reaction.emoji.name === '0️⃣' && user.id === message.author.id;
};

const collector = message.createReactionCollector();

collector.on('collect', (reaction, user) => {
  if (reaction.emoji.name === '0️⃣') role = message.guild.roles.find(role => role.name === "Test Role");
  message.member.addRole(role);
});

But that doesn't work.

Your filter isn't correct. You only need to check if the reaction is the correct one.

I've edited your code by replacing the message variable name to msg. msg defines the message sent. The message where you need to listen to reactions.

Then adapt your filter, and include your whole code inside the if bracket.

message.channel.send('Message').then(msg => {
  msg.react("0️⃣")
  const filter = (reaction, user) => {
    return reaction.emoji.name === '0️⃣';
  };

  const collector = msg.createReactionCollector();

  collector.on('collect', (reaction, user) => {
    if (reaction.emoji.name === '0️⃣'){
       role = message.guild.roles.find(role => role.name === "Test Role");
       message.member.addRole(role);
    }
  });
});

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