chenhg5/cc-connect · error

discord: create session: %w

Error message

discord: create session: %w

What it means

discordgo.New() failed to construct the bot session object before any network connection is attempted. discordgo.New only parses the token string; it errors when the token is empty or in an unrecognized format, so this error almost always means the Discord bot token was not loaded correctly. It is raised in buildSession, which is re-invoked on every reconnect attempt by connectLoop, so a bad token will produce this error on every retry.

Source

Thrown at platform/discord/discord.go:567

		p.mu.Unlock()
		return fmt.Errorf("discord: platform stopped")
	}
	p.handler = handler
	ctx, cancel := context.WithCancel(context.Background())
	p.cancel = cancel
	p.mu.Unlock()

	go p.connectLoop(ctx)
	return nil
}

// buildSession creates a fresh discordgo session with proxy + handlers wired up.
// It is called once per connect attempt so a session that failed mid-handshake
// is fully discarded before the next retry.
func (p *Platform) buildSession() (*discordgo.Session, error) {
	session, err := discordgo.New("Bot " + p.token)
	if err != nil {
		return nil, fmt.Errorf("discord: create session: %w", err)
	}
	if p.proxyURL != nil {
		transport := &http.Transport{Proxy: http.ProxyURL(p.proxyURL)}
		session.Client = &http.Client{Transport: transport, Timeout: 60 * time.Second}
		session.Dialer = &websocket.Dialer{Proxy: http.ProxyURL(p.proxyURL)}
		slog.Info("discord: using proxy", "proxy", p.proxyURL.Host)
	}

	session.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages | discordgo.IntentMessageContent

	session.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
		p.botID = r.User.ID
		p.appID = r.User.ID
		slog.Info("discord: connected", "bot", r.User.Username+"#"+r.User.Discriminator)
		// Signal readiness before guild role lookups so RegisterCommands
		// is not blocked by slow API calls when there are many guilds.
		select {
		case <-p.readyCh:

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Check that the Discord bot token is actually set in config.toml (or the env var) and is non-empty — echo/print its length at startup to confirm
  2. Verify the token is a Bot token from the Discord Developer Portal (starts with a bot-ID prefix) with no surrounding quotes or whitespace
  3. Recreate the token if it was reset or invalidated, and restart cc-connect
  4. Rebuild with the latest config parsing if you recently changed the config schema

Example fix

// before (config.toml)
[platforms.discord]
# token not set
// after
[platforms.discord]
token = "MTIzNDU2Nzg5.GaBcDe.xxxxxxxxxxxxxxxxxxxxxxxx"
Defensive patterns

Strategy: validation

Validate before calling

if token == "" {
    return fmt.Errorf("discord: bot token is empty; set platforms.discord.token in config.toml")
}
if strings.ContainsAny(token, " \t\r\n\"'") {
    return fmt.Errorf("discord: bot token contains whitespace or quotes")
}

Prevention

When it happens

Trigger: Calling Start() when p.token is empty (missing DISCORD_BOT_TOKEN in config.toml, wrong TOML key, or env var not set) or when the token contains stray whitespace/quotes or is a non-bot (user) token that discordgo's parser rejects.

Common situations: Fresh deployments where the bot token was never filled in; config files where the token key was renamed or placed in the wrong section; CI environments lacking the secret; copying a token with surrounding whitespace or quotes; using an OAuth client secret instead of a bot token.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/fea51051c6fcd296. Report an issue: GitHub.