Event System Architecture
AutoResponse uses a dynamic event loading system that automatically discovers and registers event handlers from thesrc/events/ directory. The event system is initialized in src/client.js and provides robust error handling and event management capabilities.
Event Loading Process
The bot follows this process when loading events:Validate Event Structure
Each event must export an object with:
name: The Discord event nameexecute: An async function that handles the event
Event Handler Structure
All event handlers follow a consistent structure. Here’s the template:All event handlers include try-catch blocks for error handling and logging to ensure the bot remains stable even when individual events fail.
Core Events
ready
Fired when the bot successfully connects to Discord. This event handles initialization tasks. Location:src/events/ready.js
- Register application commands with Discord API
- Initialize leaderboard database
- Load guild-specific settings from databases
messageCreate
The primary event for handling incoming messages and implementing auto-reply functionality. Location:src/events/messageCreate.js
Key Features:
- Owner command processing with prefix
- Opt-out list checking
- Reply cooldown management
- Attachment downloading
- Dynamic reply chance system
- Support for embeds and polls
interactionCreate
Handles slash commands and context menu interactions. Location:src/events/interactionCreate.js
- Slash commands (
/ping,/help, etc.) - Context menu commands (user and message context menus)
- Proper reply/edit handling for deferred interactions
Guild Events
guildCreate
Fired when the bot joins a new server.guildMemberAdd
Monitors new members joining and reacts to their first message with trust indicators.Message Events
messageDelete
Logs deleted messages with full context.messageUpdate
Tracks message edits (implementation varies by needs).Error Events
error
Handles Discord client errors.warn
Handles Discord client warnings.Disabled Events System
You can disable specific events by creating adata/disabledEvents.json file:
Why disable events?
Why disable events?
Some events like
typingStart and presenceUpdate can generate excessive logs and consume resources without providing value for most use cases. Disabling them improves performance.Complete Event List
AutoResponse includes handlers for 75+ Discord events:Message Events
Message Events
messageCreate- New message receivedmessageDelete- Message deletedmessageDeleteBulk- Multiple messages deletedmessageUpdate- Message editedmessageReactionAdd- Reaction addedmessageReactionRemove- Reaction removedmessageReactionRemoveAll- All reactions removedmessageReactionRemoveEmoji- Specific emoji reactions removedmessagePollVoteAdd- Poll vote addedmessagePollVoteRemove- Poll vote removed
Guild Events
Guild Events
guildCreate- Bot joins serverguildDelete- Bot leaves serverguildUpdate- Server settings updatedguildAvailable- Server becomes availableguildUnavailable- Server becomes unavailableguildMemberAdd- Member joinsguildMemberRemove- Member leavesguildMemberUpdate- Member updatedguildBanAdd- Member bannedguildBanRemove- Member unbanned
Channel Events
Channel Events
channelCreate- Channel createdchannelDelete- Channel deletedchannelUpdate- Channel updatedchannelPinsUpdate- Pins updated
Thread Events
Thread Events
threadCreate- Thread createdthreadDelete- Thread deletedthreadUpdate- Thread updatedthreadMemberUpdate- Thread member updatedthreadMembersUpdate- Thread members updated
Role & Emoji Events
Role & Emoji Events
roleCreate- Role createdroleDelete- Role deletedroleUpdate- Role updatedemojiCreate- Emoji createdemojiDelete- Emoji deletedemojiUpdate- Emoji updated
Automod Events
Automod Events
autoModerationActionExecution- Automod action triggeredautoModerationRuleCreate- Automod rule createdautoModerationRuleDelete- Automod rule deletedautoModerationRuleUpdate- Automod rule updated
Best Practices
Always Include Error Handling
Wrap event logic in try-catch blocks and use the logging utilities to report errors.
Use Event-Specific Logging
Import event-specific logging functions from
utils/logging.js for consistent log formatting.Handle Partial Data
Some events may receive partial objects. Always check for partials and handle them appropriately.
Clean Up Listeners
Remove event listeners when they’re no longer needed to prevent memory leaks.
Testing Events
To test event handlers:- Enable debug logging in your environment
- Trigger the event in Discord (send a message, join a server, etc.)
- Check the console output and
data/logs.dbfor event logs - Verify the event handler executed successfully
All events are automatically logged to the SQLite database in
data/logs.db with timestamps and event types for debugging.