0
votes

I have been trying to write a discord bot using discord.py. VoiceChannel.connect() works just like stated in the documents however VoiceChannel.disconnect() doesn't work even though it is stated in the documents. I need to use to make the bot leave the voice channel. Below is the code.

    elif message.content == ("/bind"):
        channel = message.author.voice.channel
        vc = await channel.connect()


    elif message.content == ("/unbind"):
        channel = message.author.voice.channel
        vc = await channel.disconnect()

The error im getting is:

AttributeError: 'VoiceChannel' object has no attribute 'disconnect'

also I'm using @client.event instead of @client.command

1

1 Answers

0
votes

The error is pretty clear, VoiceChannel does not have disconnect.

What you are looking for is VoiceClient.disconnect().

What your code is doing is on the initial channel.connect() a VoiceClient object is created and asigned to variable vc. You can use await vc.disconnect() to disconnect from a voice channel.

Another option is to check if the bot is connected to a voice channel on the server/guild using Client.voice_client, and if so disconnect it from it.

import discord

client = discord.Client()


@client.event
async def on_message(message):
    if message.content == "/bind":
        channel = message.author.voice.channel
        vc = await channel.connect()

    elif message.content == "/unbind":
        for vc in client.voice_clients:
            if vc.guild == message.guild:
                await vc.disconnect()


client.run('token')