如何在discord bot上删除所有角色并添加一个角色,然后删除添加的角色并恢复以前的角色

时间:2022-11-20
本文介绍了如何在discord bot上删除所有角色并添加一个角色,然后删除添加的角色并恢复以前的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一行代码用于不和谐机器人删除特定命名角色并在特定时间内添加名为静音"的角色.基本上,服务器只能有 3 个角色,一个可以发出命令,一个具有正常权限的普通"等级,然后是一个静音"角色.我的代码专门删除了正常角色并添加了静音角色,因此他们没有任何权限.

I have a line of code for a discord bot to remove a specific named role and add a role named "muted" for a specific amount of time. Basically, the server can only have 3 roles, one that can issue the command, a "normal" rank with normal permissions, and then a "muted" role. and my code specifically removed the normal role and adds the muted role so they don't have any permissions.

好吧,我将我的机器人添加到了具有 3 个以上角色的另一台服务器上,我决定给每个人一个正常的角色,同时也做一个静音的角色.当我运行命令时,它可以工作,但由于人们有其他角色,因此即使静音角色处于最高优先级,它也允许继续发言.

Well I added my bot onto another server with more than 3 roles, I decided to give everyone the normal role and also make the muted role. when I run the command, it works, but since the people have other roles, it allows then to continue speaking even though the muted role is at the top priority.

我的问题是,是否有一些代码可以删除他们的所有角色并添加静音角色.并且当静音期结束时,静音角色被删除并恢复他们以前的角色?下面是我的代码:

My questions is that is there some code where I can just remove all their roles and add the muted role. and when the muted period is over, the muted role is removed and their previous roles are restored? here is my code below:

 case 'mute':

    if(!message.member.roles.cache.find(r => r.name === "Admin")) return message.channel.send('You dont have permissions to do that you clown')

    let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
    if(!person) return message.reply("User Doesn't Exist");

    let mainrole = message.guild.roles.cache.find(role => role.name == "normal");
    let muterole = message.guild.roles.cache.find(role => role.name == "muted");

    if(!muterole) return message.reply("Role Doesn't Exist");

    let time = args[2];

    if(!time){
        return message.reply("How Long?");
    }

    person.roles.remove(mainrole.id);
    person.roles.add(muterole.id);

    message.channel.send(`@${person.user.tag} has now been muted for ${ms(ms(time))}`);

    setTimeout(function(){
        person.roles.add(mainrole.id);
        person.roles.remove(muterole.id);
        message.channel.send(`@${person.user.tag} has now been unmuted`)
    }, ms(time));
        break;
    case 'help':
        const embed2 = new MessageEmbed()
        .setTitle("How To Get Commands")
        .setColor(0x00fff7)
        .setDescription("Do /commands to get the list of commands");

        message.author.send(embed2);
        break;

推荐答案

最简单的方法是从用户那里获取角色列表,清除他们的角色,然后申请 Muted 角色.然后,您需要缓存他们以前的角色,甚至将数据存储在数据库中以防机器人出现故障,这样一旦重新启动,您就可以获取数据并从中断的地方继续.

The simplest way to do this would be to get the list of roles from the user, clear their roles, and then apply for the Muted role. You'd then want to cache their previous roles or even store the data in a database in case the bot goes down, that way once restarted you could fetch the data and continue where you left off.

这是我即时编写的一个简单示例.您可能希望缓存角色的方式与我的方式有所不同,因为我的方法只是为了快速演示,我强烈建议将数据保存到数据库甚至是 .json 文件之类的文件中.

Here's a quick example I wrote on the fly. You probably want to cache the roles a bit differently than the way I did since my method was just for a quick demo, and i highly recommend saving the data to a database or even something like a .json file.

let cachedUserRoles = {};

function addMutedRole(guildId, userId, roleId) {
    //Get the guild the user is apart of
    let guild = client.guilds.get(guildId);
    //Specify the user from the guild
    let guildMember = guild.members.get(userId);

    //Cache the users existing roles so we can restore them later
    cachedUserRoles[userId] = guildMember.roles.cache
    //Remove all roles from user
    guildMember.roles.set([])
        //Add the muted role after all roles have been removed with an array of the single role ID
        .then(member => member.roles.add([roleId]))
        //Catch and report any errors that might happen
        .catch(console.error)
}

function restoreRoles(guildId, userId) {
    //Get the guild the user is apart of
    let guild = client.guilds.get(guildId);
    //Specify the user from the guild
    let guildMember = guild.members.get(userId);
    //Get and set the user's previouse roles from cachedUserRoles and error log if anything goes wrong
    guildMember.roles.set(cachedUserRoles[userId]).catch(console.error)
}

在你的情况下,它看起来像这样:

In your case, it would look something like this:

case 'mute':
        if(!message.member.roles.cache.find(r => r.name === "Admin")) return message.channel.send('You dont have permissions to do that you clown')

        let person = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[1]))
        if(!person) return message.reply("User Doesn't Exist");

        let muterole = message.guild.roles.cache.find(role => role.name == "muted");

        if(!muterole) return message.reply("Role Doesn't Exist");

        let time = args[2];

        if(!time){
            return message.reply("How Long?");
        }

        //Cache their already existing roles
        cachedUserRoles[person.id] = person.roles.cache;
        //Set their roles to an empty array to clear them, then add the muted role once all roles were removed successfully
        person.roles.set([]).then(member => member.roles.add(muterole)).catch(console.error);

        message.channel.send(`@${person.user.tag} has now been muted for ${ms(ms(time))}`);

        setTimeout(function(){
            //Grab their existing roles and set them back to the user, we wont need to remove the muted role since setting the roles would override all existing ones already
            person.roles.set(cachedUserRoles[person.id]).catch(console.error)
            message.channel.send(`@${person.user.tag} has now been unmuted`)
        }, ms(time));
        break;
    case 'help':
        const embed2 = new MessageEmbed()
            .setTitle("How To Get Commands")
            .setColor(0x00fff7)
            .setDescription("Do /commands to get the list of commands");

        message.author.send(embed2);
        break;

这篇关于如何在discord bot上删除所有角色并添加一个角色,然后删除添加的角色并恢复以前的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何查看有多少人在特定的不和谐服务器中担任角色? 下一篇:discord.js 检查公会是否有一个具有特定名称的频道,如果有,则将该频道 ID 存储到一个变量中

相关文章

最新文章