Discord bots with AI features face a unique scaling problem. Your server might have 10 users or 10,000, and message volume is unpredictable — quiet for hours, then suddenly everyone is pinging the bot at once during a discussion. If you are paying per-token to a single provider, a busy evening in your Discord can burn through your weekly budget. And if that provider has a rate limit or outage, your bot goes silent right when everyone is using it.
Antbase solves both problems. On the cost side, it classifies each message and routes simple questions ("what does this error mean," "translate this to Japanese") to fast free models while sending complex requests ("review this code," "design a database schema for this use case") to premium models. In a typical Discord server, 80% of bot interactions are simple questions that cost nothing with Antbase. On the reliability side, Antbase has built-in failover — if one provider hits its rate limit during a burst of messages, requests automatically route to another provider.
This combination means you can run an AI-powered Discord bot for a large server without constantly monitoring costs or worrying about uptime. The bot just works — fast responses for casual questions, thoughtful responses for complex ones, and no single point of failure.
A Discord bot backed by Antbase gives your server an AI assistant with intelligent model routing. Simple questions get fast, free models; complex coding or analysis requests get premium ones. This guide uses discord.js and the OpenAI SDK.
Setup
npm install discord.js openai
# Set environment variables
export DISCORD_TOKEN="your-discord-bot-token"
export ANTBASE_API_KEY="ant_your-api-key"Bot Code
import { Client, GatewayIntentBits } from 'discord.js';
import OpenAI from 'openai';
const discord = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const ai = new OpenAI({
baseURL: 'https://antbase.ai/v1',
apiKey: process.env.ANTBASE_API_KEY,
});
discord.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (!message.mentions.has(discord.user!)) return;
const content = message.content.replace(/<@!?\d+>/g, '').trim();
if (!content) return;
await message.channel.sendTyping();
const response = await ai.chat.completions.create({
model: 'auto',
messages: [
{ role: 'system', content: 'You are a helpful Discord bot. Keep replies under 2000 characters.' },
{ role: 'user', content },
],
});
const reply = response.choices[0].message.content ?? 'No response.';
await message.reply(reply.slice(0, 2000));
});
discord.login(process.env.DISCORD_TOKEN);Thread-Based Conversations
For multi-turn conversations, use Discord threads. When the bot gets a message in a thread, fetch the thread history and include it as context:
// Fetch thread messages for context
const threadMessages = await message.channel.messages.fetch({ limit: 20 });
const history = threadMessages.reverse().map((m) => ({
role: m.author.bot ? 'assistant' as const : 'user' as const,
content: m.content,
}));Tips
- •Discord has a 2000-character message limit. Truncate or split long AI responses.
- •Use sendTyping() before the AI call so users see the "Bot is typing..." indicator.
- •For slash commands, register them via the Discord developer portal and handle them with discord.js interaction handlers.
- •Rate limit your bot to avoid hitting Discord API limits — one AI response per user per 5 seconds is a safe default.



