Concepts
How to add AI answers to a Discord bot, safely and cheaply
Add an /ask command to your Discord bot with any OpenAI-compatible API: key handling, cooldowns, length limits, mention filtering and deferred replies.
An AI command is one of the most asked-for features: members ask a question and the bot answers. Technically it is small. The parts that need care are cost, abuse and the three second reply limit.
The basic flow
- A member runs /ask with a question.
- The bot defers the reply, because the AI call takes time.
- It sends the question to an AI API with a system prompt that sets the personality and the rules.
- It edits the reply with the answer, cut to the length Discord allows.
Any OpenAI-compatible API
Many providers and local servers offer a chat completions endpoint with the same request format. The bot needs a base URL, a key and a model name, so keep all three in environment variables. That lets you change the provider without touching the code.
AI_BASE_URL=https://your-provider.example/v1
AI_API_KEY=your-key
AI_MODEL=model-nameKeep the cost under control
- A cooldown per member, for example 10 seconds.
- A limit on the question length.
- A maximum number of tokens for the answer.
- Optionally a daily cap per member, or a role that may use the command.
Handle Discord limits
A message can hold at most 2000 characters, so cut the answer, for example to 1500, or split it. An embed description has its own limit. The bot should defer the reply before the call, and show a friendly message when the API fails or times out.
Keep it safe in a server
- Turn off mentions in the reply so the model cannot ping @everyone or roles.
- Tell the model in the system prompt what it should refuse.
- Do not send private data, such as member lists, to the API.
- Log errors, not the full prompts of members.
Memory
By default each question stands alone. A short memory per channel or per member makes answers more natural, but it costs more tokens and has privacy aspects. Add it only if you need it.
Where the key lives
The key goes into the .env file or the host settings, never into the code. If it leaks, revoke it with the provider and create a new one.