简体   繁体   中英

How to mute/unmute all in discord voice channel for discord.js?

I'm trying to create a bot that mutes everyone currently in a voice channel by adding a role "TempMute" to them. Then when I send /unmuteAll I would like everyone who has the "TempMute" tag to have the tag removed. I have tried several different ways with no success. I am using discord.js version 12.2.0.

const Discord = require('discord.js');


voiceChatChannelId="123456789"

bot.on('message', (message) => {
    var vc = bot.channels.cache.get(voiceChatChannelId);

    if (message.content == '/muteAll') {
        for (let member of vc.members){
            //add members all to "TempMute" role
        }
    }

    else if (message.content == '/unmuteAll') {
        for (let member of vc.members){
            //remove ALL members from role "TempMute"
        }
    }
});

Since you already have the GuildMember you can use GuildMember.roles .add or GuildMember.roles .remove methods to either add or remove a role.


bot.on('message', (message) => {
    var vc = bot.channels.cache.get(voiceChatChannelId);

    if (message.content == '/muteAll') {
        for (let member of vc.members.array()){
            //add members all to "TempMute" role
             member.roles.add("RoleID")
        }
    }

    else if (message.content == '/unmuteAll') {
        for (let member of vc.members.array()){
            //remove ALL members from role "TempMute"
             member.roles.remove("RoleID")
        }
    }
});

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