Antbase/Documentation

Discord Bot Integration

Build an AI-powered Discord bot using Antbase and Node.js. The bot responds to messages and slash commands with intelligent model routing and automatic failover.

What this enables

  • AI chatbot in Discord channels and DMs
  • Slash commands for different AI tasks
  • Thread-based conversations with context
  • Automatic failover and model routing

Quick Start

1. Create a Discord application

Go to the Discord Developer Portal, create a new application, add a bot, and copy the token. Enable the Message Content Intent.

2. Install dependencies

npm init -y
npm install discord.js openai

3. Create the bot

// bot.js
import { Client, GatewayIntentBits } from 'discord.js';
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.ANTBASE_API_KEY,
  baseURL: 'https://antbase.ai/v1',
});

const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
});

// Per-channel conversation history
const conversations = new Map();

client.on('messageCreate', async (message) => {
  if (message.author.bot) return;
  if (!message.mentions.has(client.user)) return;

  const content = message.content.replace(/<@!?\d+>/g, '').trim();
  if (!content) return;

  const channelId = message.channel.id;
  if (!conversations.has(channelId)) {
    conversations.set(channelId, []);
  }

  const history = conversations.get(channelId);
  history.push({ role: 'user', content });

  // Keep last 20 messages
  if (history.length > 20) history.splice(0, history.length - 20);

  await message.channel.sendTyping();

  try {
    const response = await openai.chat.completions.create({
      model: 'ant:auto',
      messages: [
        { role: 'system', content: 'You are a helpful Discord bot. Keep responses under 2000 characters.' },
        ...history,
      ],
      max_tokens: 1024,
    });

    const reply = response.choices[0].message.content;
    history.push({ role: 'assistant', content: reply });

    await message.reply(reply);
  } catch (error) {
    await message.reply('Sorry, something went wrong. Please try again.');
  }
});

client.login(process.env.DISCORD_BOT_TOKEN);
console.log('Bot is starting...');

4. Run it

export ANTBASE_API_KEY=your-ant-api-key
export DISCORD_BOT_TOKEN=your-discord-bot-token
node bot.js

Configuration

Add slash commands for different AI modes:

// Register slash commands (run once)
import { REST, Routes, SlashCommandBuilder } from 'discord.js';

const commands = [
  new SlashCommandBuilder()
    .setName('ask')
    .setDescription('Ask the AI a question')
    .addStringOption(opt => opt.setName('prompt').setDescription('Your question').setRequired(true))
    .addStringOption(opt => opt.setName('model').setDescription('Model to use')
      .addChoices(
        { name: 'Auto', value: 'ant:auto' },
        { name: 'Fast', value: 'ant:fast' },
        { name: 'Best', value: 'ant:best' },
        { name: 'Reasoning', value: 'ant:reasoning' },
        { name: 'Coder', value: 'ant:coder' },
      )),
];

const rest = new REST().setToken(process.env.DISCORD_BOT_TOKEN);
await rest.put(Routes.applicationCommands(APPLICATION_ID), { body: commands });

Virtual Models

ModelBest forNotes
ant:autoGeneral chatBest default for Discord bots
ant:fastQuick repliesFast responses for active servers
ant:bestDetailed answersFrontier models for quality output
ant:freeDevelopmentZero cost while building
ant:reasoningProblem solvingChain-of-thought reasoning
ant:coderCode helpCode-optimized for developer bots

See Virtual Models for the full list.

Troubleshooting

Bot not responding to messages

Make sure the Message Content Intent is enabled in the Discord Developer Portal under your app's Bot settings. Without it, the bot cannot read message content.

Response exceeds Discord limit

Discord messages are limited to 2000 characters. Set a lower max_tokens or split long responses across multiple messages.