简体   繁体   中英

Command to remove all roles and add specific role to mentioned user

I'm looking to create a command where the mentioned user will have all their roles removed and one specific role added. I'm having some difficulting with permissions and guild syntax.

The current code I've got is:

    name: 'megabean',
    description: 'makes Craig megabean people',
    execute(message, args){

        let guild = client.guilds.cache.get(guildId);
        
        message.channel.send(`${message.author} has **MEGABEANED** ${args [0]}. Say goodbye to your perms
**Reason for MEGABEAN:** ${args.splice(1).join(" ")} `)
            .then(guild.roles.set([]))
            .then(guild.roles.add(['741934348273451032']))
    
    }                
} 

Currently, the client is not defined although I have const client = new Discord.Client(); in my main.js file

I'm just looking for some info on how to better approach this task. Sorry if I may seem somewhat ill-prepared, so far research has not gotten me too far...

This is because you are not using your current client but creating a new instance, I'd suggest you pass your existing client as a function argument to it (using execute(message, args, client) , you could also login in this file again but I'd be careful with that as it can have unwanted side-effects. Do you also need help with removing the roles?

EDIT: For removing roles:

message.member.roles.remove(message.member.roles); //remove all
message.member.roles.add(message.guild.roles.cache.get("role ID here")); //add the specific role

That's all.

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