ai newsMo., 26. Okt.
EilmeldungGPT-6.1 Terra halves OpenAI's API prices and still gains ground on its larger models

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, o

dev.toMontag 22:01 · 26.10.4 Min. Lesezeit
Bild: dev.to

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:

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.

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

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.

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

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

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? synthesize_text sends a POST request to ElevenLabs’ TTS endpoint. The bot listens for the!say command. It writes the returned MP3 to a temporary file (Discord’s FFmpegPCMAudio needs a file or a stream).

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.

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

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.

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!

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