Hereux
Hereux

Reputation: 45

discord.py | check for users joining in a voice channel

I'm trying to make a support bot that join in the voice-support-channel when a user connects the support-channel.

@client.command(aliases=['sup', 's'])
async def support(ctx):
    log = client.get_channel(id=701765882417774663)
    channels = ['bot-befehle']
    vc = client.get_channel(id=702412635127152686)
    global player

    if str(ctx.channel) in channels:

        try:
            player = await vc.connect()
        except:
            return

        player.play(discord.FFmpegPCMAudio('support.mp3'))
        player.source = discord.PCMVolumeTransformer(player.source)
        player.source.volume = 1.00
        await asyncio.sleep(30)
        player.stop()
        await player.disconnect()
        await log.send('Habe den Befehl "!support" erfolgreich ausgeführt.')
        print('Habe den Befehl "Support" erfolgreich ausgeführt.')

I have no idea how to check for a user in the voice-channel. Can anyone help?

Upvotes: 2

Views: 7243

Answers (2)

stijndcl
stijndcl

Reputation: 5650

Your explanation was a bit double. I'm going to assume you don't want the bot to join automatically, but only after using this command (as otherwise the command would do nothing).

To check if anyone is in a VoiceChannel, you can use it's members attribute, which will return a list of all members in the channel. You can simply check if this list is empty or not.

@client.command(aliases=['sup', 's'])
async def support(ctx):
    log = client.get_channel(id=701765882417774663)
    channels = ['bot-befehle']
    vc = client.get_channel(id=702412635127152686)

    if not vc.members:
        return

    # ... rest of your code

This way, the command will stop executing in case the channel doesn't have anyone connected.

Upvotes: 3

Hassan Serhan
Hassan Serhan

Reputation: 402

import discord

from discord.ext import commands

@commands.Cog.listener()

async def on_voice_state_update(self, member, before, after):

      if after.channel.id == channel_id and not member.bot:

         voice_client = await channel.connect()

         #do whatever you want here

       `  

Upvotes: 0

Related Questions