Event Handlers
Documentation for automatic event handlers in the LuaBeans - Community Manager Discord bot.
Overview
The bot automatically handles Discord events to keep the web panel in sync with Discord. These events run automatically and require no user interaction.
Event Handlers
guildMemberAdd
Fired when a new member joins the Discord server.
Handler: bot/events/guildMemberAdd/00-syncUser.js
What it does:
Creates or updates user record in database
Syncs Discord roles to department memberships
Assigns LEO ranks based on Discord role mapping
Updates RBAC permissions cache
Configuration: None required - runs automatically
When it runs: Immediately when member joins
Logging: Logs to console with user information
guildMemberUpdate
Fired when a member's information is updated (roles changed, nickname changed, etc.).
Handler: bot/events/guildMemberUpdate/00-syncUser.js
What it does:
Updates user record in database
Syncs updated Discord roles to department memberships
Updates LEO rank assignments based on role changes
Refreshes RBAC permissions cache
Configuration: None required - runs automatically
When it runs: When member roles or information changes
Logging: Logs role changes to console
Common Triggers:
Discord role added to member
Discord role removed from member
Member nickname changed
Member avatar changed
messageCreate
Fired when a message is created in a Discord channel.
Handler: bot/events/messageCreate/00-tebexPurchase.js
What it does:
Checks if message is from Tebex bot
Parses purchase information from message
Creates purchase record in database
Links purchase to user (if identified)
Processes for garage redemption workflow
Configuration:
TEBEX_BOT_ID- Tebex bot's Discord IDTEBEX_ALLOWED_CHANNEL_IDS- Channels to monitor for purchasesTEBEX_DEBUG- Enable debug logging
When it runs: When message is posted in monitored channels
Use Case: Automatic garage slot assignment for Tebex purchases
Notes:
Only processes messages from Tebex bot
Only monitors configured channels
Requires Tebex bot integration setup
interactionCreate
Fired when an interaction is created (slash command, button, etc.).
Handler: bot/events/interactionCreate/00-autocomplete.js
What it does:
Handles autocomplete for slash commands
Provides department suggestions
Provides user suggestions
Configuration: None required - runs automatically
When it runs: When user types slash command with autocomplete options
Use Case: Command autocomplete functionality
ready
Fired when bot is ready and connected to Discord.
Handler: bot/events/ready/00-startup.js
What it does:
Logs bot startup information
Registers commands (if needed)
Initializes bot state
Verifies database connection
Configuration: None required - runs automatically
When it runs: On bot startup
Logging: Logs bot readiness to console
Example Output:
Event Configuration
Enabling/Disabling Events
Events are enabled by default. To disable an event:
Remove or rename the event file in
bot/events/Or modify the event handler to return early
Note: Disabling critical events (like guildMemberAdd and guildMemberUpdate) will prevent automatic role syncing.
Event Order
Events are processed in this order:
ready - Bot initialization
guildMemberAdd - New member handling
guildMemberUpdate - Member update handling
messageCreate - Message processing
interactionCreate - Command handling
Rate Limiting
The bot automatically handles Discord API rate limits:
Retries failed requests automatically
Logs rate limit warnings
Respects rate limit headers
Queues requests when rate limited
Rate Limit Logging:
Troubleshooting Events
Events Not Firing
Issue: Events aren't running
Solutions:
Check bot is online (green dot)
Verify required intents are enabled (Members Intent)
Check console for errors
Verify event files exist in
bot/events/Check bot has required permissions
Role Sync Not Working
Issue: Discord roles not syncing to web panel
Solutions:
Check
guildMemberUpdateevent is runningVerify bot has permission to read members
Check Members Intent is enabled
Verify
DISCORD_GUILD_IDis correctManually trigger sync:
/syncmecommand
Tebex Events Not Processing
Issue: Purchase messages not being processed
Solutions:
Verify
TEBEX_BOT_IDis correctCheck
TEBEX_ALLOWED_CHANNEL_IDSincludes target channelVerify Tebex bot is posting messages
Check message format matches expected pattern
Enable
TEBEX_DEBUG=truefor detailed logging
Database Errors in Events
Issue: Events fail with database errors
Solutions:
Verify database connection is working
Check database credentials in
.envVerify database is running
Check database logs for errors
Ensure tables exist (run schema if needed)
Event Logging
All events log to console. In production, redirect logs to files:
Log Levels
Info: Normal event processing
Warn: Rate limiting, non-critical issues
Error: Failed event processing, critical errors
Log Format
Examples:
Best Practices
Monitor Event Logs - Check logs regularly for errors
Handle Rate Limits - Bot handles automatically, but monitor for excessive limits
Verify Permissions - Ensure bot has required permissions for events
Test Events - Test events after bot updates
Database Health - Ensure database is healthy for event processing
Related Documentation
Bot Setup - Installing and configuring the bot
Bot Commands - Available commands
Bot Permissions - Required permissions
Last updated
