DEV Community

VoiceDeveloper
VoiceDeveloper

Posted on

Adding AI Voice to Discord Bots

Why Voice Matters for Discord Bots

If you’ve spent a few hours tweaking a text‑based bot, you’ve probably realized how much more engaging a bot can be when it talks. Voice lets your bot:

  • Deliver notifications in real time without the user having to read a message
  • Read out loud game stats, trivia, or news headlines
  • Offer a more natural “human” interaction, especially in gaming communities
  • Create immersive role‑playing or storytelling experiences

Adding voice isn’t a magic bullet, but it’s a powerful way to increase engagement. The good news? With modern text‑to‑speech (TTS) and voice‑cloning APIs, the barrier to entry is lower than ever.


Quick Primer on Text‑to‑Speech & Voice Cloning

Feature What It Does Typical Use‑Case
Text‑to‑Speech (TTS) Converts plain text into spoken audio. Announcements, reading messages aloud, narration.
Voice Cloning Trains a model on a small sample of a real voice and then can generate speech in that voice. Personalizing bots to sound like a particular user or brand voice.

Both can be combined: clone a voice, then feed the clone with dynamic content via TTS.


Choosing a Provider

There are several TTS/voice‑cloning services: Google Cloud Text‑to‑Speech, Amazon Polly, Azure Cognitive Services, and the newer players like ElevenLabs, Replica Studios, and Resemble AI.

Why ElevenLabs?

  • Extremely natural‑sounding voices (thanks to neural‑network‑based synthesis).
  • Fast API with low latency – perfect for real‑time Discord bot responses.
  • Voice cloning support with a simple “upload a few minutes of audio” workflow.
  • Competitive pricing and generous free tier.

If you want a quick, reliable start, ElevenLabs is a solid choice. You can sign up and get an API key here: https://try.elevenlabs.io/kr07zfuqn1bp


Setting Up Your Environment

Below is a minimal example in Python using discord.py and the ElevenLabs API. You’ll need:

  1. A Discord bot token (from the Discord Developer Portal).
  2. An ElevenLabs API key (from the link above).
  3. discord.py (v2.0+) and requests.
pip install discord.py requests
Enter fullscreen mode Exit fullscreen mode

Create a file called voice_bot.py:

import os
import discord
import requests
from discord import FFmpegPCMAudio
from discord.ext import commands

# --- Config ---------------------------------------------------------
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")   # put your token in an env var
ELEVEN_API_KEY = os.getenv("ELEVEN_API_KEY") # put your ElevenLabs key in an env var
ELEVEN_VOICE_ID = "21m00Tcm4TlvDq8ikWAM"     # example voice ID, replace with yours
BASE_URL = "https://api.elevenlabs.io/v1"

# --- ElevenLabs helper ------------------------------------------------
def synthesize_text(text: str) -> bytes:
    """
    Calls ElevenLabs TTS endpoint and returns raw audio bytes (MP3).
    """
    url = f"{BASE_URL}/text-to-speech/{ELEVEN_VOICE_ID}"
    headers = {
        "xi-api-key": ELEVEN_API_KEY,
        "Content-Type": "application/json"
    }
    payload = {"text": text}
    resp = requests.post(url, headers=headers, json=payload)
    resp.raise_for_status()
    return resp.content

# --- Discord Bot ----------------------------------------------------
intents = discord.Intents.default()
intents.message_content = True

bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")

@bot.command(name="say")
async def say(ctx, *, message: str):
    """
    Usage: !say <your message>
    Bot will speak the message aloud.
    """
    audio_bytes = synthesize_text(message)

    # Save to a temporary file (Discord expects a file or stream)
    with open("temp.mp3", "wb") as f:
        f.write(audio_bytes)

    # Play in the voice channel the user is in
    if ctx.author.voice and ctx.author.voice.channel:
        vc = ctx.author.voice.channel
        if not ctx.voice_client:
            await vc.connect()
        audio_source = FFmpegPCMAudio("temp.mp3")
        ctx.voice_client.play(audio_source)
        # Wait until the audio finishes
        while ctx.voice_client.is_playing():
            await discord.utils.sleep_until(discord.utils.utcnow() + discord.utils.timedelta(seconds=1))
        await ctx.voice_client.disconnect()
        os.remove("temp.mp3")
    else:
        await ctx.send("You need to be in a voice channel first!")

# --------------------------------------------------------------------
if __name__ == "__main__":
    bot.run(DISCORD_TOKEN)
Enter fullscreen mode Exit fullscreen mode

What’s happening?

  1. synthesize_text sends a POST request to ElevenLabs’ TTS endpoint.
  2. The bot listens for the !say command.
  3. It writes the returned MP3 to a temporary file (Discord’s FFmpegPCMAudio needs a file or a stream).
  4. The bot joins the user’s voice channel, plays the audio, and then leaves.

Feel free to replace the voice ID with your own or experiment with the free voices offered by ElevenLabs.


Voice Cloning in Practice

If you want your bot to sound like a specific person (or a custom brand voice), ElevenLabs offers a simple voice‑cloning workflow:

  1. Collect a 5–10 minute audio sample in a quiet environment.
  2. Upload it via the ElevenLabs dashboard or API (/voice-cloning).
  3. Wait for the model to train (usually a few minutes).
  4. Use the new voice ID in your ELEVEN_VOICE_ID variable.

Cloned voices can be used just like any other TTS voice – the same endpoint, same request body. That means you can keep the rest of your bot code unchanged and just swap out the voice ID.


Advanced Tips

Tip Why It Helps
Cache audio Re‑synthesizing identical text wastes API calls and increases latency. Store MP3s locally or in a Redis cache.
Use async HTTP aiohttp instead of requests to avoid blocking the bot’s event loop.
Chunk long messages Discord’s voice limit is ~20 seconds. Split longer texts and queue them.
Error handling ElevenLabs can return rate‑limit errors; implement exponential back‑off.
Security Never hard‑code API keys. Use environment variables or secret managers.

Common Pitfalls & How to Avoid Them

Pitfall Fix
Latency spikes Use async calls; keep the voice file in memory if possible.
Discord disconnections Ensure the bot leaves the channel after playback.
Audio quality issues Check your internet connection; ElevenLabs’ default MP3 quality is fine for most use cases, but you can tweak the bitrate in the payload if needed.
API limits Monitor your usage in the ElevenLabs dashboard; upgrade or add caching as needed.

Wrapping Up

Adding AI voice to a Discord bot is a fun and powerful way to boost engagement. With a simple TTS endpoint and a bit of audio handling, you can have your bot speak in a natural, expressive voice. ElevenLabs makes the process straightforward—great voices, easy API, and even voice cloning for that extra personal touch.

Give it a try: sign up at https://try.elevenlabs.io/kr07zfuqn1bp, get your API key, and start turning your Discord bot into a talking companion today!

Top comments (0)