discord.py
bot that integrates Perplexity’s Sonar API into your Discord server. Ask questions and get AI-powered answers with web access through slash commands or by mentioning the bot.

✨ Features
- 🌐 Web-Connected AI: Uses Perplexity’s Sonar API for up-to-date information
- ⚡ Slash Command: Simple
/ask
command for questions - 💬 Mention Support: Ask questions by mentioning the bot
- 🔗 Source Citations: Automatically formats and links to sources
- 🔒 Secure Setup: Environment-based configuration for API keys
🛠️ Prerequisites
1
Python Environment
Python 3.8+ installed on your system
2
Perplexity API Access
Active Perplexity API Key from Perplexity AI Settings
You’ll need a paid Perplexity account to access the API. See the pricing page for current rates.
3
Discord Bot Application
Discord Bot Token from the Discord Developer Portal
🚀 Quick Start
1. Repository Setup
Clone the repository and navigate to the bot directory:2. Install Dependencies
3. Configure API Keys
1
Get Your Perplexity API Key
- Visit Perplexity AI Account Settings
- Generate a new API key
- Copy the key to the .env file
Keep your API key secure! Never commit it to version control or share it publicly.
2
Create Discord Bot Application
- Go to the Discord Developer Portal
- Click “New Application” and give it a descriptive name
- Navigate to the “Bot” section
- Click “Reset Token” (or “Add Bot” if first time)
- Copy the bot token
3
Configure Environment Variables
Copy the example environment file and add your keys:Edit
.env
with your credentials:.env
🎯 Usage Guide
Bot Invitation & Setup
1
Generate Invite URL
In the Discord Developer Portal:
- Go to OAuth2 → URL Generator
- Select scopes:
bot
andapplications.commands
- Select bot permissions:
Send Messages
,Use Slash Commands
- Copy the generated URL
2
Invite to Server
- Paste the URL in your browser
- Select the Discord server to add the bot to
- Confirm the permissions
3
Start the Bot
How to Use
Slash Command:

📊 Response Format
The bot provides clean, readable responses with:- AI Answer: Direct response from Perplexity’s Sonar API
- Source Citations: Clickable links to sources (when available)
- Automatic Truncation: Responses are trimmed to fit Discord’s limits
🔧 Technical Details
This bot uses:- Model: Perplexity’s
sonar-pro
model - Response Limit: 2000 tokens from API, truncated to fit Discord
- Temperature: 0.2 for consistent, factual responses
- No Permissions: Anyone in the server can use the bot