Use this startup order:
- Validate environment variables.
- Build command declarations and handle validation errors.
- Create the session and explicitly set intents.
- Register all routes, checking every returned error.
- Attach
router.Handleand open the session. - Synchronize commands with the application ID and chosen guild scope.
- Wait for shutdown and close the connection.
Use func run() error to own the session and defer session.Close() after a successful open. Return startup errors from run; log the final failure in main, so cleanup can run before process exit.
SyncOptions{GuildID: guildID} registers in a test server. An empty GuildID means global commands. Delete: true removes remote commands outside your declaration; enable it only when this process owns the entire command set in that scope.
Follow the support bot cookbook for a complete program using this lifecycle.