Discord bots can record voice from voice channels using the Pycord library’s built-in recording module. This feature lets your bot capture audio from specific users, which is useful for meeting logs, podcast archiving, or transcription. The Pycord recording module handles the WebSocket and audio data streams automatically, so you do not need to manage raw Opus encoding or RTP packets. This article explains how to set up a Pycord bot, join a voice channel, start and stop recording, and save audio files to disk.
Key Takeaways: Pycord Discord Bot Voice Recording Setup
- Pycord VoiceRecvClient class: Replaces the default voice client to enable receiving audio from users.
- Bot.join_voice_channel method: Connects the bot to a voice channel and returns a VoiceRecvClient instance.
- VoiceRecvClient.listen method: Starts recording audio from all users or a specific user in the voice channel.
- VoiceRecvClient.stop_listening method: Stops the recording and frees the audio stream resources.
- Sinks (default, mp3, wav): Pycord audio sinks that convert Opus packets to common audio file formats.
Overview of the Pycord Recording Module
Pycord is a modern Python wrapper for the Discord API. Version 2.0 and above includes a voice recording module that lets bots receive audio from voice channels. The module uses the VoiceRecvClient class, which extends the standard voice client with listening capabilities. When the bot joins a voice channel, it can listen to all users who are speaking. The recorded audio is delivered as Opus packets. Pycord provides several built-in sinks that convert these Opus packets into common audio formats such as WAV and MP3. You can also write a custom sink if you need a different output format.
Before you start, you need a Discord bot token, Python 3.8 or later, and Pycord installed. Install Pycord with the voice extra: pip install py-cord[voice]. The bot must have the voice_states and guilds intents enabled. You can enable them in the Discord Developer Portal under the Bot section. The recording feature works in server voice channels. It does not work in group DMs or stage channels.
Steps to Record Voice With a Pycord Bot
Follow these steps to create a bot that joins a voice channel, records audio, and saves it to a file. The example uses the default sink, which outputs raw PCM data. For WAV or MP3 output, replace the sink with the appropriate class.
- Create the bot and enable intents
Create a new Python file. Import discord and create a Bot instance with the voice_states and guilds intents. Set the command prefix. Example:bot = discord.Bot(intents=discord.Intents.all()). Using all intents is the simplest approach for development. - Write a command to join a voice channel
Create a slash command or prefix command that takes a voice channel as an argument. Inside the command, callawait channel.connect(cls=discord.VoiceRecvClient). This connects the bot and returns a VoiceRecvClient instance. Store the client in a variable for later use. - Start recording audio
Callvoice_client.listen(discord.sinks.WaveSink())to start recording. The WaveSink saves audio as WAV files. You can passdiscord.sinks.MP3Sink()for MP3 output. To record only one user, pass the user’s ID as a second argument:voice_client.listen(sink, user_id). The bot will record only that user’s audio. - Stop recording and save the files
Callvoice_client.stop_listening()to stop the recording. After stopping, the sink object contains the recorded data. Access the data usingsink.audio_data, which is a dictionary mapping user IDs to their audio data. Write each user’s audio to a file. Example:with open(f"{user_id}.wav", "wb") as f: f.write(sink.audio_data[user_id].file.read()). - Leave the voice channel
After saving the files, callawait voice_client.disconnect()to leave the channel and free resources.
Common Mistakes and Limitations to Avoid
Bot does not receive audio after joining
This usually happens when the bot lacks the necessary intents. Make sure you enabled voice_states in the Discord Developer Portal and passed the correct intents when creating the Bot instance. Also check that the bot has the connect and speak permissions in the server.
Recorded audio is silent or has low volume
The default sink records audio at the original volume from Discord. If the file is silent, the user may not have been speaking. Use a sink that writes audio immediately to disk to verify that data is being captured. Also check that the sink’s encoding matches your player. For example, WaveSink outputs 16-bit 48 kHz PCM. If your player expects a different format, you may need to convert the file using a library like pydub.
Memory usage grows too high during long recordings
The default sinks store all audio in memory until you call stop_listening. For long recordings, this can consume a lot of RAM. Use a custom sink that writes audio to disk in real time. To create a custom sink, subclass discord.sinks.Sink and implement the write method to write audio data to a file as it arrives.
Comparison of Pycord Audio Sinks
| Item | WaveSink | MP3Sink | DefaultSink |
|---|---|---|---|
| Output format | WAV (16-bit PCM) | MP3 | Raw PCM |
| File extension | .wav | .mp3 | None (raw bytes) |
| Requires FFmpeg | No | Yes | No |
| Memory usage | Stores all audio in memory | Stores all audio in memory | Stores all audio in memory |
| Best for | Quick testing, archival | Smaller file size, sharing | Custom processing |
Pycord’s recording module is a reliable way to capture voice from Discord voice channels. You now know how to set up a bot, join a voice channel, start and stop recording with the listen and stop_listening methods, and save audio files using sinks. For production use, consider writing a custom sink that writes to disk continuously to avoid memory issues. The discord.sinks module also supports stereo recording and per-user files, which you can configure through the sink parameters.