Overview
Faculty Bot uses PostgreSQL as its primary data store. The database schema is defined inmigrations/faculty_manager.sql and mapped to Rust structs using SQLx.
Database Tables
verified_users
Stores user verification data linking Discord accounts to student emails.src/structs.rs
- Track which users have verified their student status
- Prevent duplicate verifications
- Allow staff to look up user emails
user_xp
Tracks user experience points and levels for the gamification system.src/structs.rs
src/eventhandler.rs
voice_channels
Tracks dynamically created temporary voice channels.src/structs.rs
- User joins the “Create Channel” voice channel
- Bot creates a new voice channel owned by the user
- Entry added to
voice_channelstable - When channel becomes empty, bot deletes it and removes entry
mensaplan
Tracks meal plan posting history to prevent duplicates.src/structs.rs
src/tasks.rs
ads
Tracks advertisement messages for automatic deletion.src/structs.rs
- Track when ads are posted
- Enable automatic deletion after configured timeout
- Configured via
config.json→general.adstimeout
rules
Stores server rules that can be managed via commands.src/structs.rs
semestermods
Tracks users with semester moderator privileges.src/structs.rs
- Track semester moderators beyond Discord roles
- Enable additional permission checks in commands
- Used alongside
staffrolefor authorization
posted_rss
Tracks RSS feed items that have been posted to prevent duplicates.src/structs.rs
- Fetch RSS feed items
- Check if
rss_titleexists inposted_rssfor this channel - If new: post and insert into database
- If exists: check timestamp and update if newer
Database Migrations
To initialize the database:IF NOT EXISTS checks, making it safe to run multiple times.
Connection Pooling
The bot uses SQLx connection pooling for efficient database access:src/main.rs
Query Patterns
Insert or Update (Upsert)
Fetch Optional
Fetch Multiple
Type Safety
SQLx provides compile-time verification of SQL queries when used with thequery! macro:
query_as for flexibility during development.
Database Access in Commands
Access the database pool through the context:Indexing Considerations
All tables use primary keys for efficient lookups:user_id: Primary key inverified_users,user_xp,semestermodschannel_id: Primary key invoice_channelsmessage_id: Primary key inads,posted_rssdate: Primary key inmensaplanrule_number: Primary key with UNIQUE constraint inrules