How hoanbucon/discord-bot works
Main Components
• Bot Entry Point: At the center is the main bot file (src/index.ts), which initializes the Discord client and sets the stage for event handling.
• Command Handlers: Under the “handlers” directory, you have CommandHandler.ts, PrefixHandler.ts, and SlashHandler.ts. These components are responsible for interpreting incoming messages and dispatching them to the corresponding commands.
• Commands: The “src/commands” directory is divided into generic command definitions (Command.ts) and a subdirectory (botCommands) containing specific command files (e.g., BanCommand.ts, KickCommand.ts, etc.) that implement the bot’s functionalities.
• Data and Media Resources: Within botCommands, the “dataFiles” subdirectory stores JSON files (like bannedUsers.json, mutedUsers.json, tictactoeData.json) and various multimedia resources used by commands (organized within media folders for meme commands).
• Utilities: The “utils” folder contains helper modules (like BanDataManager, MuteDataManager, DonutUtils, TictactoeGameplay, etc.) that encapsulate business logic, manage data persistence, and assist with various operations.
• Interfaces: The “interfaces” directory holds TypeScript interfaces which ensure consistent data structures (e.g., ICommand, IBaseData).
Relationships and Interactions
• The Discord Bot’s entry point (index.ts) initializes the Discord client and registers the appropriate event listeners.
• Incoming Discord events (messages, slash commands) are routed to the Command Handlers (PrefixHandler and SlashHandler) which rely on the CommandHandler to delegate the specific commands.
• Each command (e.g., BanCommand, HelpCommand) encapsulated within its file is invoked by the handler. Commands may reference their corresponding utility functions (like data managers or file utilities) to perform operations.
• The utility modules interact with the file system to read/write JSON files or load media assets, while also handling permissions and other command-specific logic.
• External dependencies (Discord.js and Bun) should be highlighted, as they support real-time communication and rapid development.
Architectural Patterns & Design Principles
• Separation of Concerns: Command implementations, event handling, and business logic (in utilities) are separated into distinct directories.
• Modular Design: Commands are separated into individual files, making them easily extensible and maintainable.
• Clear Data Management: Data persistence for banned/muted users and specific game states (Tictactoe) is managed via JSON files in the dataFiles folder.
Final Advice
• Modular design: each command and utility is an independent unit, promoting separation of concerns.