Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a discord.py bot mute the voice of others in a voice call?

I am the owner of an Among Us Discord guild with some bots I implemented into it. When we play over on of the voice calls, sometimes people forget to mute themselves so we can hear them which often reveals valuable information.

I am trying to design a set of commands that will allow one person to easily mute everyone the voice channel. They use the command claim "VC Name" to claim command over the indicated voice channel, then use the command set_mute "true/false" to either mute or unmute everyone. The first part I have down, but I am struggling with the code of the second part that actually mutes/unmutes the voice channel members.

While reading the documentation for discord.py, I found a few options that might work, but I don't know how to execute any of them.

  1. discord.VoiceState (Documentation): discord.VoiceState has attributes mute, muted, and self_mute. Perhaps it is possible to modify the voice state of the member with something like member.VoiceState.mute = True, member.VoiceState.muted = True or member.VoiceState.self_mute = True. I'm not sure how to use this method since using the above line of code results in an AttributeError.
  2. discord.Member.edit (Documentation): member.Member.edit has the option to set mute to True. This actually sets the member to a server mute (and I can't seem to undo it), so I would rather avoid this option unless there is a solution through this method.
  3. I could set a unique role that is assigned to all the members in the voice channel and the bot can set the speak permissions on command. This is the method I am using right now but I would like to use an alternative one, if it exists.

This is the code that I have right now. Under the last if/else statement is where I will put the solution to mute and unmute the members in the call.

async def mute(ctx, setting):
    regex, claimed = re.compile(f"_VC: (Lobby [0-9])_"), False
    for role in cx.message.author.roles:
        if regex.search(role.name):
            claimed = True
            break
    if not claimed:
        await ctx.send("You have not claimed any of the game lobbies")
        return
    voice_channel = discord.utils.get(ctx.guild.channels, name=regex.search(role.name).group(1))
    for member in voice_channel.members:
        if setting.lower() == 'true':
            await member.voice_state.set_to_muted()
        elif setting.lower() == 'false':
            await member.voice_state.set_to_not_muted()
like image 298
Jacob Lee Avatar asked Oct 15 '22 23:10

Jacob Lee


1 Answers

  • For your first option, if you look at the API references of discord.VoiceState.self_mute, you'll see a description of this attribute:

Indicates if the user is currently muted by their own accord.

So this means that you cannot set any value, this attribute returns True/False depending on user muted his/herself or not.


  • For your second option, it seems the best way to do this. And yes, members who has no permission, can't undo their mute but you can unmute them from code or manually.

  • For your third option, it's another solution but I won't suggest it while you can use member.edit.

So I guess you're trying to make 2 methods named set_to_muted and set_to_unmuted but for this, you have to edit the main python files of discord.py library. Instead of that, you can create 2 async functions that takes member as parameter and you can mute them by this way.

async def set_to_muted(member: discord.Member):
    await member.edit(mute=True)

async def set_to_unmuted(member: discord.Member):
    await member.edit(mute=False)

@client.command()
async def mute(ctx, setting):
    voice_channel = discord.utils.get(ctx.guild.channels, name='')
    if setting.lower() == 'true':    
        for member in voice_channel.members:
            await set_to_muted(member)
    elif setting.lower() == 'false':    
        for member in voice_channel.members:
            await set_to_unmuted(member)
like image 69
Nurqm Avatar answered Oct 21 '22 06:10

Nurqm