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

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

How to remove all roles and add one role on discord bot, and then remove the added role and restore previous roles(如何在discord bot上刪除所有角色并添加一個(gè)角色,然后刪除添加的角色并恢復(fù)以前的角色) - IT屋-程
本文介紹了如何在discord bot上刪除所有角色并添加一個(gè)角色,然后刪除添加的角色并恢復(fù)以前的角色的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一行代碼用于不和諧機(jī)器人刪除特定命名角色并在特定時(shí)間內(nèi)添加名為靜音"的角色.基本上,服務(wù)器只能有 3 個(gè)角色,一個(gè)可以發(fā)出命令,一個(gè)具有正常權(quán)限的普通"等級(jí),然后是一個(gè)靜音"角色.我的代碼專門刪除了正常角色并添加了靜音角色,因此他們沒有任何權(quán)限.

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.

好吧,我將我的機(jī)器人添加到了具有 3 個(gè)以上角色的另一臺(tái)服務(wù)器上,我決定給每個(gè)人一個(gè)正常的角色,同時(shí)也做一個(gè)靜音的角色.當(dāng)我運(yùn)行命令時(shí),它可以工作,但由于人們有其他角色,因此即使靜音角色處于最高優(yōu)先級(jí),它也允許繼續(xù)發(fā)言.

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.

我的問題是,是否有一些代碼可以刪除他們的所有角色并添加靜音角色.并且當(dāng)靜音期結(jié)束時(shí),靜音角色被刪除并恢復(fù)他們以前的角色?下面是我的代碼:

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;

推薦答案

最簡(jiǎn)單的方法是從用戶那里獲取角色列表,清除他們的角色,然后申請(qǐng) Muted 角色.然后,您需要緩存他們以前的角色,甚至將數(shù)據(jù)存儲(chǔ)在數(shù)據(jù)庫(kù)中以防機(jī)器人出現(xiàn)故障,這樣一旦重新啟動(dòng),您就可以獲取數(shù)據(jù)并從中斷的地方繼續(xù).

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.

這是我即時(shí)編寫的一個(gè)簡(jiǎn)單示例.您可能希望緩存角色的方式與我的方式有所不同,因?yàn)槲业姆椒ㄖ皇菫榱丝焖傺菔荆覐?qiáng)烈建議將數(shù)據(jù)保存到數(shù)據(jù)庫(kù)甚至是 .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;

這篇關(guān)于如何在discord bot上刪除所有角色并添加一個(gè)角色,然后刪除添加的角色并恢復(fù)以前的角色的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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(如何讓我的機(jī)器人提及發(fā)出該機(jī)器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時(shí)的歡迎消息)
主站蜘蛛池模板: 亚洲中午字幕 | 日本不卡高字幕在线2019 | 久久影院一区 | 蜜臀久久99精品久久久久久宅男 | 久久精品国产免费 | 午夜影院在线观看视频 | 国产精品久久一区二区三区 | av在线天堂 | 91 视频网站 | 一区天堂| 欧美激情视频一区二区三区在线播放 | 99精品久久久久久中文字幕 | 综合国产| 精久久久久 | 亚洲国产小视频 | 亚洲欧美中文日韩在线v日本 | 国产一区二区三区在线免费观看 | 国产精品福利网站 | 日韩精品一区二区三区中文字幕 | 91精品国产一区二区三区动漫 | 亚洲天堂成人在线视频 | 中文字幕视频三区 | 久久男人 | 你懂的av| 精品国产乱码久久久久久闺蜜 | 国产综合久久 | 久久伊人青青草 | 一区二区三区视频在线观看 | 亚洲欧美国产毛片在线 | 国产一区二区三区久久久久久久久 | 久久伊人影院 | 欧美精品a∨在线观看不卡 欧美日韩中文字幕在线播放 | 亚洲精品一级 | 免费看啪啪网站 | 91一区二区三区在线观看 | 亚洲网视频| av在线播放网址 | 成人网视频 | av片网站| 一区二区三区国产好 | 在线成人一区 |