Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a bot join voice channels discord.py

I'm using discord.py to create a music bot, but I'm having trouble connecting the bot to a voice channel. Im using a Cog to seperate the music features from the rest.

@commands.command()
async def join_voice(self, ctx):
    channel = ctx.author.voice.channel
    print(channel.id)
    await self.client.VoiceChannel.connect()

But I get the error: AttributeError: 'NoneType' object has no attribute 'channel'

I've looked all through the docs and all the similar questions on here and still no solutions!

Could someone help please?

like image 688
Legacy Coding Avatar asked Dec 23 '22 18:12

Legacy Coding


1 Answers

You're really close! The only thing you've got to change is:

@commands.command()
async def join_voice(self, ctx):
    connected = ctx.author.voice
    if connected:
        await connected.channel.connect() #  Use the channel instance you put into a variable

What you were doing was grabbing the VoiceChannel class object, and not the actual VoiceChannel instance that the user was connected to. That's where your error came in, as it was trying to find a voice channel which didn't exist.

Glad to see the progress, keep it up!

like image 144
Diggy. Avatar answered Jan 09 '23 23:01

Diggy.