I'm working on a discord bot for my friend group's gaming server. I'd like to add a command mutes everyone in the voice channel. I figured that this msg.member.voice.channel.members.setmute(true);
would work but it's returning back not being a function and crashes the bot. This msg.member.voice.setMute(true);
works as in it will server mute the member who sends the message but obviously not the whole channel which is what I'm going for. I'm brand new to discord.js and the documentation has been a little confusing. Thanks for your time!
4
votes
1 Answers
5
votes
I did something similar in a project not too long ago where I had to mute everyone but the person issuing the command.
You can accomplish this by iterating through an array of all users in the current channel.
// Your invokation here, for example your switch/case hook for some command (i.e. '!muteall')
// Check if user is in a voice channel:
if (message.member.voice.channel) {
let channel = message.guild.channels.cache.get(message.member.voice.channel.id);
for (const [memberID, member] of channel.members) {
// I added the following if statement to mute everyone but the invoker:
// if (member != message.member)
// This single line however, nested inside the for loop, should mute everyone in the channel:
member.voice.setMute(true);
}
} else {
message.reply('You need to join a voice channel first!');
}