1
votes

How can I make my Discord bot give a user a role? It has the "Administrator" permission, and I'm using the nodejs library. Here's my code so far (this initializes the bot):

var auth = require("./auth.json");
var fs = require("fs");
var bot = new discord.Client();
bot.login("TOKENTOKENTOKENTOKENTOKENTOKENTOKENTOKENTOKEN")

bot.on("ready", function(event) {
        console.log("Initialized");
});
3
You need to use the GuildMember.addRole() method. Can you give more details about your question? Which member you want to add the role to? And when?Androz2091
I just want the function to add the role.LostXOR

3 Answers

1
votes

You must use the GuildMember.addRole() function. (to get a guild member, use Guild#members.get()). Read the Discord.js.org documentation for more informations.

0
votes

You can use:

bot.on("message", (message) => {
    if(!message.member) return
    // Find the role by name
    var role = message.guild.roles.find((r) => r.name === "Name of your role");
   // Assign the role to the message author
   message.member.roles.add(role);
});
0
votes

You can try this

client.on('guildMemberAdd', member => {

    //Search and add the role to the new user
    member.addRole(member.guild.roles.find(nm => nm.name === "Your role name"));

    // Send the message to a designated channel on a server:
    const channel = member.guild.channels.find(ch => ch.name === 'general');

    // Do nothing if the channel wasn't found on this server
    if (!channel) return;

    // Send the message, mentioning the member
    channel.send(`Welcome to the server, ${member}`);
  });