0
votes

I am trying to make a Discord bot that mutes the voice of the participant using voice chat.

For this, I am using Python.

This is my code, but it is not working as expected.

import discord 
from discord.ext import commands 

client = commands.Bot(command_prefix=" !") 

@client.event 
async def on_ready():
     print('BOT ACTIVATED')

@client.command() 
async def mute(ctx):
     voice_client = ctx.guild.voice_client
     if not voice_client:
         return
     channel = voice_client.channel
     await voice_client.voice_state(ctx.guild.id, channel.id, self_mute=True)
 
client.run(My Token)

The idea I have is:

Command I will enter: !muteall \

And the bot will mute all the participants in the voice chat

Command I will enter: !unmuteall \

And the bot will unmute all the participants in the voice chat.

1
Can you show your entire code file, minus the token? Do you have the @client.command() decorator above async def mute ? What is main_ws supposed to be in voice_client.main_ws.voice_state() ? I can't find it in the discord.py docsAllister
import discord from discord.ext import commands client = commands.Bot(command_prefix=" !") @client.event async def on_ready(): print('BOT ACTIVATED') @client.command() async def mute(ctx): voice_client = ctx.guild.voice_client if not voice_client: return channel = voice_client.channel await voice_client.voice_state(ctx.guild.id, channel.id, self_mute=True) client.run(My Token)Eshan

1 Answers

1
votes

Before we get to the crux of the question, a short word on your prefix:
Your command prefix is ! with a space beforehand. I do not know if this was intentional or not, but if it was, in my testing I found using this to be impossible. Discord strips beginning whitespace so all my messages !test come out as !test.

After fixing this, attempting to use the !mute command produces an error:
'VoiceClient' object has no attribute 'voice_state' Indeed, I have been unable to find anything like this in the docs. It took me a lot of searching, but I may have found what you wanted.

client = commands.Bot(command_prefix="!") 

@client.command() 
async def mute(ctx):
        voice_client = ctx.guild.voice_client #get bot's current voice connection in this guild
        if not voice_client:  #if no connection...
            return  #probably should add some kind of message for the users to know why nothing is happening here, like ctx.send("I'm not in any voice channel...")
        channel = voice_client.channel #get the voice channel of the voice connection
        people = channel.members #get the members in the channel
        for person in people: #loop over those members
            if person == client.user: #if the person being checked is the bot...
                continue #skip this iteration, to avoid muting the bot
            await person.edit(mute=True, reason="{} told me to mute everyone in this channel".format(ctx.author))
            #edit the person to be muted, with a reason that shows in the audit log who invoked this command. Line is awaited because it involves sending commands ("mute this user") to the server then waiting for a response.

Your bot will need permissions to mute users.