Project Structure
Faculty Bot is organized into a modular structure for maintainability and clarity:Core Components
1. Main Entry Point (main.rs)
Themain.rs file orchestrates the entire application startup:
src/main.rs
- Initialize the Discord bot via
start_bot() - Launch the Rocket web server for API endpoints
- Set up graceful shutdown handling
- Run both services concurrently with
tokio::select!
2. Bot Initialization Flow
Thestart_bot() function sets up the Discord bot:
src/main.rs
3. Data Struct (Global State)
TheData struct holds shared state accessible to all commands and handlers:
src/main.rs
4. Error Handling
Centralized error handling through a custom error enum:src/main.rs
Command System
Command Organization
Commands are organized by category insrc/commands/:
- user.rs: Commands for all users (verify, xp, leaderboard)
- administration.rs: Admin-only commands (getmail, set-xp, force-post-mensaplan)
- moderation.rs: Moderation tools (pin, delete, promote/demote)
Command Registration
All commands are registered inmain.rs:
src/main.rs
Event System
The event handler processes Discord events ineventhandler.rs:
src/eventhandler.rs
Background Tasks
Background tasks run concurrently with the bot intasks.rs:
Task Types
-
Mensaplan Posting (tasks.rs:33-117)
- Checks for new meal plans at configured intervals
- Posts to designated channel on specific weekdays
- Prevents duplicate posts via database tracking
-
RSS Feed Monitoring (tasks.rs:119-221)
- Fetches RSS feeds at regular intervals
- Posts new items or updates existing ones
- Tracks posted items in database
-
Latency Logging (tasks.rs:342-371)
- Monitors bot latency to Discord API
- Logs metrics to InfluxDB every 60 seconds
Web Server (Rocket)
The web component provides:- API Endpoints (
src/web/api/): REST API for verification - Authentication (
src/web/auth.rs): JWT-based authentication - Templates (
templates/): Handlebars templates for web UI
Web Routes
src/main.rs
Database Layer
The bot uses SQLx for compile-time checked SQL queries:structs.rs.
Internationalization
The bot supports multiple languages usingrosetta-i18n:
src/main.rs
i18n/*.json files (see build.rs).
Concurrency Model
The application uses Tokio for async runtime:- Multi-threaded: Bot and web server run concurrently
- Task spawning: Background tasks spawn independent Tokio tasks
- Channel communication: MPSC channels for email queue
- Shared state: Arc and DashMap for thread-safe state sharing
Configuration Management
Two-tiered configuration:- Environment Variables (
.env): Secrets and deployment-specific settings - Config File (
config.json): Bot behavior and Discord IDs
Data struct for global access.