0
votes

It only mutes me and itself when it shouldn't, bot has highest role and also has permission in joined voice channel. any ideas?

@bot.command() 
async def mute(ctx):
        voice_client = ctx.guild.voice_client #
        if not voice_client:  
            return
        channel = voice_client.channel 
        people = channel.members 
        for person in people: 
            if person == client.user: 
                continue
            await person.edit(mute=True, reason="{} told me to mute everyone in this channel".format(ctx.author))

edit Full code:

import discord
from discord.ext import commands
import os
from discord.utils import get


client = discord.Client()

DISCORD_TOKEN = os.getenv("TokenGoesHere")


bot = commands.Bot(command_prefix="$")

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

@bot.command()
async def join(ctx):
    channel = ctx.message.author.voice.channel
    await channel.connect()

@bot.command()
async def disconnect(ctx):
    channel = ctx.message.author.voice.channel
    await channel.disconnect()



@bot.command() 
@commands.has_permissions(administrator=True)
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.

@bot.command() 
@commands.has_permissions(administrator=True)
async def unmute(ctx):
        voice_client = ctx.guild.voice_client
        if not voice_client:  
            return
        channel = voice_client.channel 
        people = channel.members 
        for person in people: 
            if person == client.user: 
                continue
            await person.edit(mute=False, reason="{} told me to mute everyone in this channel".format(ctx.author))



bot.run("TokenGoesHere")

Hope this helps, Bot sometimes mutes only itself or only itself and other user, but specifically that one user... It only mutes me and itself when it shouldn't, bot has highest role and also has permission in joined voice channel. any ideas?

1
Did you define intents on your code?Nurqm
oh no.. how can I do it?David Khvedelidze
I added full codeDavid Khvedelidze

1 Answers

0
votes

You have to define the intents to use some events, functions like on_message, guild.members, channel.members etc.

# Here is your imports
import discord

intents = discord.Intents().all()
bot = commands.Bot(command_prefix='', intents=intents)
# Rest of your code

Also, you have to activate intents from Discord Developer Portal.

    • Go to your bot application.
    • Go to Bot -> Privileged Gateway Intents.
    • Activate Presence Intent and Server Members Intent.