简体   繁体   中英

Bot to Assign Reacting User to a Role in Discord

I am trying to make a Discord Bot that reads every Emoji Reaction in a Server and assigns the reacting User with a particular role that is specific to the Emoji.

My code should Assign the Role Red to Users reacting with 🔴 in any message in the server. Assign the Role Blue to Users reacting with 🔵 in any message in the server. Assign the Role Yellow to Users reacting with 🟡 in any message in the server.

Here are the IDs of these roles:

Red: <@&870162738561814578>
Blue: <@&870162842983206922>
Yellow: <@&870162885412810773>

Here's a code snippet of the bot:

const RYellow = '<@&870162885412810773>';
const RBlue = '<@&870162842983206922>';
const RRed = '<@&870162738561814578>';

const RRedEmoji = '🔴';
const RBlueEmoji = '🔵';
const RYellowEmoji = '🟡';

client.on('messageReactionAdd', async (reaction, user) => {
  if (reaction) await reaction.fetch();
  if (user.bot) return;
  console.log(reaction);
  if (reaction.emoji.name === RYellowEmoji) {
    await reaction.message.guild.members.cache.get(user.id).roles.add(RYellow);
    await reaction.message.guild.members.cache.get(user.id).roles.remove(RRed);
    await reaction.message.guild.members.cache.get(user.id).roles.remove(RBlue);
  }
  if (reaction.emoji.name === RRedEmoji) {
    await reaction.message.guild.members.cache.get(user.id).roles.add(RRed);
    await reaction.message.guild.members.cache
      .get(user.id)
      .roles.remove(RYellow);
    await reaction.message.guild.members.cache.get(user.id).roles.remove(RBlue);
  }
  if (reaction.emoji.name === RBlueEmoji) {
    await reaction.message.guild.members.cache.get(user.id).roles.add(RBlue);
    await reaction.message.guild.members.cache.get(user.id).roles.remove(RRed);
    await reaction.message.guild.members.cache
      .get(user.id)
      .roles.remove(RYellow);
  }
});

And heres the error:

E:\\Projects\\DBot\\node_modules\\discord.js\\src\\managers\\GuildMemberRoleManager.js:93 throw new TypeError('INVALID_TYPE', 'roles', 'Role, Snowflake or Array or Collection of Roles or Snowflakes'); ^

TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes. at GuildMemberRoleManager.add (E:\\Projects\\DBot\\node_modules\\discord.js\\src\\managers\\GuildMemberRoleManager.js:93:15) at Client. (E:\\Projects\\DBot\\index.js:49:71) at processTicksAndRejections (node:internal/process/task_queues:96:5) { [Symbol(code)]: 'INVALID_TYPE' }

So, what am I doing wrong?

<@&870162885412810773> is not a snowflake or an ID. You'll need to remove <@& and > :

const RYellow = '870162885412810773'
const RBlue = '870162842983206922'
const RRed = '870162738561814578'

// ...

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