Eeyeo
Eeyeo

Reputation: 107

How to make discord bot leave voice channel in python

I made the code for the bot to join a voice channel here,

import discord
client = discord.Client()

@client.event
async def on_message(message):

    if message.content.startswith("join"):
        channel = message.author.voice.channel
        await channel.connect()

client.run("TOKEN")

I want the bot to leave the server that it is in when any user types "leave". How do I do that?

Upvotes: 0

Views: 141

Answers (1)

DaveStSomeWhere
DaveStSomeWhere

Reputation: 2540

You need to use VoiceClient.disconnect(). Also, you'll need to create the voice client object and add the get import - from discord.utils import get

Try adding the following:

    if message.content.startswith("leave"):
        channel = message.author.voice.channel
        if client.user in channel.members:
            voice = get(client.voice_clients, guild=message.guild)
            await voice.disconnect()

Upvotes: 1

Related Questions