Skip to content

gregoriomelo/discord-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Discord Bot with OpenAI Integration

A Discord bot built with Python, discord.py, and OpenAI. This bot uses AI to break down feature descriptions into detailed lists of features.

Features

  • Basic Discord client setup
  • OpenAI GPT-4o-mini integration
  • AI-powered feature breakdown command
  • Event handlers for bot ready and message events
  • Environment variable configuration
  • Virtual environment support

Setup Instructions

1. Prerequisites

  • Python 3.8 or higher
  • A Discord application and bot token
  • An OpenAI API key

2. Create a Discord Bot

  1. Go to Discord Developer Portal
  2. Click "New Application" and give it a name
  3. Go to the "Bot" section
  4. Click "Add Bot"
  5. Copy the bot token (keep this secret!)

3. Get OpenAI API Key

  1. Go to OpenAI Platform
  2. Sign in or create an account
  3. Click "Create new secret key"
  4. Copy the API key (keep this secret!)

4. Bot Permissions

When inviting your bot to a server, make sure to give it these permissions:

  • Send Messages
  • Read Message History
  • Use Slash Commands
  • Embed Links

5. Installation

  1. Clone or download this repository

  2. Create and activate a virtual environment:

    # Create virtual environment
    python -m venv venv
    
    # Activate virtual environment
    # On macOS/Linux:
    source venv/bin/activate
    # On Windows:
    # venv\Scripts\activate
  3. Install the required dependencies:

    pip install -r requirements.txt
  4. Set up environment variables:

    cp env.example .env
  5. Edit the .env file and add your API keys:

    DISCORD_TOKEN=your_actual_bot_token_here
    OPENAI_KEY=your_actual_openai_key_here
    

6. Running the Bot

python feature_breakdown.py

If everything is set up correctly, you should see a message like:

YourBotName has connected to Discord!
Bot is in X guild(s)

Available Commands

  • $breakdown <feature_description> - Use AI to break down a feature description into a detailed list of features

Example Usage:

$breakdown Create a user authentication system

The bot will use OpenAI's GPT-4o-mini model to analyze your feature description and provide a comprehensive breakdown of the features needed.

Project Structure

├── feature_breakdown.py # Main bot file with OpenAI integration
├── requirements.txt     # Python dependencies
├── env.example         # Environment variables template
├── .env                # Your actual environment variables (create this)
├── .gitignore          # Git ignore file
├── venv/               # Virtual environment (created during setup)
└── README.md           # This file

Customization

Adding New Commands

To add a new command, modify the on_message event handler:

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    
    if message.content.startswith('$breakdown'):
        feature_description = message.content.split('$breakdown ')[1]
        breakdown = call_openai(feature_description)
        await message.channel.send(breakdown)
    
    # Add your new command here
    if message.content.startswith('$mycommand'):
        await message.channel.send('Hello from my custom command!')

Adding Event Handlers

To add new event handlers, use the @client.event decorator:

@client.event
async def on_member_join(member):
    print(f"{member} joined the server!")

Changing the Command Prefix

Modify the command prefix in the on_message event handler:

if message.content.startswith('!'):  # Change $ to ! or any other prefix
    feature_description = message.content.split('!breakdown ')[1]
    breakdown = call_openai(feature_description)
    await message.channel.send(breakdown)

Adding OpenAI Functions

To add new AI-powered functions, create new functions similar to call_openai():

def call_openai_for_analysis(text):
    response = oa_client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": f"Analyze this text: {text}"}]
    )
    return response.choices[0].message.content

Troubleshooting

Common Issues

  1. "DISCORD_TOKEN not found": Make sure you've created a .env file with your bot token
  2. "OPENAI_KEY not found": Make sure you've added your OpenAI API key to the .env file
  3. "Missing Permissions": Ensure your bot has the necessary permissions in the server
  4. "Command not found": Check that you're using the correct prefix (default is $)
  5. "OpenAI API Error": Verify your OpenAI API key is valid and you have credits available
  6. Virtual environment issues: Make sure you've activated your virtual environment before running the bot

Getting Help

License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages