https://github.com/johndotpub/discordianai
A ChatGPT based Discord bot that uses GPT models to provide chat-based interactions with users in Discord channels and via DMs. Supports OpenAI and Perplexity API backends!
https://github.com/johndotpub/discordianai
ai chatgpt discord openai-api perplexity-api python
Last synced: 5 months ago
JSON representation
A ChatGPT based Discord bot that uses GPT models to provide chat-based interactions with users in Discord channels and via DMs. Supports OpenAI and Perplexity API backends!
- Host: GitHub
- URL: https://github.com/johndotpub/discordianai
- Owner: johndotpub
- License: unlicense
- Created: 2023-06-06T07:06:16.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-08-11T08:42:49.000Z (12 months ago)
- Last Synced: 2025-08-11T08:47:26.208Z (12 months ago)
- Topics: ai, chatgpt, discord, openai-api, perplexity-api, python
- Language: Python
- Homepage: https://github.com/johndotpub/DiscordianAI
- Size: 469 KB
- Stars: 19
- Watchers: 0
- Forks: 4
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/johndotpub/DiscordianAI/actions/workflows/ci.yml)
[](https://github.com/johndotpub/DiscordianAI/actions/workflows/github-code-scanning/codeql)
[](https://codecov.io/github/johndotpub/DiscordianAI)
[](https://pypi.org/project/pip-audit/)
[](https://github.com/johndotpub/DiscordianAI/actions/workflows/ci.yml)
[](https://www.python.org/downloads/)
[](https://github.com/psf/black)
[](https://github.com/astral-sh/ruff)
[](https://pre-commit.com/)
[](https://discordpy.readthedocs.io/)
[](https://openai.com/)
[](https://www.perplexity.ai/)
[](http://unlicense.org/)
[](https://github.com/johndotpub/DiscordianAI/releases)
[](https://github.com/johndotpub/DiscordianAI/stargazers)
[](https://github.com/johndotpub/DiscordianAI/pkgs/container/discordianai)
[](https://github.com/johndotpub/DiscordianAI/issues)
# DiscordianAI
DiscordianAI is a production-ready Discord bot that blends OpenAI GPT-5 frontier models with Perplexity Sonar-Pro web search. It focuses on conversation consistency, deterministic routing, and Discord-native formatting so answers stay contextual, accurate, and easy to read.
## What Makes It Different
- **Hybrid intelligence**: GPT-5 handles reasoning/creative prompts while Sonar-Pro covers current events with structured citations.
- **Conversation consistency**: Follow-up detector keeps a thread on the same AI service, preventing jarring context shifts.
- **Discord-first UX**: Citation-aware embeds, smart message splitting, and automatic embed suppression keep chats readable.
- **Operations minded**: HTTP/2 connection pooling, circuit breakers, and health checks keep long-running bots stable.
- **Single-source configuration**: `config.ini` drives every runtime mode, and validators catch unsupported parameters early.
## Operation Modes
| Mode | Description |
|------|-------------|
| 🧠 **Smart Hybrid** | Analyze each message, then route to GPT-5 or Sonar-Pro with follow-up consistency.
| 🤖 **OpenAI Only** | Force GPT-5 for every response; great for creative and coding-focused servers.
| 🔍 **Perplexity Only** | Force Sonar-Pro for every response; ideal for research or news-heavy servers.
## Quick Start
1. **Install Python 3.12 or newer** and create a virtual environment.
2. **Clone & install**: `pip install -e .[dev]`
3. **Copy config**: `cp config.ini.example config.ini`
4. **Set tokens** in `config.ini` (Discord, OpenAI, Perplexity as needed).
5. **Run the bot**: `python -m src.main --conf config.ini`
6. **Run tests** before shipping: `pytest` (coverage gate: 84% via tox/Codecov)
Need the full Discord bot creation walkthrough and environment prep? See [docs/Setup.md](docs/Setup.md).
### Running directly
```bash
python -m src.main --conf config.ini --folder /path/to/base/folder
```
- `--conf` points at the config file.
- `--folder` (optional) makes config/log paths relative to a base directory (useful for Docker or systemd units).
## Configuration Essentials
| Key | Purpose |
|-----|---------|
| `DISCORD_TOKEN` | Bot authentication token from the Discord Developer Portal.
| `GPT_MODEL` | GPT-5 series model (`gpt-5-mini`, `gpt-5`, `gpt-5-nano`, `gpt-5-chat`).
| `PERPLEXITY_MODEL` | Perplexity Sonar model (`sonar-pro` default, `sonar` fallback).
| `INPUT_TOKENS` / `OUTPUT_TOKENS` | Caps for prompt and completion tokens (GPT-5 uses `max_completion_tokens`).
| `LOOKBACK_MESSAGES_FOR_CONSISTENCY` | How many prior turns to inspect when enforcing AI service consistency.
| `OPENAI_MAX_CONNECTIONS` / `PERPLEXITY_MAX_CONNECTIONS` | HTTP/2 pooling limits for each API client.
### Minimal hybrid config
```ini
[Discord]
DISCORD_TOKEN=your_discord_bot_token
[Default]
OPENAI_API_KEY=sk-your-openai-key
OPENAI_API_URL=https://api.openai.com/v1/
GPT_MODEL=gpt-5-mini
PERPLEXITY_API_KEY=pplx-your-perplexity-key
PERPLEXITY_MODEL=sonar-pro
INPUT_TOKENS=120000
OUTPUT_TOKENS=8000
SYSTEM_MESSAGE=You are a helpful assistant with access to current web information.
[Limits]
RATE_LIMIT=10
RATE_LIMIT_PER=60
```
## Key Components
### AI Orchestration
- `smart_orchestrator.py` routes messages via semantic analysis, entity detection, and follow-up tracking.
- `message_processor.py` normalizes Discord input, calls the orchestrator, and records AI metadata for future turns.
- `openai_processing.py` and `perplexity_processing.py` build strictly supported API payloads (GPT-5 ignores temperature, Sonar-Pro sampling is managed server-side).
### Discord User Experience
- `discord_embeds.py` formats Perplexity citations into clean embeds with automatic suppression when multiple links exist.
- `message_splitter.py` enforces Discord limits (2000 / 3840 / 4096 chars) with code-block and citation awareness.
- `message_router.py` centralizes mention handling, DM routing, and rate-limit responses.
### Reliability & Ops
- `connection_pool.py` exposes tuned HTTP/2 clients (50/10 connections for OpenAI, 30/5 for Perplexity).
- `health_checks.py` verifies API reachability and validates GPT-5/Sonar configurations at startup.
- `api_validation.py` + `api_utils.py` guard against unsupported model strings and capture structured error metadata.
## Documentation Map
| Topic | Reference |
|-------|-----------|
| Setup & onboarding | [docs/Setup.md](docs/Setup.md)
| Architecture & flow | [docs/Architecture.md](docs/Architecture.md)
| Smart routing internals | [docs/HybridMode.md](docs/HybridMode.md)
| Configuration deep dive | [docs/Configuration.md](docs/Configuration.md)
| OpenAI usage | [docs/OpenAI.md](docs/OpenAI.md)
| Perplexity usage & citations | [docs/Perplexity.md](docs/Perplexity.md)
| Message splitting & embeds | [docs/MessageSplitting.md](docs/MessageSplitting.md), [docs/EmbedLimits.md](docs/EmbedLimits.md)
| Web scraping pipeline | [docs/WebScraping.md](docs/WebScraping.md)
| Connection pooling & HTTP/2 | [docs/ConnectionPooling.md](docs/ConnectionPooling.md)
| Docker & daemonization | [docs/Docker.md](docs/Docker.md), [docs/Daemon.md](docs/Daemon.md)
| Security & rate limiting | [docs/Security.md](docs/Security.md)
| Development workflow | [docs/Development.md](docs/Development.md)
| API parameter validation | [docs/API_Validation.md](docs/API_Validation.md)
## Development Workflow
```bash
# Install dev extras
pip install -e .[dev]
# Lint & format
ruff check --fix .
black .
# Run the targeted suites
pytest -q
pytest tests/test_smart_orchestrator_coverage.py
```
Use `tox` for the CI matrix (`tox`, `tox -e py312`, `tox --parallel auto`, `tox -e audit`). CI runs Ruff, Black, pytest with coverage, and pip-audit; locally run `tox -e audit` to scan runtime + dev deps.
## Security Checklist
- Keep `config.ini` out of version control; copy from `config.ini.example` and set `chmod 600 config.ini` on servers.
- Prefer environment variables in production (`export DISCORD_TOKEN=...`).
- Rotate OpenAI (`sk-...`) and Perplexity (`pplx-...`) keys regularly; validators will reject malformed prefixes.
- Limit Discord bot permissions to the channels where it operates.
- Monitor logs for rate-limit bursts or scraping failures and adjust `RATE_LIMIT` / `RATE_LIMIT_PER` accordingly.
## Support & License
- Questions or feedback: [github@john.pub](mailto:github@john.pub)
- Issues and feature requests: open a GitHub issue.
- Licensed under the [Unlicense](LICENSE).