2
votes

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?

2
What exactly are you trying to do? Your explanation is a bit hard to understand. Do you want to check if anyone is in the voice channel that it joined? - stijndcl
Yes that is it exactly. When anyone is in the channel, the bot should join to this channel. - Makli26

2 Answers

3
votes

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.

0
votes

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

       `