How sabafly/gobot works
Project Type and Purpose
• gobot is a Discord bot application.
• It listens for events from the Discord Gateway, dispatches them to command handlers, applies business logic (e.g. leveling, role panels, games), and persists data to a MySQL database via the Ent ORM.
• It is packaged as a Dockerized service (the “gobot” container) alongside a MySQL container in docker-compose.
File Structure
• Top-level:
– main.go and cmd/root.go: bootstrap the application and CLI flags.
– bot/: command and component definitions.
– internal/: utility packages (i18n, parsing, permissions, rate-limiting, etc.).
– ent/: generated ORM code and schema, plus migration logic.
– database/: abstraction over storage (memory or SQL).
– mysql/: Dockerfile and init scripts for the database.
– Dockerfile, docker-compose.yml: container definitions.
– .github/workflows: CI, linting, security scans.
• Patterns:
– Layered architecture: event ingestion → command dispatch → business logic → persistence.
– Hexagonal flavor: “bot/components” define ports/adapters for Discord, “internal” contains core utilities.
README for Insights
• Confirms it’s a multi-feature Discord bot under active development.
• Mentions localization via Crowdin (lang/ and locales/ directories).
• CI badges indicate GitHub Actions pipelines for build and security.
Main Components
a. Discord Platform (Gateway API)
b. gobot Service:
– Event Listener (DiscordGo wrapper)
– Command Dispatcher (bot/components/commands.go)
– Command Handlers (bot/commands/*): debug, play, level, role, etc.
– Core Services (internal/*): i18n, rate-limit, permissions, scheduler, translation, UUID, xp calculation.
– Database Abstraction (database/ + memory.go)
– Ent ORM Client (ent/client.go + ent/migrate)
c. MySQL Database (mysql container via docker-compose)
d. External Services:
– Localization (Crowdin integration, lang/*.yaml)
– CI/CD (GitHub Actions workflows)
Relationships and Interactions
1. Discord Gateway emits events → gobot’s Event Listener
2. Events → Command Dispatcher → Specific Command Handler
3. Command invokes Core Service(s) for business logic
4. If persistence is needed: Service calls Database Abstraction → Ent ORM → MySQL
5. Responses (embeds, messages) flow back through Discord API to the user
6. Localization subsystem resolves strings via lang/ and locales/
Architectural Patterns and Design Principles
• Layered architecture: clearly separate I/O (Discord, DB) from business logic.
• Hexagonal/adapters: “internal” as core logic, “bot/components” and “database” as ports/adapters.
• Code generation: Ent provides schema-first ORM and migrations.
• Dependency injection via simple constructors in bot/components/config.go.
Technologies, Frameworks, Libraries
• Go + DiscordGo library
• Ent ORM for type-safe database access
• MySQL as persistent store
• Docker & Docker Compose for containerization
• GitHub Actions for CI/CD
• Crowdin for localization
Detail Without Overthinking
• Every high-level component listed above.
• Startup flow (main.go → config → connect to Discord & MySQL → migrate schemas → start event loop).