{"record":{"id":"fea51051c6fcd296","repo":"chenhg5/cc-connect","slug":"discord-create-session-w","errorCode":null,"errorMessage":"discord: create session: %w","messagePattern":"discord: create session: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"platform/discord/discord.go","lineNumber":567,"sourceCode":"\t\tp.mu.Unlock()\n\t\treturn fmt.Errorf(\"discord: platform stopped\")\n\t}\n\tp.handler = handler\n\tctx, cancel := context.WithCancel(context.Background())\n\tp.cancel = cancel\n\tp.mu.Unlock()\n\n\tgo p.connectLoop(ctx)\n\treturn nil\n}\n\n// buildSession creates a fresh discordgo session with proxy + handlers wired up.\n// It is called once per connect attempt so a session that failed mid-handshake\n// is fully discarded before the next retry.\nfunc (p *Platform) buildSession() (*discordgo.Session, error) {\n\tsession, err := discordgo.New(\"Bot \" + p.token)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"discord: create session: %w\", err)\n\t}\n\tif p.proxyURL != nil {\n\t\ttransport := &http.Transport{Proxy: http.ProxyURL(p.proxyURL)}\n\t\tsession.Client = &http.Client{Transport: transport, Timeout: 60 * time.Second}\n\t\tsession.Dialer = &websocket.Dialer{Proxy: http.ProxyURL(p.proxyURL)}\n\t\tslog.Info(\"discord: using proxy\", \"proxy\", p.proxyURL.Host)\n\t}\n\n\tsession.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages | discordgo.IntentMessageContent\n\n\tsession.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {\n\t\tp.botID = r.User.ID\n\t\tp.appID = r.User.ID\n\t\tslog.Info(\"discord: connected\", \"bot\", r.User.Username+\"#\"+r.User.Discriminator)\n\t\t// Signal readiness before guild role lookups so RegisterCommands\n\t\t// is not blocked by slow API calls when there are many guilds.\n\t\tselect {\n\t\tcase <-p.readyCh:","sourceCodeStart":549,"sourceCodeEnd":585,"githubUrl":"https://github.com/chenhg5/cc-connect/blob/4000b2338aa6e850c99df54f8b0ed6ed7460b401/platform/discord/discord.go#L549-L585","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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","Verify the token is a Bot token from the Discord Developer Portal (starts with a bot-ID prefix) with no surrounding quotes or whitespace","Recreate the token if it was reset or invalidated, and restart cc-connect","Rebuild with the latest config parsing if you recently changed the config schema"],"exampleFix":"// before (config.toml)\n[platforms.discord]\n# token not set\n// after\n[platforms.discord]\ntoken = \"MTIzNDU2Nzg5.GaBcDe.xxxxxxxxxxxxxxxxxxxxxxxx\"","handlingStrategy":"validation","validationCode":"if token == \"\" {\n    return fmt.Errorf(\"discord: bot token is empty; set platforms.discord.token in config.toml\")\n}\nif strings.ContainsAny(token, \" \\t\\r\\n\\\"'\") {\n    return fmt.Errorf(\"discord: bot token contains whitespace or quotes\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always load the token from config/env and fail fast at startup if empty","Trim whitespace and quotes around the token before constructing the session","Keep bot tokens in secrets management, never hardcode them","Log token length (not value) on startup to confirm it loaded"],"tags":["discord","discordgo","config","authentication"],"backgroundTag":"missing-api-key","analyzedSha":"4000b2338aa6e850c99df54f8b0ed6ed7460b401","analyzedAt":"2026-09-06T11:45:09.575Z","contentChangedAt":"2026-09-06T11:45:09.575Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}