Why Add AI Voice to Your Discord Bot?
Discord is already a hub for real‑time chat, but when you give your bot a voice, the experience jumps to a whole new level. Whether you want to read out announcements, narrate game events, or let users hear a cloned version of their own voice, text‑to‑speech (TTS) and voice‑cloning APIs make it possible with just a few lines of code. In this post I’ll walk you through wiring up a Discord bot that speaks using ElevenLabs – a high‑quality, low‑latency TTS service that also offers voice cloning. By the end you’ll have a working bot that can:
- Join a voice channel on command
- Convert any text message into natural‑sounding speech
- (Optional) Use a custom cloned voice for a more personal touch
Let’s dive in.
Prerequisites
| What you need | Why |
|---|---|
| Python 3.9+ (or Node.js if you prefer) | The examples use discord.py but the same concepts apply to any language. |
| A Discord application & bot token | To connect to Discord’s gateway and voice servers. |
| An ElevenLabs API key | You’ll need this to call the TTS endpoint. Sign up at the affiliate link: https://try.elevenlabs.io/kr07zfuqn1bp. |
ffmpeg installed and in your PATH |
Discord expects audio in Opus format; ffmpeg does the conversion for us. |
| Basic familiarity with async code | Both Discord and ElevenLabs use async HTTP calls. |
Install the Python dependencies:
pip install -U discord.py[voice] aiohttp python-dotenv
Getting Your ElevenLabs API Key
- Go to the affiliate sign‑up page: https://try.elevenlabs.io/kr07zfuqn1bp.
- Register (or log in) and navigate to API Keys.
- Copy the key – you’ll store it in a
.envfile like this:
DISCORD_TOKEN=YOUR_DISCORD_BOT_TOKEN
ELEVENLABS_API_KEY=YOUR_ELEVENLABS_API_KEY
Tip: Keep your
.envfile out of version control (.gitignore) so you don’t accidentally publish secrets.
Connecting the Bot to a Voice Channel
Below is a minimal bot that responds to !join and !leave commands.
# bot.py
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.command(name="join")
async def join(ctx):
if ctx.author.voice:
channel = ctx.author.voice.channel
await channel.connect()
await ctx.send(f"👋 Joined **{channel.name}**")
else:
await ctx.send("❌ You need to be in a voice channel first!")
@bot.command(name="leave")
async def leave(ctx):
if ctx.voice_client:
await ctx.voice_client.disconnect()
await ctx.send("👋 Bye!")
else:
await ctx.send("❌ I'm not in a voice channel.")
bot.run(TOKEN)
Run it with python bot.py. The bot will now hop into the same voice channel as the user who typed !join.
Turning Text into Speech with ElevenLabs
ElevenLabs provides a simple HTTP endpoint. Here’s a helper that sends text and returns a stream of raw audio bytes:
import aiohttp
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
ELEVENLABS_TTS_URL = "https://api.elevenlabs.io/v1/text-to-speech/EXAMPLE_VOICE_ID"
async def synthesize(text: str, voice_id: str = "EXAMPLE_VOICE_ID") -> bytes:
headers = {
"xi-api-key": ELEVENLABS_API_KEY,
"Content-Type": "application/json",
}
payload = {
"text": text,
"model_id": "eleven_monolingual_v1", # high‑quality English model
"voice_settings": {"stability": 0.75, "similarity_boost": 0.85},
}
async with aiohttp.ClientSession() as session:
async with session.post(
f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
json=payload,
headers=headers,
) as resp:
if resp.status != 200:
raise RuntimeError(f"TTS failed: {await resp.text()}")
return await resp.read() # raw wav/pcm data
Replace EXAMPLE_VOICE_ID with the ID of the default “Rachel” voice (or any voice you like). You can find voice IDs in the ElevenLabs dashboard.
Streaming the Audio to Discord
Discord expects Opus‑encoded audio packets. The easiest way is to pipe the raw PCM into ffmpeg and feed the output to a Discord AudioSource.
from discord import PCMVolumeTransformer, FFmpegPCMAudio
import io
async def play_text(ctx, text: str):
if not ctx.voice_client:
await ctx.send("❌ I'm not connected to a voice channel.")
return
# 1️⃣ Get the TTS audio bytes
voice_id = "EXAMPLE_VOICE_ID"
wav_bytes = await synthesize(text, voice_id)
# 2️⃣ Write bytes to an in‑memory buffer
audio_buffer = io.BytesIO(wav_bytes)
# 3️⃣ Use ffmpeg to convert to Opus (Discord's preferred codec)
ffmpeg_options = {
"before_options": "-nostdin",
"options": "-vn -acodec libopus -f s16le -ar 48000 -ac 2",
}
source = FFmpegPCMAudio(audio_buffer, **ffmpeg_options)
source = PCMVolumeTransformer(source, volume=0.8)
# 4️⃣ Play!
ctx.voice_client.play(source, after=lambda e: print(f"Finished playing: {e}"))
await ctx.send(f"🔊 Speaking: *{text[:50]}…*")
Add a new command that ties everything together:
@bot.command(name="say")
async def say(ctx, *, message: str):
"""Make the bot speak the given message."""
await play_text(ctx, message)
Now !say Hello Discord! will make the bot join the channel (if not already there) and read the text aloud using ElevenLabs’ natural voice.
Optional: Voice Cloning
One of the coolest features of ElevenLabs is voice cloning – you can upload a few seconds of a user’s voice and get a custom voice ID. Here’s a quick curl example (you can adapt it to Python later):
curl -X POST "https://api.elevenlabs.io/v1/voices/add" \
-H "xi-api-key: YOUR_ELEVENLABS_API_KEY" \
-F "name=MyClone" \
-F "files=@/path/to/voice_sample.wav"
The response contains a new voice_id. Use that ID in the synthesize function to have the bot speak as you.
Pro tip: Keep the cloned voice private. ElevenLabs lets you set a “public” flag – leave it off unless you intend to share the voice with others.
Putting It All Together
Your final bot.py should look roughly like this (omitting imports for brevity):
# ... imports and env loading ...
@bot.command(name="join")
async def join(ctx): ...
@bot.command(name="leave")
async def leave(ctx): ...
@bot.command(name="say")
async def say(ctx, *, message: str):
if not ctx.voice_client:
await ctx.invoke(join) # auto‑join if needed
await play_text(ctx, message)
bot.run(TOKEN)
Run the script, invite the bot to your server with the applications.commands and bot scopes, and you’re ready to speak!
Troubleshooting Common Issues
| Symptom | Likely Cause | Fix |
|---|---|---|
Bot stays silent after !say
|
ffmpeg not in PATH or wrong options |
Verify ffmpeg -version works in your terminal. |
RuntimeError: TTS failed |
Invalid API key or voice ID | Double‑check the key in .env and the voice ID from ElevenLabs. |
| Audio is garbled | Mismatch in sample rate (48 kHz) | Ensure the ffmpeg options include -ar 48000. |
| Bot disconnects after a few seconds | Discord voice timeout | Keep the bot playing a silent loop or handle the after callback to stay connected. |
Next Steps
- Add caching – store generated audio files locally to avoid re‑synthesizing the same phrase.
- Integrate with slash commands – Discord’s newer interaction model feels cleaner.
-
Experiment with prosody – ElevenLabs’
voice_settingslet you tweak stability, speed, and emphasis.
Wrap‑Up
Adding AI voice to a Discord bot is now a matter of a few API calls and a little ffmpeg plumbing. With ElevenLabs you get studio‑grade TTS and the ability to clone voices, all behind a simple REST endpoint. The code above gives you a solid foundation; from here you can build announcement bots, immersive RPG narrators, or even a personal AI assistant that sounds exactly like you.
Ready to give your bot a voice? Grab your ElevenLabs API key at the affiliate link below, plug it into the example, and watch your Discord community react to the new soundscape.
Try ElevenLabs today and let your bots speak for themselves! https://try.elevenlabs.io/kr07zfuqn1bp
Top comments (0)