如何为discord.js的不和谐语音通道全部静音/取消静音?

时间:2020-09-16 05:36:03

标签: javascript discord discord.js

我正在尝试创建一个机器人,该机器人通过向其添加“ TempMute”角色来使当前处于语音通道中的每个人静音。然后,当我发送/ unmuteAll时,我希望具有“ TempMute”标签的每个人都删除该标签。我尝试了几种不同的方法,但均未成功。我正在使用discord.js版本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"
        }
    }
});

1 个答案:

答案 0 :(得分:0)

由于您已经拥有GuildMember,因此可以使用GuildMember.roles .addGuildMember.roles .remove方法来添加或删除角色。


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")
        }
    }
});