0

I use this code to count discord members

class CONFING:
    PREFIX = 'k.'
client = commands.Bot(command_prefix=CONFING.PREFIX)
client.remove_command('help')
@client.event
async def on_ready():
    print('1')
    print('2')
    print('3')    
    print('GanGz Iz Here')
    servers = len(client.guilds)
    members = 0
    for guild in client.guilds:
        members += guild.member_count
    await client.change_presence(activity = discord.Activity(
        type = discord.ActivityType.watching,
        name = f'✨{members} ᴍᴇᴍʙᴇʀꜱ'
    ))

and I wanna add active mics to activity too (I mean the users that they are connected to voice) I tried many ways but I failed

Zoe
  • 27,060
  • 21
  • 118
  • 148
AwJ
  • 33
  • 8

2 Answers2

1

You have to go through all the channels one by one to get all connected members from each channel.

connected_members = 0
for guild in client.guilds:
    for channel in guild.voice_channels:
        connected_members += len(channel.members)
print(f'There are currently {connected_members} users connected to voice channels.')


Copy and paste

class CONFING:
    PREFIX = 'k.'
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=CONFING.PREFIX), help_command=None, intents=intents)

@client.event
async def on_ready():
    print('1')
    print('2')
    print('3')    
    print('GanGz Iz Here')
    servers = len(client.guilds)
    members = 0
    connected_members = 0
    for guild in client.guilds:
        members += guild.member_count
        for channel in guild.voice_channels:
            connected_members += len(channel.members)
    await client.change_presence(activity = discord.Activity(
        type = discord.ActivityType.watching,
        name = f'✨{members} members ({connected_members} connected).'
    ))

Edit

Discord has something called "Privileged Gateway Intents" - also called intents. You have to go to your application on discord developer website and allow "server member intent" (you'll find it in bot section). After that add this to your code:

intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=CONFING.PREFIX), intents=intents)
RiveN
  • 2,595
  • 11
  • 13
  • 26
  • `for channel in guild.voice_channels: ^ IndentationError: expected an indented block` – AwJ Aug 27 '21 at 02:06
  • You probably used space/tab in this line as an error says ([IndentationError](https://stackoverflow.com/questions/4446366/why-am-i-getting-indentationerror-expected-an-indented-block)). Maybe copy the whole code I just pasted. – RiveN Aug 29 '21 at 18:03
  • Also small tip: if you want it to update consider using the loop. – RiveN Aug 29 '21 at 18:09
  • ok now it run but dosent count the connected users, shows 0 (i have done things in discord developer ) – AwJ Aug 30 '21 at 20:56
  • Hmm. I tested it and it worked. Did you join the channel after running the bot or before? Remember that you don't have a loop in this function and the `on_ready` event is called when the bot is done with the setup, so this will get all users connected to the channels at the moment you run your code. This means that if you join the channel when the bot is already running this won't work. As I said earlier, consider looping it to get it updated. – RiveN Aug 30 '21 at 22:24
  • bot is already in channel and has perm, but still when i run the bot for eg 10 users are connected but it shows 0 – AwJ Sep 02 '21 at 01:18
  • I just edited my answer, check if this works. Sorry for the late answer, but I just found the answer accidentally while programming my own bot. – RiveN Sep 27 '21 at 10:22
0

I add it a loop that refresh the status.

class CONFING:
    PREFIX = 'k.'
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=CONFING.PREFIX, intents=intents)

@client.event
async def on_ready():
    print("\n▄▀█ █░█░█ ░░█\n█▀█ ▀▄▀▄▀ █▄█\n")
    if not loop.is_running():
        loop.start()

@tasks.loop(seconds=10)
async def loop():
    print("Refresh")
    servers = len(client.guilds)
    members = 0
    connected_members = 0
    for guild in client.guilds:
        members += guild.member_count
        for channel in guild.voice_channels:
            connected_members += len(channel.members)
            await client.change_presence(activity=discord.Game(f'✨{members} ᴍᴇᴍʙᴇʀꜱ⚡{connected_members} ᴍɪᴄ'))
    print("Done")
Zoe
  • 27,060
  • 21
  • 118
  • 148
AwJ
  • 33
  • 8