# 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!](https://2961845821-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjPk-Yu4sOq8KGrr_yG%2F-MjvoS1BrdV7cECJK_lD%2F-MjvqljdMewywUvDky0Q%2Fimage.png?alt=media\&token=4159adb9-a1d5-468f-b723-4b23fdb0eb85)

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!](https://2961845821-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjPk-Yu4sOq8KGrr_yG%2F-MjvoS1BrdV7cECJK_lD%2F-Mjvqum75b_69GTbwZQN%2Fimage.png?alt=media\&token=f8eb4e21-2459-461a-b703-1ea47a454f29)

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](https://2961845821-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjPk-Yu4sOq8KGrr_yG%2F-MjvvaWiVqVSylVRO-7J%2F-MjvxjgKT3_DstuIk3M1%2Fimage.png?alt=media\&token=abb19323-0de9-461a-b6c2-4df80709eebe)

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.

![](https://2961845821-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MjPk-Yu4sOq8KGrr_yG%2F-Mjw00fjnEorTW0tuw2m%2F-MjwBIGxscMC_m_9JH4b%2Fimage.png?alt=media\&token=73a4c216-c4b0-4841-b421-8106a586ba71)

### 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.
