A lightweight Discord bot powered by OpenAI’s Chat Completions API. It exposes a /ask slash command that creates a dedicated thread for each request, keeps the entire thread history in memory, and replies inside that thread so everyone can follow the conversation.
Status: Actively developed proof-of-concept. Great for personal servers; adapt before running at larger scale or with persistent storage.
/askslash command with per-thread OpenAI context- Automatic thread creation and cleanup when threads are archived/deleted
- In-memory conversation history for quick experimentation
- Configurable OpenAI model via
OPENAI_MODEL
- Python 3.11+
- Discord application & bot token with the Message Content Intent enabled
- OpenAI API key with access to the model you configure (default
gpt-4o-mini)
- Clone and install deps:
git clone https://github.com/noahatkins/python-bot.git cd python-bot python -m venv .venv .\.venv\Scripts\activate # source .venv/bin/activate on macOS/Linux pip install -r requirements.txt
- Copy
env.example→.envand fill in:DISCORD_TOKENOPENAI_API_KEY- optional
OPENAI_MODEL
- Run the bot:
python bot.py
- Invite the bot with scopes
bot applications.commands. Grant Send Messages, Create Public Threads, Send Messages in Threads, Manage Threads, Read Message History, and Use Slash Commands.
1. /ask prompt:"Could you outline the steps to deploy this Discord bot to a Raspberry Pi?"
2. Bot replies in the channel, creates a thread, asks OpenAI, and posts the first answer inside the new thread.
3. Keep chatting in that thread; every new user message becomes part of the OpenAI context so responses stay on topic.
The bot ignores messages outside those threads and skips replies to other bots by default.
| Variable | Description | Required |
|---|---|---|
DISCORD_TOKEN |
Token from the Discord developer portal | ✅ |
OPENAI_API_KEY |
OpenAI API key with chat-completions access | ✅ |
OPENAI_MODEL |
Override default model (gpt-4o-mini) |
❌ |
Secrets are loaded via .env thanks to python-dotenv. Never commit .env; use env.example when sharing config.
.envis already gitignored; keep your real keys there.- Rotate Discord/OpenAI keys immediately if they leak (Discord dev portal → Regenerate Token, OpenAI dashboard → View API keys).
- When opening PRs, redact logs that might contain prompt contents or server names.
- Formatting/linting:
python -m py_compile bot.py services/openai_client.py - Run locally with
python bot.py - Tests are manual for now; add your own harness or mocks before shipping to production servers.
- Slash command not visible: restart the bot so
bot.tree.sync()runs, and ensure the invite includedapplications.commands. - Thread creation fails: verify the bot has the thread permissions listed above and that you run
/askin a guild text channel (not DMs). - OpenAI errors: check that
OPENAI_API_KEYis valid and the model name exists in your account. The bot logs failures to the console.
MIT © noahatkins