# Voice Commands

{% hint style="danger" %}
This guide has been moved to <https://namantech.me/pycord>. Please visit the new guide instead, since this one might be outdated.

The new guide looks better, has better explanations, and is way better overall.
{% endhint %}

## Installing

To use voice commands, you need to update your library first -&#x20;

```bash
# Linux/macOS
python3 -m pip install -U "py-cord[voice]"

# Windows
py -3 -m pip install -U py-cord[voice]
```

## Joining and Leaving

### Join Command

First of all, we want to make a command that will make the bot join our VC.

```python
@bot.command()
async def join(ctx):
    voice = ctx.message.author.voice

    if voice != None:
        await voice.channel.connect()
        await ctx.send(f"Connected and bound to {voice.channel.mention}!")
    else:
        await ctx.send(
            "You need to be connected to a voice channel to use this command!"
        )
```

When the user is not connected to a channel, the voice variable returns none. When it

* [x] If the user is not connected to a voice channel, tell them to do so

![When you are not connected to a voice channel!](/files/-MjvqljdMewywUvDky0Q)

Next, if the user is connected, we connect the the channel too, using `await channel.connect()`

* [x] If the user is connected to the voice channel, connect to it too.

![When you are connected to a voice channel, the bot joins it too!](/files/-Mjvqum75b_69GTbwZQN)

Now, we have a problem. We left the VC, but our bot is stuck there. So, we need to make a leave command to make the bot leave the VC.

### Leave Command

```python
@bot.command()
async def leave(ctx):
    voice = ctx.voice_client
    if voice != None:
        await voice.disconnect()
        await ctx.send(f"Left the VC!")
    else:
        await ctx.send("I am not connected to any voice channel!")
```

This command will make the bot leave ANY VC that it is in, as well as inform the user if the bot is not in a VC.

![Leave Command](/files/-MjvxjgKT3_DstuIk3M1)

We can also make it necessary for the user to be in a Voice Channel to use this command, similar to the join command, but we don't want it, so lets not do it for now.

## Playing Audio

### Playing local audio files

```python
@bot.command(name="play")
async def play(ctx):
    voice_channel = ctx.author.voice.channel
    if voice_channel != None:
        vc = await voice_channel.connect()
        vc.play(
            discord.FFmpegPCMAudio(
                executable=r"C:\Path\ffmpeg.exe",
                source=r"C:\Users\HP\Downloads\never_gonna_give_you_up.mp3",
            )
        )
        await ctx.send(f"Connected to {voice_channel.name}, playing audio.")
    else:
        await ctx.send("You need to be in a voice channel to use this command")
```

This is how you play local audio files.

Remember to replace Path and source with your own values.&#x20;

Eg. C:\KMPlayer\ffmprg.exe for me. You can find the path through searching in file explorer on windows.

![](/files/-MjwBIGxscMC_m_9JH4b)

### Can you play music through YouTube or Spotify?

Its possible to so by using libraries such as YouTube-dl, however,  downloading videos from **YouTube is in breach of YouTube's Terms** of Service, and the company could sue you.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://brucecodes.gitbook.io/pycord/guide/voice-commands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
