DEV Community

hardik singh
hardik singh

Posted on

record VC audio in discord!

I have been working on this side project where I can record the audio of a vc with the help of

in this post ill talk about how did I achieve this. you can also check out the YouTube video i made about this.

before we get to the fun part, you will have to make and set up a bot via the discord developer portal. you will also need to have a fairly good amount of knowledge about and how it works!

now once you have a bot with a basic test command working, we can import the required modules!

import discord
from discord.ext import commands
from discord.ext.audiorec import NativeVoiceClient #important!
from secrets import token #bot's secret token
import random
Enter fullscreen mode Exit fullscreen mode

now to record audio in the vc, we need to first make the bot join the vc in which the user is, which can be done using this command:

async def join(ctx: commands.Context):
    channel: discord.VoiceChannel =
    if ctx.voice_client is not None:
        return await ctx.voice_client.move_to(channel)
    await channel.connect(cls=NativeVoiceClient)
    await ctx.invoke(client.get_command('rec'))
Enter fullscreen mode Exit fullscreen mode

as you can see, this command invokes a command named 'rec'. this 'rec' command is the main command which we will use to record the audio in the vc!

async def rec(ctx):
    ctx.voice_client.record(lambda e: print(f"Exception: {e}"))
    embedVar = discord.Embed(title="Started the Recording!",
                             description="use !stop to stop!", color=0x546e7a)
    await ctx.send(embed=embedVar)
Enter fullscreen mode Exit fullscreen mode

we start the recording and then send an embed notifying the user that the recording has started!
the recording can be stopped and saved by using this command

async def stop(ctx: commands.Context):
    if not ctx.voice_client.is_recording():
    await ctx.send(f'Stopping the Recording')

    wav_bytes = await ctx.voice_client.stop_record()

    name = str(random.randint(000000, 999999))
    with open(f'{name}.wav', 'wb') as f:
    await ctx.voice_client.disconnect()
Enter fullscreen mode Exit fullscreen mode

over here the bot basically gets all the audio data in the form of bytes and then writes al the data into a wav file(this file's name is randomly generated), which is then saved to the system
now since the bot's job is done, it leaves the vc!

some extras:
you can also add commands to make sure there is someone in the vc before the bot joins the vc, or uploading the audio file to a server on the cloud. all these points are discussed in my youtube video, feel free to check it out!

all the code for this project is available on github too!

GitHub logo realhardik18 /

repo for recording the vc in an audio channel in

Top comments (1)

victorbcls profile image
Victor Barcelos

I cant find shit about the "from discord.ext.audiorec import NativeVoiceClient"
where is this library?