{"record":{"id":"cf8b22b08de302dc","repo":"discordjs/discord.js","slug":"token-has-not-been-set","errorCode":null,"errorMessage":"Token has not been set","messagePattern":"Token has not been set","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/ws/src/ws/WebSocketManager.ts","lineNumber":262,"sourceCode":"\tprivate shardIds: number[] | null = null;\n\n\t/**\n\t * Strategy used to manage shards\n\t *\n\t * @defaultValue `SimpleShardingStrategy`\n\t */\n\tprivate readonly strategy: IShardingStrategy;\n\n\t/**\n\t * Gets the token set for this manager. If no token is set, an error is thrown.\n\t * To set the token, use {@link WebSocketManager.setToken} or pass it in the options.\n\t *\n\t * @remarks\n\t * This getter is mostly used to pass the token to the sharding strategy internally, there's not much reason to use it.\n\t */\n\tpublic get token(): string {\n\t\tif (!this.#token) {\n\t\t\tthrow new Error('Token has not been set');\n\t\t}\n\n\t\treturn this.#token;\n\t}\n\n\tpublic constructor(options: CreateWebSocketManagerOptions) {\n\t\tif (typeof options.fetchGatewayInformation !== 'function') {\n\t\t\tthrow new TypeError('fetchGatewayInformation is required');\n\t\t}\n\n\t\tsuper();\n\t\tthis.options = {\n\t\t\t...DefaultWebSocketManagerOptions,\n\t\t\t...options,\n\t\t};\n\t\tthis.strategy = this.options.buildStrategy(this);\n\t\tthis.#token = options.token ?? null;\n\t}","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/discordjs/discord.js/blob/a81ed8a306d37fdc746e26a634b6a42164ba2c8c/packages/ws/src/ws/WebSocketManager.ts#L244-L280","documentation":"WebSocketManager's token getter throws if no token has been stored on the manager. The getter is used internally to hand the token to the sharding strategy, so accessing it (or triggering anything that reads it) before setToken() has been called fails.","triggerScenarios":"Accessing manager.token before calling setToken(); constructing a manager and connecting without ever providing a token.","commonSituations":"Forgetting to load the token from environment variables (e.g. DISCORD_TOKEN undefined); calling connect() before setToken() in custom sharding-strategy setups; token set on the REST instance but never on the WebSocketManager.","solutions":["Call setToken(yourToken) before accessing token or calling connect()","Validate the token env var is defined and non-empty at startup","If auth-free usage is intended, don't use this manager's token getter"],"exampleFix":"// before\nconst manager = new WebSocketManager(options);\nawait manager.connect(); // reads token internally -> throws\n// after\nconst manager = new WebSocketManager(options);\nmanager.setToken(process.env.DISCORD_TOKEN!);\nawait manager.connect();","handlingStrategy":"validation","validationCode":"const token = process.env.DISCORD_TOKEN;\nif (!token || token.trim().length === 0) {\n  throw new Error('DISCORD_TOKEN must be set before creating the WebSocketManager');\n}\nmanager.setToken(token);","typeGuard":"function hasToken(m: WebSocketManager, tokenSet: boolean): tokenSet is true {\n  return tokenSet;\n}","tryCatchPattern":"try {\n  const t = manager.token;\n} catch {\n  manager.setToken(process.env.DISCORD_TOKEN!);\n}","preventionTips":["Fail fast at startup if DISCORD_TOKEN is undefined","Call setToken() immediately after constructing the manager, before connect()"],"tags":["authentication","configuration","token"],"backgroundTag":"missing-env-var","analyzedSha":"a81ed8a306d37fdc746e26a634b6a42164ba2c8c","analyzedAt":"2026-08-30T04:07:22.193Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}