{"record":{"id":"bb26fe3dfaec27b7","repo":"discordjs/discord.js","slug":"clientnotready","errorCode":"ClientNotReady","errorMessage":"The client needs to be logged in to ${action}.","messagePattern":"The client needs to be logged in to (.+?)\\.","errorType":"error_code","errorClass":"DiscordjsError","httpStatus":null,"severity":"error","filePath":"packages/discord.js/src/client/Client.js","lineNumber":738,"sourceCode":"   * @example\n   * const link = client.generateInvite({\n   *   scopes: [OAuth2Scopes.ApplicationsCommands],\n   * });\n   * console.log(`Generated application invite link: ${link}`);\n   * @example\n   * const link = client.generateInvite({\n   *   permissions: [\n   *     PermissionFlagsBits.SendMessages,\n   *     PermissionFlagsBits.ManageGuild,\n   *     PermissionFlagsBits.MentionEveryone,\n   *   ],\n   *   scopes: [OAuth2Scopes.Bot],\n   * });\n   * console.log(`Generated bot invite link: ${link}`);\n   */\n  generateInvite(options = {}) {\n    if (typeof options !== 'object') throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true);\n    if (!this.application) throw new DiscordjsError(ErrorCodes.ClientNotReady, 'generate an invite link');\n\n    const { scopes } = options;\n    if (scopes === undefined) {\n      throw new DiscordjsTypeError(ErrorCodes.InvalidMissingScopes);\n    }\n\n    if (!Array.isArray(scopes)) {\n      throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'scopes', 'Array of Invite Scopes', true);\n    }\n\n    if (!scopes.some(scope => [OAuth2Scopes.Bot, OAuth2Scopes.ApplicationsCommands].includes(scope))) {\n      throw new DiscordjsTypeError(ErrorCodes.InvalidMissingScopes);\n    }\n\n    if (!scopes.includes(OAuth2Scopes.Bot) && options.permissions) {\n      throw new DiscordjsTypeError(ErrorCodes.InvalidScopesWithPermissions);\n    }\n","sourceCodeStart":720,"sourceCodeEnd":756,"githubUrl":"https://github.com/discordjs/discord.js/blob/a81ed8a306d37fdc746e26a634b6a42164ba2c8c/packages/discord.js/src/client/Client.js#L720-L756","documentation":"This DiscordjsError (code ClientNotReady) is thrown because generating an invite requires the application's client ID, which is only available after the client has logged in and the READY handshake populated client.application. If this.application is falsy, the client is not logged in yet.","triggerScenarios":"Calling client.generateInvite(...) before client.login() resolves, during the ClientReady event lag, or in a separate script/instance that never logged in.","commonSituations":"Constructing the invite link at module top-level instead of inside the ready handler; calling it in another process that shares config but not the live client; a failed login leaving the client half-initialized.","solutions":["Call generateInvite only after login completes, e.g. inside the clientReady/ready event handler","await client.login(token) and chain the invite generation after the promise resolves","If you only need the link, construct it manually with your application's client ID without needing a live client"],"exampleFix":"// before\nconst link = client.generateInvite({ scopes: [OAuth2Scopes.Bot] });\nclient.login(token);\n// after\nclient.once('clientReady', () => {\n  const link = client.generateInvite({ scopes: [OAuth2Scopes.Bot] });\n  console.log(link);\n});\nawait client.login(token);","handlingStrategy":"try-catch","validationCode":"if (!client.isReady() || !client.application) {\n  throw new Error('Cannot generate invite before client is ready');\n}","typeGuard":"const canGenerateInvite = (client) => client.isReady() && client.application !== null;","tryCatchPattern":"try {\n  const link = client.generateInvite({ scopes: [OAuth2Scopes.Bot] });\n} catch (err) {\n  if (err.code === 'ClientNotReady') {\n    // defer or queue the link generation until ready\n    client.once('clientReady', () => console.log(client.generateInvite({ scopes: [OAuth2Scopes.Bot] })));\n  } else throw err;\n}","preventionTips":["Generate invite links only inside the clientReady handler","Never call client APIs at module top level before login","Check client.isReady() before any application-dependent call"],"tags":["discord-js","client-not-ready","lifecycle","login-state"],"backgroundTag":"client-not-ready","analyzedSha":"a81ed8a306d37fdc746e26a634b6a42164ba2c8c","analyzedAt":"2026-08-30T04:07:22.193Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}