0
votes

I have previously asked a question for the actual process of this and I got it working perfectly but there's just one bug to this.

async def on_voice_state_update(self, member, before, after):
    def is_connected(member):
        voice_client = get(self.bot.voice_clients, guild=member.guild)
        return voice_client and voice_client.is_connected()

    if is_connected(member):
        await asyncio.sleep(9)
        if member.id == 'id of member' and after.afk and not before.afk:
            prevchannel = int(before.channel.id)
            channel = self.bot.get_channel(prevchannel)
            voice = await channel.connect()
            voice.play(discord.FFmpegPCMAudio('dir of mp3 file'))
            await asyncio.sleep(5)
            await voice.disconnect()
        else:
            pass
    else:
        if member.id == 'id of member' and after.afk and not before.afk:
            prevchannel = int(before.channel.id)
            channel = self.bot.get_channel(prevchannel)
            voice = await channel.connect()
            voice.play(discord.FFmpegPCMAudio('dir of mp3 file'))
            await asyncio.sleep(5)
            await voice.disconnect()
        else:
            pass

I am trying to check if the bot is already connected to a voice channel so that if more than one person joins the afk at a similar time (like 2 seconds later) it waits until the next iteration of the program is executed: await asyncio.sleep(10)

The Problem

I can't seem to get the is_connected function to work, although I believe I am heading in the right direction. I have thought of trying to get the user id of the bot and checking if it's connected to a voice channel. It would be something like this:

def is_botconnected(member):
    botid = self.bot.get_user('id of bot')
    channel = (all the channels)
if botid in channel.members:
    await asyncio.sleep(9)
    #execute program
else:
    #execute program
1

1 Answers

0
votes

The solution

I managed to get it to work by using bot.voice_clients.

async def on_voice_state_update(self, member, before, after):
    def is_connected(self):
        bot_client = get(self.bot.voice_clients)
        return bot_client and bot_client.is_connected()

    if is_connected(self):
        await asyncio.sleep(9)
        #execute program
    else:
        #execute program

self is used as it is a cog.