Documentation
¶
Overview ¶
Package gdiscordbot is a high-level framework over github.com/bwmarrin/discordgo for building Discord bots. It provides a Router for interactions, typed Option Handles for Command options, middleware, diff-then-bulk Command syncing, Modules, Event listeners, and capability-based error handling. See the docs/ directory for the full design.
Package gdiscordbot is the root framework package. It provides the Bot type, lifecycle management (New, Run, Start, Close), functional options, and re-exports the interaction Context so bot authors import only this package in their handlers.
Usage: create a Bot with New, register Modules via WithModule, then call Run (blocking) or Start + Close (non-blocking). See SPEC §2.1, §2.6 and CONTEXT.md for terminology.
Index ¶
- type Bot
- type Context
- type Handler
- type Middleware
- type Option
- func WithDevGuild(guildID string) Option
- func WithErrorMiddleware(mw ...middleware.Middleware) Option
- func WithIntents(i discordgo.Intent) Option
- func WithLogger(l *slog.Logger) Option
- func WithMiddleware(mw ...middleware.Middleware) Option
- func WithModule(m module.Module) Option
- func WithModules(mods ...module.Module) Option
- func WithSyncer(s syncdiscord.Syncer) Option
- func WithoutAutoSync() Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bot ¶
Bot is the top-level framework object. It wires Modules onto the Router, manages the Discord gateway lifecycle, and synchronizes the application command catalog at startup.
Embed or compose Bot to build your application. Always use Bot.Session as the escape hatch for discordgo capabilities not exposed by this framework (ADR 0001).
func New ¶
New creates a Bot with the given token and options. It validates the token, creates the discordgo session, applies global middleware, and wires the router's Dispatch handler. It does NOT connect to the Discord gateway — call Run or Start for that.
Returns an error if token is empty or if discordgo session creation fails.
func (*Bot) Close ¶
Close closes the Discord gateway session. Call after cancelling the context passed to Start to ensure a clean shutdown.
func (*Bot) Run ¶
Run opens the Discord gateway, runs the full lifecycle, and blocks until ctx is cancelled. It returns after all modules' Teardown completes.
Lifecycle: Open gateway → Register modules → Sync commands → Setup modules (in order) → block on ctx → Teardown modules (reverse order).
func (*Bot) Start ¶
Start opens the Discord gateway and launches the lifecycle in a goroutine. It returns once the gateway is open, or on error. The goroutine exits (after Teardown) when ctx is cancelled. Call Close after cancelling ctx for a clean shutdown. Use Wait to observe the lifecycle goroutine's error after it exits.
type Context ¶
Context is the per-interaction object handed to every handler. It is a type alias for gdctx.Context so that bot authors import only this root package. See SPEC §2.2 and Q1.
type Middleware ¶
type Middleware = middleware.Middleware
Middleware is the middleware function signature. Type alias for middleware.Middleware.
type Option ¶
type Option func(*config)
Option is a functional option for configuring a Bot. Pass Options to New.
func WithDevGuild ¶
WithDevGuild configures a guild ID for instant command sync during development. When set, commands sync exclusively to that guild (instant propagation) instead of the global scope. Use for development iteration; remove or leave empty to sync globally in production. An empty guildID is a no-op.
func WithErrorMiddleware ¶
func WithErrorMiddleware(mw ...middleware.Middleware) Option
WithErrorMiddleware prepends middleware in front of the default error-handling middleware (Phase 7). The prepended middleware runs before the framework's built-in error resolution logic. Repeatable — each call appends to the error middleware list.
func WithIntents ¶
WithIntents overrides the default Discord gateway intents. The default is discordgo.IntentsGuilds | discordgo.IntentsGuildMessages (non-privileged baseline). Provide all required intents; this call replaces (does not append to) the default.
func WithLogger ¶
WithLogger sets the logger used by the Bot, Router, and event listeners. The default is slog.Default().
func WithMiddleware ¶
func WithMiddleware(mw ...middleware.Middleware) Option
WithMiddleware appends global interaction middleware. Applied to all handlers registered after New returns, in global → module → route composition order. Repeatable — each call appends to the global middleware stack.
func WithModule ¶
WithModule registers a Module with the Bot. Modules are initialized in registration order (Setup) and torn down in reverse order (Teardown). Repeatable — each call appends the module to the list.
func WithModules ¶
WithModules registers multiple Modules at once. Equivalent to calling WithModule for each module in order.
func WithSyncer ¶
func WithSyncer(s syncdiscord.Syncer) Option
WithSyncer injects a custom Syncer. The default is sync.NewBulkSyncer(). Inject a fake Syncer in tests to verify sync behavior without hitting Discord.
func WithoutAutoSync ¶
func WithoutAutoSync() Option
WithoutAutoSync disables the automatic command sync that normally runs at startup. Use when you manage the command catalog yourself or want to skip it in tests.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package command declares Commands and their typed Option Handles.
|
Package command declares Commands and their typed Option Handles. |
|
Package errs defines the optional Error capability interfaces a domain error may implement to control how the framework presents it: UserFacing (supplies a user-safe message) and Presenter (renders its own response).
|
Package errs defines the optional Error capability interfaces a domain error may implement to control how the framework presents it: UserFacing (supplies a user-safe message) and Presenter (renders its own response). |
|
Package event provides the Event listener family: typed handlers over raw discordgo gateway events (for example MessageCreate or GuildMemberAdd), bundled in a Module.
|
Package event provides the Event listener family: typed handlers over raw discordgo gateway events (for example MessageCreate or GuildMemberAdd), bundled in a Module. |
|
examples
|
|
|
kitchensink
command
Command kitchensink is a comprehensive tour of the gdiscordbot framework.
|
Command kitchensink is a comprehensive tour of the gdiscordbot framework. |
|
Package gdctx defines the per-interaction Context handed to interaction Handlers.
|
Package gdctx defines the per-interaction Context handed to interaction Handlers. |
|
Package middleware defines the Middleware type — func(next Handler) Handler — and the built-in middleware (RequirePermissions, Cooldown, Recover).
|
Package middleware defines the Middleware type — func(next Handler) Handler — and the built-in middleware (RequirePermissions, Cooldown, Recover). |
|
Package module defines the Module interface — a compile-time unit that bundles related Commands, Component and Modal Routes, and Event listeners, with optional Setup and Teardown hooks.
|
Package module defines the Module interface — a compile-time unit that bundles related Commands, Component and Modal Routes, and Event listeners, with optional Setup and Teardown hooks. |
|
Package router provides the Router: the single component that dispatches every incoming interaction — Commands, Component and Modal Routes, and autocomplete — to a Handler, through one unified API and middleware model.
|
Package router provides the Router: the single component that dispatches every incoming interaction — Commands, Component and Modal Routes, and autocomplete — to a Handler, through one unified API and middleware model. |
|
Package sync reconciles the bot's declared Command set with what Discord has registered.
|
Package sync reconciles the bot's declared Command set with what Discord has registered. |