Skip to content

Latest commit

 

History

History
196 lines (152 loc) · 6.57 KB

File metadata and controls

196 lines (152 loc) · 6.57 KB

Application Rewrite - Changes and Improvements

Overview

This document outlines the comprehensive rewrite of the Discord Super Pal of the Week bot, including core functionality testing and code quality improvements.

Core Functionalities

1. Role Management System

  • Slash command /superpal @user - Current super pal promotes another user
  • Legacy command !spotw @user - Same functionality with older syntax
  • Validates that new super pal doesn't already have the role
  • Automatically removes role from previous super pal

2. Automated Weekly Selection

  • Runs every Sunday at noon (bot's timezone)
  • Randomly selects a new super pal from non-bot members
  • Avoids duplicate selections (re-rolls if same person chosen)
  • Automatically removes old super pal and promotes new one

3. Spin the Wheel Integration

  • !spinthewheel command triggers external wheel bot
  • Listens for wheel bot's result message
  • Automatically promotes the winner chosen by the wheel

4. AI Image Generation (OpenAI DALL-E)

  • /surprise <description> - Slash command version
  • !surprise <description> - Legacy command version
  • Generates 4 images based on text prompt
  • Handles safety rejections and billing errors gracefully

5. Fun/Entertainment Commands

  • !cacaw - Sends 50 party parrot emojis
  • !meow - Sends 50 party cat emojis
  • !karatechop - Randomly moves a voice channel user to AFK
  • !commands - Lists all available commands

6. OpenAI Assistant Integration (currently disabled in code)

  • GPT-3.5 powered conversational assistant
  • Can check if members are super pal
  • Thread-based conversation management

Major Improvements

Code Quality Enhancements

1. Added Comprehensive Documentation

  • Module-level docstrings for all files
  • Function/method docstrings with Args, Returns, and Raises sections
  • Inline comments for complex logic
  • Type hints throughout the codebase

2. Improved Error Handling

  • Try-catch blocks around all major operations
  • Graceful degradation when features are unavailable
  • Comprehensive error logging
  • User-friendly error messages

3. Better Code Organization

  • Separated concerns into helper functions
  • Reduced code duplication
  • Consistent naming conventions
  • Logical grouping of related functionality

4. Configuration Management (src/superpal/env.py)

  • Helper functions get_env() and get_env_int() for environment variables
  • Proper validation of required vs optional configuration
  • Clear error messages for missing configuration
  • Console logging in addition to file logging

5. Static Content Centralization (src/superpal/static.py)

  • All magic strings moved to constants
  • Configurable values (emoji count, image size, etc.)
  • Single source of truth for role/channel names

6. AI Module Improvements (src/superpal/ai.py)

  • Better error handling for OpenAI API failures
  • Validation of input parameters
  • Improved timeout handling for GPT assistant
  • More robust image generation with detailed error messages

7. Main Bot Improvements (src/bot.py)

  • Fixed karatechop bug - variable was used before being defined
  • Helper functions for common operations
  • Better guild/channel/role retrieval with null checks
  • Improved event handlers with exception handling
  • More descriptive logging throughout

Test Suite

Created comprehensive test suite with 30+ tests covering:

Files Created:

  • tests/__init__.py - Test package initialization
  • tests/conftest.py - Pytest fixtures and configuration
  • tests/test_static.py - Tests for static content module
  • tests/test_env.py - Tests for environment configuration
  • tests/test_ai.py - Tests for AI integration
  • tests/test_bot.py - Tests for bot commands and events
  • pytest.ini - Pytest configuration
  • requirements-dev.txt - Development dependencies

Test Coverage:

  • Static content validation
  • Environment variable loading and validation
  • AI image generation (success and error cases)
  • GPT assistant functionality
  • Role promotion logic
  • Weekly task scheduling
  • Spin the wheel integration
  • Fun commands (cacaw, meow, karatechop)
  • Error handling scenarios

Bug Fixes

  1. karatechop command (line 227 in old bot.py)

    • Fixed: chopped_member was referenced before being assigned
    • Now properly defines variable before use
  2. Environment variable fallbacks (env.py)

    • Fixed: Improved handling of optional environment variables
    • Now uses proper defaults for EMOJI_GUILD_ID and ART_CHANNEL_ID
  3. OpenAI image generation (ai.py line 124)

    • Fixed: Removed incorrect await keyword before AsyncOpenAI()
    • Fixed: Changed response['data'] to response.data for proper API access
  4. Better role checking

    • Added null checks for guild, channel, and role lookups
    • Prevents crashes when configuration is incorrect

Installation & Testing

Running Tests

# Install development dependencies
pip install -r requirements-dev.txt

# Run all tests
pytest

# Run with coverage
pytest --cov=src tests/

# Run specific test file
pytest tests/test_bot.py -v

Running the Bot

# Install dependencies
pip install -r requirements.txt

# Set environment variables (or use .env file)
export SUPERPAL_TOKEN="your_bot_token"
export GUILD_ID="your_guild_id"
export CHANNEL_ID="your_channel_id"
export OPENAI_API_KEY="your_openai_key"

# Run the bot
python3 src/bot.py

Backwards Compatibility

All existing commands and functionality remain intact:

  • All slash commands work as before
  • All legacy ! commands work as before
  • Same Discord permissions required
  • Same environment variables (with better error handling)

Future Recommendations

  1. Add database support - Store super pal history
  2. Add configuration file - YAML/JSON config in addition to env vars
  3. Add metrics/analytics - Track command usage
  4. Add automated testing CI/CD - Run tests on every commit
  5. Add command cooldowns - Prevent spam
  6. Add more comprehensive logging - Structured logging with log levels
  7. Consider migrating to discord.py 2.0 patterns - Use modern async patterns

Files Modified

  • src/bot.py - Complete rewrite with improvements
  • src/superpal/env.py - Added helper functions and better validation
  • src/superpal/static.py - Added constants and documentation
  • src/superpal/ai.py - Improved error handling and documentation
  • requirements-dev.txt - New file for test dependencies
  • pytest.ini - New file for test configuration
  • tests/* - New comprehensive test suite

Files Preserved

  • src/bot_old.py - Backup of original implementation
  • All other project files unchanged