简体   繁体   中英

Add a role in Discord.js

I'm new at JS and I'm trying to make my bot give a specific role to a specific member.

If you can help me, please, do this.

The code:

bot.on("message", (msg) => {
    if ((msg.content === "!give", role, member))
        var role = msg.guild.roles.cache.find((r) => {
            return r.name === "convidado astolfofo";
        });
    var member = msg.mentions.members.first();
    member.roles.add(role);
    console.log(member);
    console.log(role);
});

The errors I encountered:

(node:2364) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
TypeError: Cannot read property 'add' of undefined discord.js

There are a couple of errors with your code. I'm not sure what you tried to do with if ((msg.content === ",give", role, member)) -- probably checking if the message starts with !give and if there is a role and a member -- but it won't work like that in JavaScript. You need to check them separately and once these variables are defined.

If you check if msg.content === "!give" but you also want the member to mention a member, it will never be true. If the whole message is just !give , there is no mentioned user. If there is a mentioned user, msg.content will be more than !give . Try to check if the message.content starts with "!give" .

Next, if you don't use curly braces following your if statement, only the next line is inside that statement. So you try to check if the command is !give , and if it is, you search for a role, but the following lines where you check where you add the role are outside of this statement. It means, it runs on every incoming message. Try to use curly braces.

It's also a good idea to check permissions and send replies to let the user know if there are any missing ones.

Check the working example below, I've added comments to make it easier to understand:

bot.on('message', async (msg) => {
  if (msg.content.startsWith('!give')) {
    // check if the message author has the permission to add a role
    if (!msg.member.hasPermission('MANAGE_ROLES')) {
      return msg.reply('You need `MANAGE_ROLES` permission to add a role');
    }

    // check if the bot has the permission to add a role
    if (!msg.guild.me.hasPermission('MANAGE_ROLES')) {
      return msg.reply('I do not have `MANAGE_ROLES` permission to add a role');
    }

    // check if there is a member mentioned
    const member = msg.mentions.members.first();
    // if there is none, send a reply
    if (!member) {
      return msg.reply("Don't forget to mention someone!");
    }

    // search for the role
    // don't forget that it's case-sensitive
    const role = msg.guild.roles.cache.find((r) => r.name === 'convidado astolfofo');

    // check if the role exists
    if (!role) {
      return msg.reply("Oh-oh, I can't find the role `convidado astolfofo`");
    }

    // try to add a role
    try {
      await member.roles.add(role);
      msg.reply(`Role added to ${member}`);
    } catch (error) {
      console.log(error);
      msg.reply('Oops, role not added, there was an error');
    }
  }
});

You seem to be using return incorrectly. Try this:

bot.on("message", (msg) => {
    if ((msg.content === "!give", role, member))
        var role = msg.guild.roles.cache.find(r => r.id === "<role id goes here>");
        var member = msg.mentions.members.first();
        member.roles.add(role);
        console.log(member.username);
        console.log(role.name);
});

I also changed your console.log statements since your console would get spammed with objects

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