sipeed/picoclaw · error

failed to get bot user: %w

Error message

failed to get bot user: %w

What it means

Start() calls session.User("@me") (GET /users/@me) before opening the gateway to capture botUserID and avoid a race. This error means that REST call failed: invalid/expired token (401), network or proxy failure reaching discord.com, or a Discord API error. The underlying discordgo error is wrapped with %w.

Source

Thrown at pkg/channels/discord/discord.go:115

		typingStop:  make(map[string]chan struct{}),
		bus:         bus,
		voiceSSRC:   make(map[string]map[uint32]string),
	}
	ch.playTTSFn = ch.playTTS
	ch.ttsVoiceFn = ch.voiceConnectionForTTS
	ch.progress = channels.NewToolFeedbackAnimator(ch.EditMessage)
	return ch, nil
}

func (c *DiscordChannel) Start(ctx context.Context) error {
	logger.InfoC("discord", "Starting Discord bot")

	c.ctx, c.cancel = context.WithCancel(ctx)

	// Get bot user ID before opening session to avoid race condition
	botUser, err := c.session.User("@me")
	if err != nil {
		return fmt.Errorf("failed to get bot user: %w", err)
	}
	c.botUserID = botUser.ID

	c.session.AddHandler(c.handleMessage)

	go c.listenVoiceControl(c.ctx)

	if err := c.session.Open(); err != nil {
		return fmt.Errorf("failed to open discord session: %w", err)
	}

	c.SetRunning(true)

	logger.InfoCF("discord", "Discord bot connected", map[string]any{
		"username": botUser.Username,
		"user_id":  botUser.ID,
	})

View on GitHub (pinned to 49183d7e8d)

Solutions

  1. Verify the token independently: curl -H 'Authorization: Bot <token>' https://discord.com/api/v10/users/@me should return 200 with the bot JSON
  2. Fix proxy configuration: valid scheme in discord.proxy (http/https/socks5) or unset broken HTTP_PROXY/HTTPS_PROXY
  3. Check network egress/DNS from the host and retry Start on transient failures
  4. If 401, create a new token in the Developer Portal and update config
Defensive patterns

Strategy: validation

Validate before calling

// Pre-flight the token before Start()
req, _ := http.NewRequest(http.MethodGet, "https://discord.com/api/v10/users/@me", nil)
req.Header.Set("Authorization", "Bot "+cfg.Token.String())
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode != 200 {
    return fmt.Errorf("discord token invalid or discord unreachable: %v / %d", err, respStatus(resp))
}

Try / catch

if err := ch.Start(ctx); err != nil {
    if strings.Contains(err.Error(), "failed to get bot user") {
        // 401 => bad token (fix config); network error => fix egress/proxy, then retry
    }
    return err
}

Prevention

When it happens

Trigger: Token was regenerated in the Developer Portal (old token revoked), token typo/truncated, HTTP(S)_PROXY env or cfg.Proxy pointing to an unreachable proxy (applyDiscordProxy installs it on the session client), DNS/firewall blocking discord.com, Discord 5xx.

Common situations: Token reset in the portal but config not updated, container without network egress, proxy env vars leaking into the process, transient Discord API issues at startup.

Related errors


AI-assisted analysis of sipeed/picoclaw@49183d7e8d (2026-08-15). Data as JSON: /api/errors/bd8b535ca2a6b475. Report an issue: GitHub.