Skip to content

Voice Support

So you want to start playing some 🎵tunes🎶 in voice channels? Well let's get that going for you.

First you're going to want to get the voice dependencies installed:

1
pip install dis-snek[voice]

Then you'll need to download FFmpeg and place it in your project directory or PATH.

Now you've got those; let's make a simple play command to get you started.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import naff
from naff.api.voice.audio import YTDLAudio


@naff.slash_command("play", "play a song!")
@naff.slash_option("song", "The song to play", 3, True)
async def play(self, ctx: naff.InteractionContext, song: str):
    if not ctx.voice_state:
        # if we haven't already joined a voice channel
        # join the authors vc
        await ctx.author.voice.channel.connect()

    # Get the audio using YTDL
    audio = await YTDLAudio.from_url(song)
    await ctx.send(f"Now Playing: **{audio.entry['title']}**")
    # Play the audio
    await ctx.voice_state.play(audio)

Now just join a voice channel, and type run the "play" slash command with a song of your choice.

Congratulations! You've got a music-bot.

But what about local music?

If you want to play your own files, you can do that too! Create an AudioVolume object and away you go.

Note

If your audio is already encoded, use the standard Audio object instead. You'll lose volume manipulation, however.

1
2
3
4
5
6
7
8
import naff
from naff.api.voice.audio import AudioVolume


@naff.slash_command("play", "play a song!")
async def play_file(ctx: naff.InteractionContext):
    audio = AudioVolume("some_file.wav")
    await ctx.voice_state.play(audio)

Check out Active Voice State for a list of available methods and attributes.