简体   繁体   中英

Assign every user a role on a Discord Server with discord.js

I'd like to add a role to every user on a Discord Server. I am using "discord.js". I know how to assign a role to a specific user, but I do not know how to do the same thing to every user.

Here is my current code:

//defines the role to a variable
var role = message.guild.roles.cache.find(role => role.name === "myRole");

Thanks for your help:)

You'll have to loop through each member of the guild and add the respective role.

var role = message.guild.roles.cache.find(role => role.name === "myRole"); // Getting the role
message.guild.members.cache.forEach(member => { // Looping through each member.
    member.roles.add(role); // Adding the role.
});

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