久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在discord bot上刪除所有角色并添加一個角色,

How to remove all roles and add one role on discord bot, and then remove the added role and restore previous roles(如何在discord bot上刪除所有角色并添加一個角色,然后刪除添加的角色并恢復以前的角色) - IT屋-程
本文介紹了如何在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上刪除所有角色并添加一個角色,然后刪除添加的角色并恢復以前的角色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 亚洲成人免费av | 日本不卡视频 | www.男人的天堂 | 久久a级片 | 手机av免费 | 亚洲综合二区 | 99色在线 | 国产免费一区二区三区免费视频 | 国产精品福利在线 | 黄色a毛片| 岛国精品在线播放 | 五月婷婷视频 | 国产成人小视频 | 一级片在线免费观看 | 天堂资源av | 亚洲久久久久久 | 91麻豆精品一区二区三区 | 国产欧美精品一区 | 久久久久国产精品夜夜夜夜夜 | 日韩激情一区 | 日韩精品无 | 欧美日韩综合在线 | 2017天天干| 免费一区二区三区 | 五月婷婷综合网 | 亚洲精品www | 午夜影院福利 | 精品一区二区视频 | 免费成人小视频 | 久久久久久一区二区 | 天天操天天干天天操 | 久久久久久久久久国产精品 | 国产欧美日韩综合 | 国产裸体永久免费视频网站 | 日韩亚洲欧美在线观看 | 日韩一级片在线观看 | 亚洲欧美日韩另类 | 五月天婷婷激情 | 欧美视频一二三区 | 免费看a级片 | 在线观看91 |