Discord is where many online communities, streamers and gamers spend their evenings, and its voice channels are the heart of it. It’s also easy to extend: a bot can add new features to any server you run.
In this tutorial, you’ll build a Discord voice bot in Node.js that joins a voice channel, listens to whoever is speaking, sends what they said to a GPT model, and speaks the answer back into the channel. The whole pipeline runs on one AssemblyAI API key: streaming speech-to-text for the listening, LLM Gateway for the GPT reply, and AssemblyAI text-to-speech for the voice. You don’t need an OpenAI or ElevenLabs account.
The finished code is in the companion repo: kelsey-aai/discord-voice-bot-assemblyai.
How the bot works
Every conversation turn goes through the same five steps:
- You type !join in a text channel while you’re in a voice channel, and the bot joins you.
- When someone starts speaking, the bot subscribes to their audio. Discord delivers it as 48 kHz stereo Opus packets.
- The bot decodes each packet to 16 kHz mono PCM and streams it to AssemblyAI over a WebSocket. One second of silence ends the utterance, and the bot collects the final transcript.
- The transcript goes to a GPT model through LLM Gateway, with the last few turns of the conversation as context.
- AssemblyAI text-to-speech turns the reply into 48 kHz audio, and the bot plays it into the voice channel.
Here’s what handles each step:
| Step | What handles it | Model or voice ID |
|---|---|---|
| Listen | AssemblyAI-STT-3.7-Realtime-Agent (streaming speech-to-text) | assemblyai-stt-3-7-realtime-agent |
| Think | GPT-6 Luna through AssemblyAI LLM Gateway | gpt-6-luna |
| Speak | AssemblyAI text-to-speech, Kyla voice | assemblyai-tts-1 + aai-kyla |
| Discord audio in and out | discord.js, @discordjs/voice, prism-media | n/a |
Before you start
This tutorial assumes you know JavaScript and are comfortable with async/await and npm. You’ll need:
- Node.js 22.12 or later. The current @discordjs/voice release (0.19) requires it, and it’s the release that supports Discord’s end-to-end encrypted voice (the DAVE protocol).
- A Discord server where you can add bots.
- An AssemblyAI API key.
Set up the project
Create a project folder, switch it to ES modules, and install the dependencies:
mkdir discord-voice-bot && cd discord-voice-bot
npm init -y
npm pkg set type=module
npm install discord.js @discordjs/voice @discordjs/opus prism-media assemblyai dotenvHere’s what each package does:
- discord.js connects to Discord and handles text commands.
- @discordjs/voice joins voice channels, receives audio and plays audio. On Node 22 it encrypts voice with the built-in aes-256-gcm cipher, so you don’t need a separate sodium library.
- @discordjs/opus and prism-media decode the Opus audio Discord sends and encode the audio the bot plays.
- assemblyai is the AssemblyAI Node SDK. It covers streaming speech-to-text, LLM Gateway and text-to-speech.
- dotenv loads your credentials from a .env file.
Store your credentials
Create a file named .env in the project folder with two variables. You’ll fill them in over the next two sections:
ASSEMBLYAI_API_KEY=
DISCORD_TOKEN=Add .env to your .gitignore so the keys never reach source control. Copy the AssemblyAI key from your dashboard into ASSEMBLYAI_API_KEY now.
Next, create config.js. It loads the .env file, stops with a clear message if either variable is missing, and creates the one AssemblyAI client the rest of the bot shares:
import "dotenv/config";
import { AssemblyAI } from "assemblyai";
const { ASSEMBLYAI_API_KEY, DISCORD_TOKEN } = process.env;
if (!ASSEMBLYAI_API_KEY || !DISCORD_TOKEN) {
console.error(
"Missing credentials. Copy .env.example to .env and set ASSEMBLYAI_API_KEY and DISCORD_TOKEN."
);
process.exit(1);
}
// One AssemblyAI client covers speech-to-text, LLM Gateway and text-to-speech.
export const assemblyai = new AssemblyAI({ apiKey: ASSEMBLYAI_API_KEY });
export const discordToken = DISCORD_TOKEN;Create the Discord application
Open the Discord Developer Portal, click New Application and give it a name. Then:
- Open the Bot page and click Reset Token. Paste the token into .env as DISCORD_TOKEN.
- On the same page, turn on Message Content Intent under Privileged Gateway Intents. Without it, the bot can’t read the !join command.
- Open OAuth2, then the URL generator. Under Scopes, select bot. Under Bot Permissions, select View Channels, Send Messages, Read Message History, Connect and Speak. You don’t need Administrator.
- Copy the generated URL, open it in your browser and add the bot to your server.
Streaming speech-to-text, LLM Gateway and text-to-speech all run on the same AssemblyAI API key. Get a free key and start building.
Record and transcribe speech in real time
Discord gives each speaker their own audio stream, so the bot can transcribe one person at a time with no speaker separation to untangle. The receiver on a voice connection exposes subscribe(userId), which returns a stream of that user’s Opus packets. With EndBehaviorType.AfterSilence, the stream ends on its own after a set stretch of silence. That gives you a natural end to each utterance.
The bot streams that audio to AssemblyAI-STT-3.7-Realtime-Agent (assemblyai-stt-3-7-realtime-agent), the real-time model built for voice agents. The streaming API’s default input is 16-bit mono PCM at 16 kHz. Discord sends 48 kHz stereo Opus, so the bot decodes each packet with prism-media and asks the Opus decoder for 16 kHz mono output directly. libopus can decode at 16 kHz natively, so there’s no separate resampling step. Each 20 ms packet becomes 640 bytes of PCM, inside the API’s 20 to 1,000 ms range per audio message.
Create transcribe.js:
import { once } from "node:events";
import prism from "prism-media";
import { EndBehaviorType } from "@discordjs/voice";
import { assemblyai } from "./config.js";
const SAMPLE_RATE = 16_000;
// Records one utterance from a Discord user and returns the final transcript.
export async function transcribeUtterance(receiver, userId) {
const transcriber = assemblyai.streaming.transcriber({
// The SDK's TypeScript types don't list the 3.7 model IDs yet; the API accepts them.
speechModel: "assemblyai-stt-3-7-realtime-agent",
sampleRate: SAMPLE_RATE,
encoding: "pcm_s16le",
});
const finalTurns = [];
transcriber.on("turn", (turn) => {
if (turn.end_of_turn && turn.transcript) finalTurns.push(turn.transcript);
});
transcriber.on("error", (error) => console.error("Streaming error:", error));
// Discord sends 48 kHz stereo Opus packets. The stream ends after 1 s of silence.
const opusStream = receiver.subscribe(userId, {
end: { behavior: EndBehaviorType.AfterSilence, duration: 1_000 },
});
// Decode to 16 kHz mono 16-bit PCM: the streaming API's default format.
// libopus decodes at 16 kHz natively, so no separate resampling step is needed.
const pcmStream = opusStream.pipe(
new prism.opus.Decoder({ rate: SAMPLE_RATE, channels: 1, frameSize: 320 })
);
const ended = once(pcmStream, "end");
// Hold audio that arrives while the WebSocket is still connecting.
const pending = [];
let connected = false;
pcmStream.on("data", (chunk) => {
if (connected) transcriber.sendAudio(chunk);
else pending.push(chunk);
});
await transcriber.connect();
connected = true;
for (const chunk of pending) transcriber.sendAudio(chunk);
await ended;
// close() sends Terminate, which finalizes the open turn before the session ends.
await transcriber.close();
return finalTurns.join(" ").trim();
}A few details are worth knowing:
- The WebSocket takes a moment to open. The person is already talking when the bot starts the session, so the first packets go into a short buffer and are sent as soon as connect() resolves. Without the buffer, the first word or two of every question would go missing.
- turn events carry the text. Each turn event has a transcript and an end_of_turn flag. The bot keeps only finished turns (end_of_turn is true) and joins them, since one utterance can hold more than one turn.
- close() finalizes the last turn. It sends a Terminate message and waits for the session to end, and the server finalizes the open turn before it does. That’s why the function awaits close() before returning the transcript.
The SDK’s TypeScript types don’t list the 3.7 model IDs yet. This project is plain JavaScript, so the string passes straight through. If you port it to TypeScript, cast the value. For the full message flow, see the streaming message sequence in the docs.
Generate a reply with ChatGPT through LLM Gateway
LLM Gateway is an OpenAI-compatible chat completions API that reaches 55+ models from OpenAI, Anthropic, Google and others with your AssemblyAI key. This bot uses gpt-6-luna, the lowest-priced GPT-6 model on LLM Gateway, which is plenty for short conversational replies. You can swap in any ID from the LLM Gateway models list.
Create reply.js:
import { assemblyai } from "./config.js";
const SYSTEM_PROMPT =
"You are a friendly voice assistant in a Discord voice channel. " +
"Answer in one to three short sentences of plain spoken English. " +
"Don't use markdown, lists, links or emoji: your reply is read aloud.";
const MAX_HISTORY = 20; // user + assistant messages kept per server
const histories = new Map(); // guildId -> messages
export async function getReply(guildId, userText) {
const history = histories.get(guildId) ?? [];
history.push({ role: "user", content: userText });
const completion = await assemblyai.llmGateway.chatCompletions({
model: "gpt-6-luna",
messages: [{ role: "system", content: SYSTEM_PROMPT }, ...history],
max_tokens: 500,
reasoning_effort: "low",
});
const reply = completion.choices[0].message.content.trim();
history.push({ role: "assistant", content: reply });
histories.set(guildId, history.slice(-MAX_HISTORY));
return reply;
}The system prompt does most of the work. A voice reply can’t show a bulleted list or a link, so the prompt asks for one to three plain sentences. The bot keeps a short history per server, so follow-up questions such as “what about the second one?” make sense. reasoning_effort is set to low because a quick answer matters more than deep reasoning in a voice chat. gpt-6-luna lists reasoning_effort as a supported parameter. If you switch to a model that doesn’t, remove the field, since those models return a 400 error when it’s sent.
Speak the reply with AssemblyAI text-to-speech
The last step turns the reply into audio. AssemblyAI text-to-speech (assemblyai-tts-1) speaks six languages, and this bot uses the Kyla voice (aai-kyla), set explicitly. Browse the others on the voices page.
The TTS API can return raw 16-bit PCM at 48 kHz, which is the sample rate Discord plays. @discordjs/voice expects raw input as 48 kHz stereo, and the TTS audio is mono, so the bot copies each sample into both channels and hands the buffer to createAudioResource with StreamType.Raw. That skips FFmpeg entirely.
Create speak.js:
import { Readable } from "node:stream";
import {
AudioPlayerStatus,
StreamType,
createAudioPlayer,
createAudioResource,
entersState,
} from "@discordjs/voice";
import { assemblyai } from "./config.js";
// Discord plays raw audio as 48 kHz, 16-bit, stereo PCM.
const DISCORD_SAMPLE_RATE = 48_000;
function monoToStereo(mono) {
const stereo = Buffer.alloc(mono.length * 2);
for (let i = 0; i + 1 < mono.length; i += 2) {
mono.copy(stereo, i * 2, i, i + 2); // left
mono.copy(stereo, i * 2 + 2, i, i + 2); // right
}
return stereo;
}
export async function speak(connection, text) {
const { audio } = await assemblyai.tts.synthesize({
text,
model_id: "assemblyai-tts-1",
voice_id: "aai-kyla",
sample_rate: DISCORD_SAMPLE_RATE,
encoding: "pcm_s16le", // raw mono PCM, no container
});
const mono = Buffer.from(audio.buffer, audio.byteOffset, audio.byteLength);
const resource = createAudioResource(
Readable.from([monoToStereo(mono)], { objectMode: false }),
{ inputType: StreamType.Raw }
);
const player = createAudioPlayer();
connection.subscribe(player);
player.play(resource);
await entersState(player, AudioPlayerStatus.Playing, 5_000);
await entersState(player, AudioPlayerStatus.Idle, 5 * 60_000);
player.stop();
}synthesize() waits for the whole clip and returns it as one buffer, which fits short replies. The function then waits for the player to start and finish, so the bot doesn’t listen to the next question while it’s still talking. For a deeper look at the TTS API, including streaming and temporary tokens for browser apps, see JavaScript text to speech, the easy way.
Test real-time transcription on your own voice in the browser, then wire the same model into your bot. No setup required.
Put it all together
index.js ties the pieces together. It does three jobs:
- !join calls joinVoiceChannel with the channel, the server and its voice adapter, then waits until the connection is ready. selfDeaf: false matters here, because the library deafens the bot by default, and a deafened bot receives no audio.
- !leave destroys the connection.
- listenAndRespond listens for the receiver’s speaking events. When someone starts talking, it runs one full turn: transcribe, reply, speak. A busy flag keeps it to one turn at a time, so the bot ignores new speech until it has finished answering.
Create index.js:
import { discordToken } from "./config.js";
import { Client, Events, GatewayIntentBits } from "discord.js";
import {
VoiceConnectionStatus,
entersState,
getVoiceConnection,
joinVoiceChannel,
} from "@discordjs/voice";
import { transcribeUtterance } from "./transcribe.js";
import { getReply } from "./reply.js";
import { speak } from "./speak.js";
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
],
});
client.once(Events.ClientReady, (c) => console.log(`Logged in as ${c.user.tag}`));
client.on(Events.MessageCreate, async (message) => {
if (message.author.bot || !message.guild) return;
const command = message.content.trim().toLowerCase();
if (command === "!join") {
const channel = message.member?.voice.channel;
if (!channel) {
await message.reply("Join a voice channel first, then send !join.");
return;
}
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
selfDeaf: false, // a deafened bot receives no audio
});
try {
await entersState(connection, VoiceConnectionStatus.Ready, 30_000);
} catch {
connection.destroy();
await message.reply("I couldn't connect to the voice channel.");
return;
}
await message.reply(`Joined ${channel.name}. Talk to me!`);
listenAndRespond(connection, message.guild.id);
}
if (command === "!leave") {
getVoiceConnection(message.guild.id)?.destroy();
await message.reply("Left the voice channel.");
}
});
function listenAndRespond(connection, guildId) {
let busy = false;
connection.receiver.speaking.on("start", async (userId) => {
if (busy) return; // one conversation turn at a time
busy = true;
try {
const text = await transcribeUtterance(connection.receiver, userId);
if (!text) return;
console.log(`User ${userId}: ${text}`);
const reply = await getReply(guildId, text);
console.log(`Bot: ${reply}`);
await speak(connection, reply);
} catch (error) {
console.error("Turn failed:", error);
} finally {
busy = false;
}
});
}
client.on(Events.Error, console.error);
client.login(discordToken).catch((error) => {
console.error(`Discord login failed: ${error.message}`);
process.exit(1);
});Start the bot:
node index.jsJoin a voice channel in your server, type !join in a text channel, and ask a question out loud. The console logs each turn in this format:
Logged in as VoiceBot#0420
User 318204761190711296: What's a good name for a Minecraft server?
Bot: How about Blocktopia? It's short, easy to remember and sounds like a world worth exploring.If ASSEMBLYAI_API_KEY or DISCORD_TOKEN is missing, the bot exits straight away and names the two variables it needs. An invalid Discord token ends with “Discord login failed” and the reason.
Where to take it next
You now have a Discord bot that listens, thinks and talks with one API key. A few directions to build on:
- Keyword activation. Reply only when a transcript starts with the bot’s name, so it doesn’t answer every remark in a busy channel.
- Tools. LLM Gateway supports tool calling, so the bot can look up a game’s patch notes or a server’s schedule before it answers.
- Faster replies. Switch synthesize() to the streaming TTS endpoint and start playback on the first chunk of audio.
The same three-step pipeline powers phone agents and in-app assistants too. For the design choices behind it, read how to choose a speech-to-text API for voice agents, or see the same pieces wired up outside Discord in building a Node.js voice agent with AssemblyAI and building a voice agent without Pipecat or LiveKit.
Transcription, GPT replies and natural speech from one API key. Sign up free and get your bot talking today.
Frequently Asked Questions (FAQs)
Can you add a ChatGPT bot to a Discord voice channel?
Yes. Discord has no built-in AI in voice channels, but a bot can join one, listen to each speaker’s audio stream and answer out loud. The bot in this tutorial sends each finished utterance to a GPT model through AssemblyAI’s LLM Gateway and plays the reply with AssemblyAI text-to-speech, all with one API key.
Can Discord transcribe voice channels on its own?
No. Discord’s built-in text-to-speech reads chat messages aloud, but it doesn’t turn what people say in a voice channel into text. That takes a bot that joins the channel, subscribes to each speaker’s audio and sends it to a speech-to-text API, which is what this bot does.
What audio format does Discord voice use, and what does the speech-to-text API need?
Discord delivers voice audio as Opus packets at 48 kHz, one stream per speaker. AssemblyAI’s streaming API takes 16-bit mono PCM at 16 kHz by default, and the sample_rate you set must match the audio you send. A mismatch produces nonsense text without throwing an error. This bot decodes the Opus packets straight to 16 kHz mono with prism-media, so the format matches the default.
How do I add a bot to a Discord voice channel?
Create an application in the Discord Developer Portal, add the bot to your server with the bot scope and the Connect and Speak permissions, and call joinVoiceChannel from @discordjs/voice with the channel ID, the server ID and the server’s voice adapter. In this tutorial, the bot joins whichever voice channel you’re in when you type !join.
Why isn’t my Discord bot transcribing anything?
Check four things in order. First, the bot must join with selfDeaf set to false, because a deafened bot receives no audio. Second, the GuildVoiceStates and MessageContent intents must be on in code, and Message Content Intent must be on in the Developer Portal. Third, the decoded audio must match the sample rate you send to the transcriber. Fourth, open a new streaming session for each utterance: once you close a session, it can’t take more audio.
Which Node.js version does a Discord voice bot need?
Node.js 22.12 or later for the current @discordjs/voice release (0.19), which adds support for Discord’s DAVE end-to-end encryption for voice. Older releases of the library ran on Node 18 and 20 but don’t support DAVE.