2025-05-24 12:53:31 -05:00
# Steve's Bot 🤖
2026-01-11 18:52:19 -06:00
This is a comprehensive Discord bot platform built with .NET 10, designed to be a full-featured and extensible solution for having my own assistant in my [Discord server ](https://discord.stevanfreeborn.com ).
2025-05-24 12:53:31 -05:00
## ✨ Features
2025-06-04 15:33:50 -05:00
### Core Bot Platform
2025-05-24 12:53:31 -05:00
- **Custom Discord Gateway Client**: Full-featured implementation with:
- WebSocket connection management
- Automatic heartbeat handling
- Session resumption and reconnection logic
- Event-driven architecture
2025-06-04 15:33:50 -05:00
- **Shared Library**: Common Discord REST client and telemetry components
2025-05-24 12:53:31 -05:00
- **Observability**: Built-in telemetry with OpenTelemetry support
- **Resilient Architecture**: Graceful error handling and automatic recovery
2025-06-04 15:33:50 -05:00
### YouTube Integration
- **YouTube Webhook Service**: Web API for receiving YouTube notifications
- **Live Stream Detection**: Automatic detection and Discord notifications for live streams
- **PubSubHubbub Integration**: YouTube webhook subscription management
- **Stream Deduplication**: Prevents duplicate notifications using in-memory store
### Deployment & Operations
- **Multi-Service Architecture**: Separate worker and webhook services
- **Containerized Deployment**: Docker containers with Docker Compose orchestration
- **Production Ready**: Environment-specific configuration and logging
2025-05-24 12:53:31 -05:00
## 🚀 Quick Start
### Prerequisites
2026-01-11 18:52:19 -06:00
- [.NET 10 SDK ](https://dotnet.microsoft.com/download )
2025-05-24 12:53:31 -05:00
- Discord Bot Token (from [Discord Developer Portal ](https://discord.com/developers/applications ))
2025-06-04 15:33:50 -05:00
- For YouTube integration: YouTube Data API v3 key from [Google Cloud Console ](https://console.cloud.google.com/apis/credentials )
2025-05-24 12:53:31 -05:00
### Configuration
2025-06-04 15:33:50 -05:00
#### Worker Service (Discord Bot)
2025-05-24 12:53:31 -05:00
1. Copy the example configuration:
```powershell
2025-06-04 15:33:50 -05:00
Copy-Item src/src/StevesBot.Worker/appsettings.Example.json src/src/StevesBot.Worker/appsettings.Development.json
2025-05-24 12:53:31 -05:00
` ``
2. Update ` appsettings.Development.json` with your Discord bot credentials:
` ``json
{
"DiscordClientOptions": {
"ApiUrl": "https://discord.com/api/",
"AppToken": "YOUR_BOT_TOKEN_HERE",
"Intents": 512
}
}
` ``
2025-06-04 15:33:50 -05:00
#### Webhook Service (YouTube Integration)
1. Copy the example configuration:
` ``powershell
Copy-Item src/src/StevesBot.Webhook/appsettings.Example.json src/src/StevesBot.Webhook/appsettings.Development.json
` ``
2. Update with your API keys and callback URLs for YouTube integration.
### Running the Services
2025-05-24 12:53:31 -05:00
#### Using VS Code Tasks
` ``powershell
2025-06-04 15:33:50 -05:00
# Build the entire solution
2025-05-24 12:53:31 -05:00
dotnet build src/StevesBot.sln
2025-06-04 15:33:50 -05:00
# Run the Discord bot worker
dotnet run --project src/src/StevesBot.Worker
2025-05-24 12:53:31 -05:00
2025-06-04 15:33:50 -05:00
# Run the YouTube webhook service (in separate terminal)
dotnet run --project src/src/StevesBot.Webhook
2025-05-24 12:53:31 -05:00
` ``
## 🏗️ Architecture
### Project Structure
` ``txt
src/
2025-06-04 15:33:50 -05:00
├── src/
│ ├── StevesBot.Library/ # Shared library components
│ │ ├── Discord/ # Common Discord REST client
│ │ └── Telemetry/ # Shared telemetry setup
│ ├── StevesBot.Worker/ # Discord bot worker service
│ │ ├── Discord/ # Discord Gateway client implementation
│ │ │ ├── Gateway/ # WebSocket gateway client
│ │ │ ├── Rest/ # REST API client
│ │ │ └── Shared/ # Common Discord models
│ │ ├── Handlers/ # Discord event handlers
│ │ ├── Telemetry/ # Worker-specific telemetry
│ │ ├── Threading/ # Async utilities
│ │ └── WebSockets/ # WebSocket abstractions
│ └── StevesBot.Webhook/ # YouTube webhook service
│ ├── YouTube/ # YouTube integration components
│ │ ├── Data/ # YouTube API models
│ │ ├── Handlers/ # Webhook request handlers
│ │ └── Tasks/ # Background tasks
│ └── Telemetry/ # Webhook-specific telemetry
├── tests/ # Comprehensive test suites
│ ├── StevesBot.Library.Tests/ # Shared library tests
│ ├── StevesBot.Worker.Tests/ # Worker service tests
│ └── StevesBot.Webhook.Tests/ # Webhook service tests
├── compose.yml # Docker Compose configuration
├── StevesBot.Worker.Dockerfile # Worker service container
└── StevesBot.Webhook.Dockerfile # Webhook service container
2025-05-24 12:53:31 -05:00
` ``
### Key Components
2025-06-04 15:33:50 -05:00
#### Discord Bot Worker
2025-05-24 12:53:31 -05:00
- **DiscordGatewayClient**: Custom WebSocket client for Discord Gateway API
- **Worker**: Background service that manages the bot lifecycle
- **WebSocket Management**: Custom WebSocket factory and connection handling
- **AsyncLock**: Thread-safe async locking mechanism
2025-06-04 15:33:50 -05:00
#### YouTube Webhook Service
- **NotificationHandler**: Processes YouTube webhook notifications
- **SubscriptionWorker**: Manages YouTube PubSubHubbub subscriptions
- **YouTubeDataApiClient**: Integrates with YouTube Data API v3
- **LastPostedStreamStore**: Prevents duplicate stream notifications
#### Shared Library
- **DiscordRestClient**: Reusable Discord REST API client
- **Telemetry Infrastructure**: OpenTelemetry setup and instrumentation
2025-05-24 12:53:31 -05:00
## 🔧 Configuration
2025-06-04 15:33:50 -05:00
### Discord Worker Options
2025-05-24 12:53:31 -05:00
| Setting | Description | Required |
|----------------------------------|-----------------------------------------|----------|
| ` DiscordClientOptions__ApiUrl` | Discord API base URL | Yes |
| ` DiscordClientOptions__AppToken` | Bot token from Discord Developer Portal | Yes |
| ` DiscordClientOptions__Intents` | Discord Gateway intents | Yes |
2025-06-04 15:33:50 -05:00
### YouTube Webhook Options
2026-01-11 18:52:19 -06:00
| Setting | Description | Required |
|-----------------------------------------|-----------------------------------|----------|
| ` YouTubeClientOptions__BaseUrl` | YouTube Data API base URL | Yes |
| ` YouTubeClientOptions__ApiKey` | YouTube Data API v3 key | Yes |
| ` SubscriptionOptions__CallbackUrl` | Webhook callback URL | Yes |
| ` SubscriptionOptions__TopicUrl` | YouTube channel topic URL | Yes |
| ` PubSubClientOptions__BaseUrl` | PubSubHubbub hub URL | Yes |
| ` DiscordNotificationOptions__ChannelId` | Discord channel for notifications | Yes |
2025-06-04 15:33:50 -05:00
2025-05-24 12:53:31 -05:00
### Telemetry Options
| Setting | Description | Required |
|----------------------------|------------------------|----------|
| ` SeqOptions__ServerUrl` | Seq logging server URL | No |
| ` SeqOptions__ApiKey` | Seq API key | No |
2025-05-24 13:27:14 -05:00
| ` SeqOptions__ApiKeyHeader` | Seq API key header | No |
2025-05-24 12:53:31 -05:00
## 🧪 Testing
The project includes a comprehensive test suite with both unit and integration tests:
` ``powershell
# Run all tests
dotnet test src/StevesBot.sln
` ``
### Test Coverage
- **Unit Tests**: Extensive coverage of Discord Gateway client, event handling, and utilities
- **Integration Tests**: WebSocket connection and Discord API integration
- **Mock-based Testing**: Isolated testing with proper dependency injection
## 🌟 Discord Gateway Features
Steve's Bot implements a full-featured Discord Gateway client with:
### Connection Management
- Automatic connection establishment
- Session resumption on disconnection
- Graceful reconnection with exponential backoff
### Heartbeat System
- Automatic heartbeat sending
- Heartbeat acknowledgment tracking
- Connection health monitoring
### Event Handling
- Type-safe event deserialization
- Extensible event handler system
- Proper error handling and logging
### Resilience
- Automatic reconnection on connection loss
- Session state preservation
- Proper cleanup on shutdown
## 📊 Observability
The bot includes comprehensive observability features:
- **Structured Logging**: JSON-formatted logs with contextual information
- **OpenTelemetry**: Distributed tracing and metrics
- **Error Tracking**: Detailed error logging and alerting
## 🐳 Deployment
### Docker Deployment
The project includes a multi-stage Dockerfile for optimized production builds:
` ``dockerfile
# Build stage with .NET SDK
2026-01-11 18:52:19 -06:00
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS base
2025-05-24 12:53:31 -05:00
# ... build steps ...
# Runtime stage with optimized ASP.NET runtime
2026-01-11 18:52:19 -06:00
FROM mcr.microsoft.com/dotnet/aspnet:10.0
2025-05-24 12:53:31 -05:00
# ... runtime setup ...
` ``
### Configuration for Production
Use environment variables or configuration providers for production secrets.
## 🤝 Contributing
1. Fork the repository
2. Create a feature branch (` git checkout -b feature/amazing-feature`)
3. Commit your changes (` git commit -m 'Add some amazing feature'`)
4. Push to the branch (` git push origin feature/amazing-feature`)
5. Open a Pull Request
### Development Guidelines
- Follow the existing code style and patterns
- Add comprehensive tests for new features
- Update documentation for API changes
- Ensure all tests pass before submitting PR
## 📄 License
This project is licensed under the terms found in the [LICENSE.md ](LICENSE.md ) file.
## 🔗 Links
- [Discord Developer Portal ](https://discord.com/developers/applications )
- [Discord API Documentation ](https://discord.com/developers/docs )
- [.NET Documentation ](https://docs.microsoft.com/en-us/dotnet/ )
- [OpenTelemetry .NET ](https://opentelemetry.io/docs/instrumentation/net/ )
---
2026-01-11 18:52:19 -06:00
Built with ❤️ using .NET 10 and a lot of coffee ☕
2025-05-24 12:53:31 -05:00
*Steve's Bot - Helping Stevan and friends since 2025* 🚀