1
votes

I want to give all the users in my Discord a "Member" role, I have already made it so every new member that joins would receive a member role. However, there are still around 90 people who still do not have the role.

What would the code be for mapping all users (I think it's mapping... or maybe a collection, idk :s) and giving them all a role?

Command should be like : '!giveallrole (role)'

2

2 Answers

0
votes
// find the role with the name "Community"
let role = message.guild.roles.find(r => r.name == 'Community')

// if role doesn't exist, notify the author of command that the role couldn't be found
if (!role) return message.channel.send(`**${message.author.username}**, role not found`)

// find all guild members that aren't bots, and add the "Community" role to each
message.guild.members.filter(m => !m.user.bot).forEach(member => member.addRole(role))

// notify the author of the command that the role was successfully added to all members
message.channel.send(`**${message.author.username}**, role **${role.name}** was added to all members`)
0
votes

This worked for me:

Command Coding

// Command coding
module.exports = {
    name: 'giveallrole',
    description: "giveallrole cmd!",
    async execute(message, args) {
        message.channel.bulkDelete(1);
        //Does the person have moderator rank? - Just change the ID.
        if(message.member.roles.cache.has('854013128928919552')){
            // What role is it?
            let role = message.mentions.roles.first() 
            // Do the command action include the role. (Change the message if you don't want it to be danish.)
            if (!role) return message.channel.send(`**${message.author.username}**, rollen blev ikke funddet`)
            // Giv the role.
            message.guild.members.cache.filter(m => !m.user.bot).forEach(member => member.roles.add(role))
            // Return message - just change the message, it's right now in danish.
            message.channel.send(`**${message.author.username}**, rollen **${role.name}** blev tilføjet til alle medlemmer!`)
        }else{
            // Return message if the person doesn't have moderator.
            message.reply("Du har ikke tilladelse til dette.");
        }
    } }

index.js / main.js coding

// Index.js / main.js coding
client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;
 
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
 
    if(command == 'giveallrole'){
        client.commands.get('giveallrole').execute(message, args);
    } 
});