Perplexity Discord Bot

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.

🌟 Features

  • Direct Sonar API Integration: Uses Perplexity’s API for real-time, web-connected answers.
  • Slash Command Ready: Implements a modern /ask_perplexity command that’s easy to use.
  • Secure Configuration: Uses environment variables (.env) for both Discord and Perplexity keys, keeping them safe.
  • Administrator Permissions: Locked to server administrators by default for easy control over API usage.
  • Formatted Embeds: Delivers answers in a clean, professional Discord embed.
  • Self-Contained Example: Minimal, single-file code designed to be easy to run and understand.

📋 Prerequisites

  • Python 3.8+
  • A Perplexity API Key
  • A Discord Bot Token

🚀 Installation & Setup

  1. Clone the Repository: Fork and clone the api-cookbook repository to your local machine.
  2. Navigate to this Example:
    cd docs/examples/discord-py-bot/
    
  3. Install Dependencies:
    pip install -r requirements.txt
    
  4. Get Your Secret Keys:
    • Perplexity API Key: Get your key from the Perplexity AI Account Settings.
    • Discord Bot Token:
      1. Go to the Discord Developer Portal.
      2. Click “New Application” and give it a name.
      3. Go to the “Bot” tab and click “Reset Token” (or “Add Bot” first if needed).
      4. Copy the token. This is your bot’s password – keep it safe!
  5. Set Up Your Environment File:
    • Rename the .env.example file to .env.
    • Open the .env file and add your secret keys:
    DISCORD_TOKEN="YOUR_DISCORD_BOT_TOKEN_HERE"
    PERPLEXITY_API_KEY="YOUR_PPLX_API_KEY_HERE"
    

🔧 Usage

1. Invite Your Bot to a Server

  • In the Discord Developer Portal, go to “OAuth2” -> “URL Generator”.
  • Select the bot and applications.commands scopes.
  • Under “Bot Permissions,” select Administrator.
  • Copy the generated URL, paste it into your browser, and invite the bot to your server.

2. Run the Bot Script

Simply execute the bot.py script from your terminal:
python bot.py
This will connect the bot to Discord and sync the /ask_perplexity command.
3. Use the Command in Discord
In any channel in your server, an administrator can type:
code
Code
/ask_perplexity prompt:What is the latest news in the world of generative AI?
📄 Output Example
The bot will defer the interaction and then respond with a formatted embed containing the answer:
code
Code
HatchMate [APP]
🌐 Perplexity's Response

The latest news in generative AI includes advancements in large language models... (and so on).

Your Prompt```
What is the latest news in the world of generative AI?
Requested by YourUsername
code
Code
## 🛠️ Extending the Bot

- **Role-Based Access**: Modify the permission checks to allow specific roles, not just administrators.
- **Model Selection**: Add an option to the slash command to choose between different models like `sonar-pro` or `sonar-small-online`.
- **Conversation History**: Implement a database (like SQLite or Redis) to store recent messages and send them with new prompts for conversational context.

## ⚠️ Limitations

- The permission system is basic (administrator-only) to keep the example simple and database-free.
- The command is single-turn and does not remember conversation history.
- API rate limits may apply based on your Perplexity account status.

## 🙏 Acknowledgements

- This project uses the [Perplexity AI API](https://docs.perplexity.ai/).
- Built with the [discord.py](https://discordpy.readthedocs.io/en/stable/) library.