最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Discord JSTrying to add role by reacting to the message - Stack Overflow

programmeradmin3浏览0评论
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

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

Share Improve this question edited Nov 28, 2019 at 11:56 T. Dirks 3,6761 gold badge23 silver badges36 bronze badges asked Nov 27, 2019 at 11:58 BixiBixi 111 gold badge1 silver badge2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

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);

You are using message.member variable despite not defining message.

Any of these methods won't really work in v12 so I updated it for someone else searching. If you find any mistakes be sure to make me aware of it.

const Discord = require('discord.js');
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] }); //partials arent really needed but I woudld recend using them because not every reaction is stored in the cache (it's new in v12)
const prefix = "-";

client.on('messageReactionAdd', async (reaction, user) => {
    if (reaction.partial) { //this whole section just checks if the reaction is partial
        try {
            await reaction.fetch(); //fetches reaction because not every reaction is stored in the cache
        } catch (error) {
            console.error('Fetching message failed: ', error);
            return;
        }
    }
    if (!user.bot) {
        if (reaction.emoji.id == yourEmojID) { //if the user reacted with the right emoji

            const role = reaction.message.guild.roles.cache.find(r => r.id === yourRoleID); //finds role you want to assign (you could also user .name instead of .id)

            const { guild } = reaction.message //store the guild of the reaction in variable

            const member = guild.members.cache.find(member => member.id === user.id); //find the member who reacted (because user and member are seperate things)

            member.roles.add(role); //assign selected role to member

        }
    }
})

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);
  }
 }
});
发布评论

评论列表(0)

  1. 暂无评论