简体   繁体   English

命令删除所有角色并将特定角色添加到提到的用户

[英]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();目前,虽然我有 const client = new Discord.Client(); ,但没有定义客户端。 in my main.js file在我的 main.js 文件中

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?这是因为您没有使用当前客户端而是创建一个新实例,我建议您将现有客户端作为函数参数传递给它(使用execute(message, args, client) ,您也可以再次登录此文件但我会小心,因为它可能会产生不必要的副作用。您是否还需要帮助来删除角色?

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.就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM