By Paul Derrington, Jr.
CEO | Chief Engineer | Research Engineer | Editor, D-CSIL
4 min read
Key Takeaways
You can put a working AI bot in your own Discord server with one Python file. Create the app in Discord's Developer Portal, turn on one privileged intent, invite the bot with the smallest permissions that work, then wire discord.py to Claude's Messages API. Every step below is checked against Discord's, discord.py's, and Anthropic's official documentation, and the code pattern was run locally before publishing.
Step 1 — Create the app and get its token
Open the Discord Developer Portal (discord.com/developers/applications) and click New Application. Name it and create it; Discord's own getting-started guide confirms every app starts there, and a bot user is enabled by default. Open the Bot page, and under Token click Reset Token to generate the bot token. Copy it now — treat it like a password, because anyone who has it controls the bot.
Do not paste the token into your code, a chat, or a screenshot. Step 4 stores it in a local .env file that never gets committed. If a token ever leaks, come back to this same Bot page and reset it.
Step 2 — Turn on one intent, then invite the bot
Still on the Bot page, scroll to Privileged Gateway Intents and turn on Message Content Intent — only that one. Intents decide which events Discord sends your bot, and message content is privileged because it exposes what people write. The discord.py quickstart's minimal bot requires exactly this intent, in both places: toggled on in the portal and set in code with intents.message_content = True. Leave Presence and Server Members off; this bot does not need them.
To invite the bot, open the Installation page (Discord's current guide uses this page for the default install link). For Guild Install, add the bot and applications.commands scopes, and under Permissions select Send Messages — the permission this bot actually uses. Copy the install link, open it, and add the bot to a test server first, not your main one.
Steps 3 and 4 — Install the libraries and store the secrets
You need Python 3.9 or newer (the Anthropic Python SDK's stated floor; discord.py also runs on current Python 3). In a fresh folder, create a virtual environment and install the three libraries: pip install discord.py anthropic python-dotenv. The versions used to check this guide were discord.py 2.7.1 and the current anthropic SDK from PyPI.
Create a file named .env in that folder with three lines: DISCORD_TOKEN (from Step 1), ANTHROPIC_API_KEY (create a key in the Anthropic Console), and CLAUDE_MODEL (the exact model ID from Anthropic's models documentation — copy it, do not type it from memory). Add .env to your .gitignore before you commit anything. Both SDKs read their keys from the environment by default, so the code itself never contains a secret.
Step 5 — Write the bot: mention in, Claude answer out
Save this as bot.py — never name the file discord.py, the discord.py quickstart warns that the name collides with the library itself. The pattern follows that quickstart exactly: build the client with the message_content intent, ignore the bot's own messages, and answer in the same channel. Two guardrails are added: the bot only responds when it is @mentioned, and the reply is cut at Discord's 2,000-character message limit.
import os / import discord / from anthropic import AsyncAnthropic / from dotenv import load_dotenv — then: load_dotenv(); intents = discord.Intents.default(); intents.message_content = True; client = discord.Client(intents=intents); claude = AsyncAnthropic(). In on_message: return if message.author == client.user; return unless client.user is in message.mentions; strip the mention out of message.content to get the prompt. Then reply = await claude.messages.create(model=os.environ['CLAUDE_MODEL'], max_tokens=500, system='You are a helpful assistant in a Discord server. Keep answers short and plain.', messages=[{'role': 'user', 'content': prompt}]); join the text blocks of reply.content and send the first 2,000 characters with await message.channel.send(...). Finish with client.run(os.environ['DISCORD_TOKEN']). AsyncAnthropic is the SDK's async client, which is the right fit inside discord.py's async event handlers, and max_tokens is a required parameter on every Messages API call.
Step 6 — Run it, test it, and keep it under control
Run python3 bot.py (on Windows: py -3 bot.py). The on_ready event prints 'Logged in as' with the bot's name when the connection is up — that line is your first pass/fail check. In your test server, @mention the bot with a plain question. A short answer in the same channel means the whole chain works: intent, token, invite, and API key.
If it fails, the cause is usually one of four things, in this order: the Message Content Intent is off in the portal or in code (empty message content is the symptom), the bot was invited without Send Messages, a secret name in .env does not match the name in the code, or the model ID is wrong. Fix one thing at a time and rerun. Two standing rules once it works: the bot only answers when mentioned, so it cannot loop or spam a channel, and every answer costs API tokens — max_tokens=500 and the mention-only trigger are what keep a hobby bot's bill small. When you are done testing, stop the script; the bot goes offline with it.