简体   繁体   中英

How to get Reaction Roles working in discord.js?

I am trying to get this functionality to work but I keep running into an issue with my reaction role. COuld it be that I am just using the wrong functions/methods? I would very much appreciate some help.

I am fairly new to discord bot dev and might be asking quite a silly question here, but your help would be very much appreciated!

Here is an example of my code!

require('dotenv').config();

const { Client, Intents, GuildMember, ReactionEmoji } = require('discord.js');
const client = new Client( 
  {
  partials: ['MESSAGE' , 'REACTON'],
  intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
});
const PREFIX = '$';

client.on('ready', () => {
  console.log(`${client.user.tag} has logged in.`);
});

client.on('messageCreate', async (message) => {
  if (message.author.bot) return;
  //console.log(`[${message.author.tag}]: ${message.content}`);
  /* if (message.content === 'hello') {
            message.reply('https://media.giphy.com/media/KtnNLZVycYLdutzEm9/giphy.gif');
            //message.channel.send('https://media.giphy.com/media/KtnNLZVycYLdutzEm9/giphy.gif');
        }*/

  if (message.content.startsWith(PREFIX)) {
    const [CMD_NAME, ...args] = message.content
      .trim()
      .substring(PREFIX.length)
      .split(/\s+/);
//KICK AND BAN 
    if (CMD_NAME === 'kick') {
      if (!message.member.permissions.has('KICK_MEMBERS'))
        return message.reply('You do not have permissions to use that command');
      if (args.length === 0)
        return message.reply('Please provide the ID of the offender.');
      const member = message.guild.members.cache.get(args[0]);
      if (member) {
        member
          .kick()
          .then((member) => message.channel.send(`${member} was kicked.`))
          .catch((err) =>
            message.channel.send('I do not have permissions :( ')
          );
      } else {
        message.channel.send('That Member was not found');
      }
    } else if (CMD_NAME === 'ban') {
      if (!message.member.permissions.has('BAN_MEMBERS'))
        return message.reply('You do not have permissions to use that command');
      if (args.length === 0)
        return message.reply('Please provide the ID of the offender.');
      message.guild.members.ban(args[0]);

      try {
        const user = await message.guild.members.ban(args[0]);
        message.channel.send('User was banned successfully.');
        console.log(user);
      } catch (err) {
        console.log(err);
        message.channel.send('An error occured. Either I do not have permission or the user was not found.')
      }
    }
  }
});

//Reactions Roles
client.on('messageReactionAdd', (reaction, user) => {
  console.log('Whats Gud');
  const { name } = reaction.emoji;
  const member = reaction.message.guild.members.cache.get(user.id);
  if (reaction.message.id === '928504577475616819') {
    switch (name) {
      case '🎵':
        member.roles.add('928503338683748352');
        break;
      case '📖':
        member.roles.add('928503897251782698');
        break;
      case '🎮':
        member.roles.add('928503941430407168');
        break;
      case '🖌️':
        member.roles.add('928503384561053847');
        break;
    }
  }
});

client.login(process.env.DISCORDJS_BOT_TOKEN);

Thanks

What I think is the problem is the fact that messageReactionAdd only works on cached messages according to this: Why messageReactionAdd do nothing discord.js

Instead of checking the message id, try checking the message content

 client.on('messageReactionAdd', (reaction, user) => { console.log('Whats Gud'); const { name } = reaction.emoji; const member = reaction.message.guild.members.cache.get(user.id); if (reaction.message.content === 'your-message-content') { switch (name) { case '': member.roles.add('928503338683748352'); break; case '': member.roles.add('928503897251782698'); break; case '': member.roles.add('928503941430407168'); break; case '️': member.roles.add('928503384561053847'); break; } } });
If that doesn't work, please attach the error message you get when you run your code so we can check where it is going wrong

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