Discord JS // Trying to add role by reacting to the message

陌路散爱 提交于 2020-08-09 07:28:48

问题


bot.on('messageReactionAdd', async (reaction, user) => {
  // Define the emoji user add       
  let role = message.guild.roles.find(role => role.name === 'Alerts');
  if (message.channel.name !== 'alerts') return message.reply(':x: You must go to the channel #alerts');
  message.member.addRole(role);
});

Thats the part of my bot.js. I want the user to react in a certain channel and receive role Alerts


回答1:


You haven't really stated what the problem is, what works and what doesn't work but I'll take a wild stab at some parts which catch my eye.

For starters you are calling properties on the variable message whilst in the code you supplied, you didn't create/set a variable named message. My guess is that you want the message to which a reaction has been added. To do that you have to use the MessageReaction parameter which is supplied in the messageReactionAdd event as reaction.

From there you can replace message.<something> with reaction.message.<something> everywhere in the code you supplied.

 

Something also to note is that you add the role Alerts to message.member. This won't work how you want it to, since it will give the Alerts role to the author of the original message.

What (I think) you want to do, is fetch the user who just reacted with the emoji and assign them the Alerts role. You'll have to find the member in the guild first and then assign them the Alerts role. To do this you'll have to use the User parameter and find the correct Member because you can't add a role to a User object but you can to a Member object. Below is some code which should hopefully put you on the right track.

// Fetch and store the guild (the server) in which the message was send.
const guild = reaction.message.guild;

const memberWhoReacted = guild.members.find(member => member.id === user.id);

memberWhoReacted.addRole(role);



回答2:


Here's a quick answer, though way too late. So I'll be updating the answer with Discord.js v.12.x (or the same as Discord.js Master)

bot.on('messageReactionAdd', async (reaction, user) => {
  //Filter the reaction
  if (reaction.id === "<The ID of the Reaction>") {
    // Define the emoji user add
    let role = message.guild.roles.cache.find(role => role.name === 'Alerts');
    if (message.channel.name !== 'alerts') {
      message.reply(':x: You must go to the channel #alerts');
    } else {
      message.member.addRole(role.id);
    }
  }
});


来源:https://stackoverflow.com/questions/59069737/discord-js-trying-to-add-role-by-reacting-to-the-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!