| 
1 | 1 | ---  | 
2 | 2 | title: Perplexity Discord Bot  | 
3 |  | -description: A runnable discord.py bot that adds a /ask_perplexity command to your server, using the Sonar API for web-connected answers.  | 
 | 3 | +description: A simple discord.py bot that integrates Perplexity's Sonar API to bring AI answers to your Discord server.  | 
4 | 4 | sidebar_position: 4  | 
5 | 5 | keywords: [discord, bot, discord.py, python, chatbot, perplexity, sonar api, command, slash command]  | 
6 | 6 | ---  | 
7 | 7 | 
 
  | 
8 |  | -# Perplexity Discord Bot  | 
 | 8 | +A simple `discord.py` bot that integrates [Perplexity's Sonar API](https://docs.perplexity.ai/) into your Discord server. Ask questions and get AI-powered answers with web access through slash commands or by mentioning the bot.  | 
9 | 9 | 
 
  | 
10 |  | -A runnable `discord.py` application that adds a `/ask_perplexity` slash command to your Discord server, using Perplexity's Sonar API for real-time, web-connected answers.  | 
 | 10 | +  | 
11 | 11 | 
 
  | 
12 |  | -## 🌟 Features  | 
 | 12 | +## ✨ Features  | 
13 | 13 | 
 
  | 
14 |  | -- **Direct Sonar API Integration**: Uses Perplexity's API for real-time, web-connected answers.  | 
15 |  | -- **Slash Command Ready**: Implements a modern `/ask_perplexity` command that's easy to use.  | 
16 |  | -- **Secure Configuration**: Uses environment variables (`.env`) for both Discord and Perplexity keys, keeping them safe.  | 
17 |  | -- **Administrator Permissions**: Locked to server administrators by default for easy control over API usage.  | 
18 |  | -- **Formatted Embeds**: Delivers answers in a clean, professional Discord embed.  | 
19 |  | -- **Self-Contained Example**: Minimal, single-file code designed to be easy to run and understand.  | 
 | 14 | +- **🌐 Web-Connected AI**: Uses Perplexity's Sonar API for up-to-date information  | 
 | 15 | +- **⚡ Slash Command**: Simple `/ask` command for questions    | 
 | 16 | +- **💬 Mention Support**: Ask questions by mentioning the bot  | 
 | 17 | +- **🔗 Source Citations**: Automatically formats and links to sources  | 
 | 18 | +- **🔒 Secure Setup**: Environment-based configuration for API keys  | 
20 | 19 | 
 
  | 
21 |  | -## 📋 Prerequisites  | 
 | 20 | +## 🛠️ Prerequisites  | 
22 | 21 | 
 
  | 
23 |  | -- **Python 3.8+**  | 
24 |  | -- **A Perplexity API Key**  | 
25 |  | -- **A Discord Bot Token**  | 
 | 22 | +<Steps>  | 
 | 23 | +  <Step title="Python Environment">  | 
 | 24 | +    **Python 3.8+** installed on your system  | 
 | 25 | +      | 
 | 26 | +    ```bash  | 
 | 27 | +    python --version  # Should be 3.8 or higher  | 
 | 28 | +    ```  | 
 | 29 | +  </Step>  | 
 | 30 | +    | 
 | 31 | +  <Step title="Perplexity API Access">  | 
 | 32 | +    **Active Perplexity API Key** from [Perplexity AI Settings](https://www.perplexity.ai/settings/api)  | 
 | 33 | +      | 
 | 34 | +    <Note>You'll need a paid Perplexity account to access the API. See the [pricing page](https://www.perplexity.ai/pricing) for current rates.</Note>  | 
 | 35 | +  </Step>  | 
 | 36 | +    | 
 | 37 | +  <Step title="Discord Bot Application">  | 
 | 38 | +    **Discord Bot Token** from the [Discord Developer Portal](https://discord.com/developers/applications)  | 
 | 39 | +  </Step>  | 
 | 40 | +</Steps>  | 
 | 41 | + | 
 | 42 | +## 🚀 Quick Start  | 
26 | 43 | 
 
  | 
27 |  | -## 🚀 Installation & Setup  | 
 | 44 | +### 1. Repository Setup  | 
28 | 45 | 
 
  | 
29 |  | -1.  **Clone the Repository:** Fork and clone the `api-cookbook` repository to your local machine.  | 
 | 46 | +Clone the repository and navigate to the bot directory:  | 
 | 47 | + | 
 | 48 | +```bash  | 
 | 49 | +git clone https://github.com/perplexity-ai/api-cookbook.git  | 
 | 50 | +cd api-cookbook/docs/examples/discord-py-bot/  | 
 | 51 | +```  | 
30 | 52 | 
 
  | 
31 |  | -2.  **Navigate to this Example:**  | 
 | 53 | +### 2. Install Dependencies  | 
 | 54 | + | 
 | 55 | +```bash  | 
 | 56 | +# Create a virtual environment (recommended)  | 
 | 57 | +python -m venv venv  | 
 | 58 | +source venv/bin/activate  # On Windows: venv\Scripts\activate  | 
 | 59 | + | 
 | 60 | +# Install required packages  | 
 | 61 | +pip install -r requirements.txt  | 
 | 62 | +```  | 
 | 63 | + | 
 | 64 | +### 3. Configure API Keys  | 
 | 65 | + | 
 | 66 | +<Steps>  | 
 | 67 | +  <Step title="Get Your Perplexity API Key">  | 
 | 68 | +    1. Visit [Perplexity AI Account Settings](https://www.perplexity.ai/settings/api)  | 
 | 69 | +    2. Generate a new API key  | 
 | 70 | +    3. Copy the key to the .env file  | 
 | 71 | +      | 
 | 72 | +    <Warning>Keep your API key secure! Never commit it to version control or share it publicly.</Warning>  | 
 | 73 | +  </Step>  | 
 | 74 | +    | 
 | 75 | +  <Step title="Create Discord Bot Application">  | 
 | 76 | +    1. Go to the [Discord Developer Portal](https://discord.com/developers/applications)  | 
 | 77 | +    2. Click **"New Application"** and give it a descriptive name  | 
 | 78 | +    3. Navigate to the **"Bot"** section  | 
 | 79 | +    4. Click **"Reset Token"** (or "Add Bot" if first time)  | 
 | 80 | +    5. Copy the bot token  | 
 | 81 | +  </Step>  | 
 | 82 | +    | 
 | 83 | +  <Step title="Configure Environment Variables">  | 
 | 84 | +    Copy the example environment file and add your keys:  | 
 | 85 | +      | 
32 | 86 |     ```bash  | 
33 |  | -    cd docs/examples/discord-py-bot/  | 
 | 87 | +    cp env.example .env  | 
34 | 88 |     ```  | 
35 |  | -3.  **Install Dependencies:**  | 
 | 89 | +      | 
 | 90 | +    Edit `.env` with your credentials:  | 
 | 91 | +      | 
 | 92 | +    ```bash title=".env"  | 
 | 93 | +    DISCORD_TOKEN="your_discord_bot_token_here"  | 
 | 94 | +    PERPLEXITY_API_KEY="your_perplexity_api_key_here"  | 
 | 95 | +    ```  | 
 | 96 | +  </Step>  | 
 | 97 | +</Steps>  | 
 | 98 | + | 
 | 99 | +## 🎯 Usage Guide  | 
 | 100 | + | 
 | 101 | +### Bot Invitation & Setup  | 
 | 102 | + | 
 | 103 | +<Steps>  | 
 | 104 | +  <Step title="Generate Invite URL">  | 
 | 105 | +    In the Discord Developer Portal:  | 
 | 106 | +    1. Go to **OAuth2** → **URL Generator**  | 
 | 107 | +    2. Select scopes: `bot` and `applications.commands`  | 
 | 108 | +    3. Select bot permissions: `Send Messages`, `Use Slash Commands`  | 
 | 109 | +    4. Copy the generated URL  | 
 | 110 | +  </Step>  | 
 | 111 | +    | 
 | 112 | +  <Step title="Invite to Server">  | 
 | 113 | +    1. Paste the URL in your browser  | 
 | 114 | +    2. Select the Discord server to add the bot to  | 
 | 115 | +    3. Confirm the permissions  | 
 | 116 | +  </Step>  | 
 | 117 | +    | 
 | 118 | +  <Step title="Start the Bot">  | 
36 | 119 |     ```bash  | 
37 |  | -    pip install -r requirements.txt  | 
 | 120 | +    python bot.py  | 
38 | 121 |     ```  | 
39 |  | -4.  **Get Your Secret Keys:**  | 
40 |  | -    -   **Perplexity API Key:** Get your key from the [Perplexity AI Account Settings](https://www.perplexity.ai/settings/api).  | 
41 |  | -    -   **Discord Bot Token:**   | 
42 |  | -        1. Go to the [Discord Developer Portal](https://discord.com/developers/applications).  | 
43 |  | -        2. Click **"New Application"** and give it a name.  | 
44 |  | -        3. Go to the **"Bot"** tab and click **"Reset Token"** (or "Add Bot" first if needed).  | 
45 |  | -        4. Copy the token. This is your bot's password – keep it safe!  | 
46 |  | - | 
47 |  | -5.  **Set Up Your Environment File:**  | 
48 |  | -    -   Rename the `.env.example` file to `.env`.  | 
49 |  | -    -   Open the `.env` file and add your secret keys:  | 
50 |  | -      ```  | 
51 |  | -      DISCORD_TOKEN="YOUR_DISCORD_BOT_TOKEN_HERE"  | 
52 |  | -      PERPLEXITY_API_KEY="YOUR_PPLX_API_KEY_HERE"  | 
53 |  | -      ```  | 
54 |  | - | 
55 |  | -## 🔧 Usage  | 
56 |  | - | 
57 |  | -### 1. Invite Your Bot to a Server  | 
58 |  | - | 
59 |  | --   In the Discord Developer Portal, go to "OAuth2" -> "URL Generator".  | 
60 |  | --   Select the `bot` and `applications.commands` scopes.  | 
61 |  | --   Under "Bot Permissions," select **Administrator**.  | 
62 |  | --   Copy the generated URL, paste it into your browser, and invite the bot to your server.  | 
63 |  | - | 
64 |  | -### 2. Run the Bot Script  | 
65 |  | - | 
66 |  | -Simply execute the `bot.py` script from your terminal:  | 
67 |  | -```bash  | 
68 |  | -python bot.py  | 
69 |  | -This will connect the bot to Discord and sync the /ask_perplexity command.  | 
70 |  | -3. Use the Command in Discord  | 
71 |  | -In any channel in your server, an administrator can type:  | 
72 |  | -code  | 
73 |  | -Code  | 
74 |  | -/ask_perplexity prompt:What is the latest news in the world of generative AI?  | 
75 |  | -📄 Output Example  | 
76 |  | -The bot will defer the interaction and then respond with a formatted embed containing the answer:  | 
77 |  | -code  | 
78 |  | -Code  | 
79 |  | -HatchMate [APP]  | 
80 |  | -🌐 Perplexity's Response  | 
81 |  | -
  | 
82 |  | -The latest news in generative AI includes advancements in large language models... (and so on).  | 
83 |  | -
  | 
84 |  | -Your Prompt```  | 
85 |  | -What is the latest news in the world of generative AI?  | 
86 |  | -Requested by YourUsername  | 
87 |  | -code  | 
88 |  | -Code  | 
89 |  | -## 🛠️ Extending the Bot  | 
90 |  | -
  | 
91 |  | -- **Role-Based Access**: Modify the permission checks to allow specific roles, not just administrators.  | 
92 |  | -- **Model Selection**: Add an option to the slash command to choose between different models like `sonar-pro` or `sonar-small-online`.  | 
93 |  | -- **Conversation History**: Implement a database (like SQLite or Redis) to store recent messages and send them with new prompts for conversational context.  | 
94 |  | -
  | 
95 |  | -## ⚠️ Limitations  | 
96 |  | -
  | 
97 |  | -- The permission system is basic (administrator-only) to keep the example simple and database-free.  | 
98 |  | -- The command is single-turn and does not remember conversation history.  | 
99 |  | -- API rate limits may apply based on your Perplexity account status.  | 
100 |  | -
  | 
101 |  | -## 🙏 Acknowledgements  | 
102 |  | -
  | 
103 |  | -- This project uses the [Perplexity AI API](https://docs.perplexity.ai/).  | 
104 |  | -- Built with the [discord.py](https://discordpy.readthedocs.io/en/stable/) library.  | 
 | 122 | +      | 
 | 123 | +    You should see output confirming the bot is online and commands are synced.  | 
 | 124 | +  </Step>  | 
 | 125 | +</Steps>  | 
 | 126 | + | 
 | 127 | +### How to Use  | 
 | 128 | + | 
 | 129 | +**Slash Command:**  | 
 | 130 | +```  | 
 | 131 | +/ask [your question here]  | 
 | 132 | +```  | 
 | 133 | + | 
 | 134 | +  | 
 | 135 | + | 
 | 136 | +**Mention the Bot:**  | 
 | 137 | +```  | 
 | 138 | +@YourBot [your question here]  | 
 | 139 | +```  | 
 | 140 | + | 
 | 141 | +  | 
 | 142 | + | 
 | 143 | +## 📊 Response Format  | 
 | 144 | + | 
 | 145 | +The bot provides clean, readable responses with:  | 
 | 146 | +- **AI Answer**: Direct response from Perplexity's Sonar API  | 
 | 147 | +- **Source Citations**: Clickable links to sources (when available)  | 
 | 148 | +- **Automatic Truncation**: Responses are trimmed to fit Discord's limits  | 
 | 149 | + | 
 | 150 | +## 🔧 Technical Details  | 
 | 151 | + | 
 | 152 | +This bot uses:  | 
 | 153 | +- **Model**: Perplexity's `sonar-pro` model  | 
 | 154 | +- **Response Limit**: 2000 tokens from API, truncated to fit Discord  | 
 | 155 | +- **Temperature**: 0.2 for consistent, factual responses  | 
 | 156 | +- **No Permissions**: Anyone in the server can use the bot  | 
0 commit comments