Beginner · 8 min read · Updated 2026-10-20

Set up AI for a Discord bot project

A rules file and setup that keep AI-written Discord bots on the current library version, handle tokens and intents safely, and get slash commands right.

Discord bots are one of the most popular first projects to build with AI, and one of the easiest to get subtly wrong. Bot libraries have changed a lot between major versions, so AI answers often mix old and new code. And a leaked bot token means someone else controls your bot. Here's a setup that avoids both.

1. Pin the library and runtime

Pick one library and write down its major version. The big two:

  • discord.js (JavaScript/TypeScript, Node.js). Version 14 reorganized a lot compared with v13 (for example, intents are passed as GatewayIntentBits), and AI often produces v12/v13-style code.
  • discord.py (Python). Version 2.x added slash commands through app_commands; older tutorials use prefix commands only.
## Tech stack
- Node.js 20 LTS, discord.js v14, JavaScript (ES modules)
- Slash commands only (no prefix commands)
- Never use discord.js v13 or older syntax. If unsure an API exists in
  v14, say so.

2. Lock down the token

Your bot token is a password for your bot. Anyone who has it can log in as your bot and do anything its permissions allow.

# .env  (never commit this file)
DISCORD_TOKEN=paste-your-token-here
CLIENT_ID=your-application-id
GUILD_ID=your-test-server-id
# .gitignore
node_modules/
.env

Load it with your runtime's env support (for example node --env-file=.env index.js on recent Node versions, or the dotenv package; python-dotenv in Python). Then add the rule:

## Security
- The bot token, client secret, and any API keys come from environment
  variables only. Never write them in code, docs, comments, or logs.
- If you see a token anywhere in the code, stop and tell me.
If your token ever leaks (pasted in a chat, pushed to GitHub, shown on stream), reset it right away in the Discord Developer Portal under your application's Bot settings. Assume a leaked token has already been copied.

3. Declare intents

Intents decide which events Discord sends your bot. Request only what you need. Some intents are privileged and must also be switched on in the Developer Portal; the most common one is Message Content, which bots need to read the text of most messages. A slash-command-only bot usually doesn't need it at all.

## Intents
- Guilds only. Slash commands don't need Message Content.
- Don't add privileged intents (Message Content, Server Members,
  Presence) unless a feature needs one. Ask me first, since I also
  have to enable it in the Developer Portal.

A classic AI mistake is adding every intent "to be safe". It works on your test server, then causes problems later, because bots in many servers need Discord's approval for privileged intents.

4. Slash command rules

  • Registration. Commands must be registered with Discord before they show up. During development, register them to your test server (guild commands); register globally when you're ready to share the bot.
  • The 3-second rule. Discord expects a response to an interaction within about 3 seconds. If a command calls an API or database, defer first (interaction.deferReply() in discord.js, interaction.response.defer() in discord.py), then send the real answer as a follow-up.
## Slash commands
- One file per command in commands/, each exporting `data`
  (a SlashCommandBuilder) and `execute(interaction)`
- Register commands with `node deploy-commands.js` (guild-scoped
  using GUILD_ID during development)
- Defer the reply for anything that might take more than ~2 seconds
- Catch errors in execute() and reply with a friendly ephemeral
  message instead of crashing

5. Structure and hosting

## Structure
- index.js: creates the client, loads commands and events
- commands/: one slash command per file
- events/: one event handler per file (ready, interactionCreate)
- deploy-commands.js: registers slash commands

## Hosting
- Runs as an always-on Node process (my VPS / a bot host)
- Restart on crash with a process manager

Worth knowing: a normal bot keeps a live connection (the gateway) open, so it needs an always-on process. Static hosts like Cloudflare Pages can't run that. Bots that use only slash commands can instead receive interactions over HTTP at an endpoint URL, which can run on serverless platforms, but that's a different architecture. Tell the AI which one you're using.

First prompt

Read AGENTS.md. Set up the project structure it describes with one
/ping command that replies "Pong!" and shows latency. Then tell me
exactly how to register the command and run the bot.

Once /ping works, commit, then add features one command at a time. The generator's "Discord bot" preset gives you this whole rules file in a minute.