Carbon

Client

The main class that is used to use Carbon

The main class that is used to use Carbon is the Client class. Everything all connects to this one class, and it is the main instance for your bot.

Creating a Client

src/index.ts
const client = new Client({
    baseUrl: " ... ",
    deploySecret: " ... ",
    clientId: " ... ",
    publicKey: " ... ",
    token: " ... ",
}, [new PingCommand()])

Here we have created a client with the following options:

  • baseUrl: The relative base URL of your app
  • deploySecret: The deploy secret of your bot, used as a password for deploying commands and other sensitive matters
  • clientId: The Discord client ID of your bot
  • publicKey: The Discord public key of your bot
  • token: The Discord token of your bot

And we have also provided it with a list of commands, which in this case is just the PingCommand we created earlier.

Event Queue Options

The client can be configured with an event queue to control how gateway events are processed.

src/index.ts
const client = new Client({
    baseUrl: " ... ",
    deploySecret: " ... ",
    clientId: " ... ",
    publicKey: " ... ",
    token: " ... ",
    eventQueue: {
        maxQueueSize: 10000,
        maxConcurrency: 50,
        listenerTimeout: 30000,
        listenerConcurrency: 10,
        yieldIntervalMs: 0,
        logSlowListeners: true,
        slowListenerThreshold: 1000
    }
}, [new PingCommand()])
  • maxQueueSize: Maximum number of events that can be queued before backpressure is applied
  • maxConcurrency: Maximum number of concurrent event processing operations
  • listenerTimeout: Maximum time in ms a single listener can take before timing out
  • listenerConcurrency: Maximum number of listeners processed at the same time per event
  • yieldIntervalMs: Yield to the event loop after this many ms of listener work
  • logSlowListeners: Whether to log slow listeners
  • slowListenerThreshold: Threshold in ms for logging slow listeners

Guidance:

  • Use lower listenerConcurrency (1-5) for CPU-heavy or sync I/O handlers, higher for mostly async work.
  • Set yieldIntervalMs to 10-50ms for long-running handlers to reduce event loop starvation, or keep 0 for maximum throughput.
Edit on GitHub

Last updated on