user15121526
user15121526

Reputation:

Discord py how can I join a voice channel

How can I make that my bot (in a cog) join the voice channel where is the user in? I have this code:

@commands.command(name='join')
async def join(self, ctx):
    channel = ctx.author.channel

    voice = discord.utils.get(ctx.guild.voice_channels, name=channel.name)

    voice_client = discord.utils.get(self.client.voice_clients, guild=ctx.guild)

    if voice_client == None:
        await voice.connect()
    else:
        await voice_client.move_to(channel)

Upvotes: 0

Views: 1521

Answers (1)

raphiel
raphiel

Reputation: 852

You need to use ctx.author.voice.channel instead of ctx.author.channel

@commands.command(name='join')
async def join(self, ctx):
    channel = ctx.message.author.voice.channel

    voice = discord.utils.get(ctx.guild.voice_channels, name=channel.name)

    voice_client = discord.utils.get(self.client.voice_clients, guild=ctx.guild)

    if voice_client == None:
        await voice.connect()
    else:
        await voice_client.move_to(channel)

This should work

Upvotes: 2

Related Questions