Build a Discord AI bot with APIVAI
A Discord AI bot lets your server chat with Claude or GPT. Create a bot application, run a small
discord.js script that forwards messages to an OpenAI-compatible model, and reply. APIVAI provides
the model at a fraction of list price, with crypto/USDT/Alipay payment.
1. Create the bot
- Go to the Discord Developer Portal → New Application → Bot → copy the bot token.
- Enable the Message Content Intent (Bot settings) so the bot can read messages.
- Invite the bot to your server (OAuth2 URL with the
botscope and Send Messages permission).
2. The bot (discord.js)
npm install discord.js openai
import { Client, GatewayIntentBits } from "discord.js";
import OpenAI from "openai";
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const ai = new OpenAI({ apiKey: process.env.APIVAI_API_KEY, baseURL: "https://api.apivai.com/v1" });
const SYSTEM = "You are a helpful assistant in a Discord server. Be concise.";
client.on("messageCreate", async (msg) => {
if (msg.author.bot) return;
// respond when mentioned (so the bot isn't noisy)
if (!msg.mentions.has(client.user)) return;
const prompt = msg.content.replace(/<@!?\d+>/g, "").trim();
await msg.channel.sendTyping();
const r = await ai.chat.completions.create({
model: "gpt-5.5",
messages: [{ role: "system", content: SYSTEM }, { role: "user", content: prompt }],
max_tokens: 500,
});
msg.reply(r.choices[0].message.content.slice(0, 1900)); // Discord 2000-char limit
});
client.login(process.env.DISCORD_BOT_TOKEN);
Set DISCORD_BOT_TOKEN and APIVAI_API_KEY, run node bot.js, and mention the bot in your server.
Polish
- Add slash commands (
/ask) for a cleaner UX. - Keep short per-channel history for context; trim to control token cost.
- Split replies over 2000 chars into multiple messages.
- Pick GPT-5.5 for general chat, Claude Sonnet for coding help.
FAQ
Can a Discord bot use APIVAI? Yes — it's a standard OpenAI chat call with base URL
https://api.apivai.com/v1; the bot forwards the message and posts the reply.
Which model should the bot use? GPT-5.5 for general/multilingual chat; Claude Sonnet for code.
How do I keep it from being spammy? Only respond when mentioned or via a slash command.
How do I control cost? Trim history and cap max_tokens; APIVAI's per-token price is already low.
Get started
Create a bot app, drop in the script with your APIVAI key, and run it. Examples:
APIVAI examples repo.







