Here is my code:
How can I assign roles to users with my discord js bot?
How can I make my discord bot assign roles to users (discord js)
I want it so that when I type !addrole a role gets added
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.on('ready', () => {
console.log('Bot Online');
});
var prefix = "!"
client.on(`message`, message => {
if(message.content.startsWith(prefix + "addrole"))
{
//ADD ROLE HERE
}
});
client.on('ready', () => {
client.user.setActivity(`!help`, { type: "PLAYING" });
});
client.login('TOKEN');
First you need to find the role you want to add:
const guild = message.guild;
let role = message.mentions.roles.first() || guild.roles.cache.find(role => role.id === message.content.substring(1))
Then you add the role using:
const member = message.member
member.roles.add(role);