ai newsDo., 15. Okt.
EilmeldungMeta and Apple team up to make Muse AI models run better on Macs

Adding AI Voice to Discord Bots

Why Voice Matters for Discord Bots If you’ve built a Discord bot before, you already know how powerful text commands and reactions can be. But imagine a bot that can talk back to your server—answering questions, announcing events, or even just saying “Hey, you’re doing great!” in a natural‑sounding

dev.toMittwoch 03:00 · 14.10.4 Min. Lesezeit
Bild: dev.to

If you’ve built a Discord bot before, you already know how powerful text commands and reactions can be. But imagine a bot that can talk back to your server—answering questions, announcing events, or even just saying “Hey, you’re doing great!” in a natural‑sounding voice. That’s the magic of AI‑powered text‑to‑speech (TTS) and voice cloning, and it’s becoming surprisingly accessible thanks to modern cloud APIs.

Most Discord bots don’t need full voice cloning unless you’re building a niche experience. For most projects, a high‑quality TTS engine is sufficient and far easier to implement.

ElevenLabs offers an API that delivers near‑real‑time, expressive voices. Their models support dozens of languages, and the voice quality is on par with many premium TTS providers. What’s more, the API is straightforward to call from any language that can make HTTP requests.

If you’re ready to give your bot a voice, give ElevenLabs a try at https://try.elevenlabs.io/kr07zfuqn1bp. The link grants you a free trial that’s perfect for experimentation.

Tip: Store your tokens in environment variables or a.env file to keep them secret.

import os import discord import aiohttp import asyncio from dotenv import load_dotenv load_dotenv () TOKEN = os. getenv ( " DISCORD_BOT_TOKEN " ) ELEVENLABS_API_KEY = os. getenv ( " ELEVENLABS_API_KEY " ) ELEVENLABS_ENDPOINT = " https://api.elevenlabs.io/v1/text-to-speech/eleven_monolingual_v1 " intents = discord. Intents. default () intents. message_content = True client = discord. Client ( intents = intents ) async def synthesize ( text: str ) -> bytes: """ Call ElevenLabs TTS API and return raw MP3 bytes.

""" headers = { " xi-api-key ": ELEVENLABS_API_KEY, " Content-Type ": " application/json ", } payload = { " text ": text, " voice_settings ": { " stability ": 0.5, " similarity_boost ": 0.75, " style ": 0.5, " use_speaker_boost ": False } } async with aiohttp. ClientSession () as session: async with session. post ( ELEVENLABS_ENDPOINT, json = payload, headers = headers ) as resp: if resp. status!= 200: raise Exception ( f " ElevenLabs error: { resp. status } " ) return await resp. read () @client.event async def on_message ( message ): if message. author. bot: return if message. content. startswith ( "!speak " ): text = message. content [ len ( "!speak " ):] # Find the voice channel the user is in if not message. author. voice: await message. channel. send ( " You’re not connected to a voice channel! " ) return vc = message. author. voice. channel # Connect to voice channel if not already if not vc. guild. me. voice: await vc. connect () # Synthesize audio try: audio_bytes = await synthesize ( text ) except Exception as e: await message. channel. send ( f " Error: { e } " ) return # Create an audio source source = discord.

FFmpegPCMAudio ( io. BytesIO ( audio_bytes )) vc. play ( source ) await message. channel. send ( f " Playing: { text } " ) client. run ( TOKEN ) Enter fullscreen mode Exit fullscreen mode What’s Happening? synthesize – Sends a JSON payload to ElevenLabs and receives an MP3. The voice_settings can be tweaked to adjust the bot’s tone.

Voice Channel Handling – The bot checks if the user is in a channel and joins if necessary. Playing Audio – FFmpegPCMAudio streams the MP3 directly to Discord. No disk writes!

5. Handling Latency and Streaming When you call the ElevenLabs API, the response can take ~500 ms to 1 s depending on load. In a busy server, this can add up.

Two common strategies:

Below is a quick snippet using asyncio.StreamReader to stream directly:

async def stream_to_ffmpeg ( audio_url: str, vc: discord. VoiceClient ): async with aiohttp. ClientSession () as session: async with session. get ( audio_url ) as resp: stream = resp. content ffmpeg = await asyncio. create_subprocess_exec ( " ffmpeg ", " -i ", " pipe:0 ", " -f ", " s16le ", " -ar ", " 48000 ", " -ac ", " 2 ", " - ", stdin = asyncio. subprocess. PIPE, stdout = asyncio. subprocess. PIPE ) while True: chunk = await stream. read ( 1024 ) if not chunk: break ffmpeg. stdin. write ( chunk ) await ffmpeg. stdin. drain () await ffmpeg. communicate () vc. play ( discord. FFmpegPCMAudio ( " pipe:0 " )) Enter fullscreen mode Exit fullscreen mode 6. Voice Cloning (Optional) If you’re adventurous, ElevenLabs also offers voice cloning. The workflow is:

Once you have a working TTS bot, consider adding:

Ready to make your Discord bot sound like a human? Start experimenting with ElevenLabs today and unlock high‑quality TTS for free. Grab your trial at https://try.elevenlabs.io/kr07zfuqn1bp, integrate the API, and watch your bot’s conversations come to life. Happy coding!

Quelle: dev.to (dev.to). Original öffnen