You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
11
+
12
+
## 🌟 Features
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.
20
+
21
+
## 📋 Prerequisites
22
+
23
+
-**Python 3.8+**
24
+
-**A Perplexity API Key**
25
+
-**A Discord Bot Token**
26
+
27
+
## 🚀 Installation & Setup
28
+
29
+
1.**Clone the Repository:** Fork and clone the `api-cookbook` repository to your local machine.
30
+
31
+
2.**Navigate to this Example:**
32
+
```bash
33
+
cd docs/examples/discord-py-bot/
34
+
```
35
+
3.**Install Dependencies:**
36
+
```bash
37
+
pip install -r requirements.txt
38
+
```
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.
# This code is a simplified example of a Discord bot that integrates with the Perplexity AI API. This is a complete, minimal bot script that someone can run directly. Notice I have simplified the permission logic to only check for administrators to remove the need for an external database, making the example much more focused on the Perplexity API.
2
+
3
+
# bot.py
4
+
importos
5
+
importdiscord
6
+
fromdiscord.extimportcommands
7
+
fromdiscordimportapp_commands
8
+
importopenai
9
+
fromdotenvimportload_dotenv
10
+
11
+
# --- Step 1: Load Environment Variables ---
12
+
# This ensures your secret keys are kept safe in a .env file.
title: 4Point Hoops | AI Basketball Analytics Platform
3
+
description: Advanced NBA analytics platform that combines live Basketball-Reference data with Perplexity Sonar to deliver deep-dive player stats, cross-season comparisons and expert-grade AI explanations
**4Point Hoops** is an advanced NBA analytics platform that turns raw basketball statistics into actionable, narrative-driven insights. By scraping Basketball-Reference in real time and routing context-rich prompts to Perplexity's Sonar Pro model, it helps fans, analysts, and fantasy players understand the "why" and "what's next" – not just the numbers.
0 commit comments