tokenThe bot token. Pass process directly: An unset variable fails with a hint that points at it
Prefer it still? Press here
Choose the token, events, logging, cache, gateway and connection options for a bot
A bot is configured with one options object passed to run. The same client options also work with create when an application manages the client itself. Only token is required. Every other option has a default that suits a small bot, so add options when the bot needs them
This bot reads its token from the environment, stops cleanly on SIGTERM, prints JSON log lines, keeps the community data that permission checks need in memory and asks Fluxer to skip events it does not use
import { runBot } from "@neontechspace/fluxerly"
await runBot({
token: process.env.FLUXER_BOT_TOKEN,
logging: { format: "json" },
cache: {
guilds: true,
channels: true,
roles: true,
members: { maxEntries: 5_000 },
messages: { maxEntries: 2_000, maxAgeMs: 60 * 60 * 1_000 },
},
gateway: {
ignoredEvents: "auto",
presence: { status: "online", customStatus: { text: "Type !ping" } },
},
rest: { defaultTimeoutMs: 15_000 },
commands: {
prefix: "!",
commands: {
ping: {
description: "Check that the bot replies",
execute: ({ reply }) => reply("Pong!"),
},
},
},
})
import { runBot } from "@neontechspace/fluxerly"
await runBot({
token: process.env.FLUXER_BOT_TOKEN,
logging: { format: "json" },
cache: {
guilds: true,
channels: true,
roles: true,
members: { maxEntries: 5_000 },
messages: { maxEntries: 2_000, maxAgeMs: 60 * 60 * 1_000 },
},
gateway: {
ignoredEvents: "auto",
presence: { status: "online", customStatus: { text: "Type !ping" } },
},
rest: { defaultTimeoutMs: 15_000 },
commands: {
prefix: "!",
commands: {
ping: {
description: "Check that the bot replies",
execute: ({ reply }) => reply("Pong!"),
},
},
},
})
Invalid options throw Configuration from run before anything connects. If the other options are valid, a missing or empty token instead returns an Err containing Configuration and is reported as a failed run. The error identifies the invalid setting. A misspelled option name gets a suggestion when it is close enough to a known name. A failure that stops the running bot, such as a rejected token, is logged once and sets process to 1, so the process ends with a failure status. Set report only when the application handles the returned Result and the exit status itself
tokenThe bot token. Pass process directly: An unset variable fails with a hint that points at it
commandsPrefix commands, guards and cooldowns, as described in commands
onError Receives handler, command and callback failures. Without it, each failure is logged in full at Error
ignoreBots Skips messages written by bots, including the bot's own, before they reach handlers and commands. On by default
processSignals Stops the bot cleanly on Ctrl+C (SIGINT) and SIGTERM. On by default, and set false to leave these signals to the application
signalAn Abort that stops the bot from application code
reportFailure Whether a failed run is logged and sets process. On by default
cacheWhich resources the client keeps in memory for local lookups. Nothing is cached by default. See history and cache
gatewayEvents Fluxer should not send, session flags, the initial presence and what to do with malformed events
connectionStartup deadline, startup attempts and reconnect timing
restHow many HTTP requests run and wait at once, and the default request deadline
transportReplace the HTTP and WebSocket implementations or the User-Agent, for proxies, instrumentation and tests
instanceConnect to a self-hosted Fluxer instance instead of hosted Fluxer
messageFields Build only the message fields the bot reads
uploadsThe byte budget reserved by queued and active attachment uploads
The reference for Bot lists every nested setting with its default and allowed range
Keep the token out of source files and images. Read it from an environment variable and start the bot with node --env-file=.env bot.js during development, or let the process manager set the variable in production, as shown in deploying. The SDK never writes the token to logs or error messages
The SDK removes surrounding spaces and one pair of matching quotes, which .env files often add. It adds the Bot scheme itself, so a token that starts with Bot or Bearer is rejected with Configuration
Fluxer's gateway has no intents setting to request. Each connection receives every event type unless gateway lists it, so registering a handler is enough to receive its event
Setting ignored asks Fluxer to skip supported event types that no registered handler, enabled cache or SDK feature needs, which saves bandwidth and decoding on busy bots. The list is computed when the bot connects, so register handlers before that, as run does
A handler registered after the bot connected receives the skipped types only after that shard starts a new session. A connection that resumes its session keeps the old list.
A raw event subscriber turns automatic filtering off.
Enabled cache categories keep the event types they need, so large caches reduce the savings.
Fluxer still delivers a message that mentions the bot, @here or @everyone.
An explicit array of upper-case dispatch names, such as ["TYPING_, is also accepted. The Gateway reference lists the names that cannot be skipped
Each event handler has a small queue. By default message runs up to eight handlers at a time and other events one at a time, and a full queue drops the oldest waiting event with a Warn record instead of stopping the bot. Pass { handler, concurrency, max instead of a function to change this for one event. The overflow setting is "drop by default, "drop to drop the arriving event instead, or "stop" to close that handler's subscription. See bot lifetime for how the runner treats handlers
Running several handlers at a time can finish events out of order. The partition setting keeps related events in order: With partition: "channel", the messages of one channel run one after another while other channels run beside them, and partition: "guild" does the same for each community, which the API calls a guild. A partitioned handler runs up to eight events at a time unless concurrency says otherwise
A partitioned handler can also hold a key's events until that key is ready, with partition, which takes the key and returns a promise or Effect. See the events guide for the hook and for how a full partitioned queue drops events from the longest backlog
The client prints startup, readiness, lost connections, long rate-limit waits, shutdown and every application error by default. Set logging or per-category levels to change what appears, and logging to forward records to an existing logger. Set the FLUXERLY_ environment variable to turn on Debug records without changing code. The logging guide shows the output and every setting
Pass on to send handler and command failures to an error tracker. Each Failure holds the original error and the IDs involved, and report returns readable text. See reliability for failures that do return a Result
The client keeps no cache unless asked. Enable a category with true, or pass { max to bound it. The max setting also accepts a function that receives each snapshot and returns its age limit, null for no limit or 0 to skip keeping it, so a category can keep only what the bot needs. Caching communities, channels, roles and members lets permission checks such as guards answer without extra requests. Enabled community, role, channel, emoji and sticker caches fill from the community data Fluxer sends when the bot connects. The history and cache guide explains what a cached value can and cannot prove
Each category keeps at most 1,000 entries and 4 MiB by default, and messages 1,000 entries and 8 MiB. The limits apply to the whole client, not per community. When a category is full, its least recently used entry is removed, so lookups start to miss. Size each category for the communities this process serves:
guilds): At least the number of communitiesUnder a supervisor, each child process sizes its caches for its own shards. This helper logs how full each enabled category is
export function logCacheUsage(client) {
for (const [kind, cache] of Object.entries(client.diagnostics().caches)) {
if (!cache.configured)
continue
console.info(`${kind}: ${cache.retainedEntries} of ${cache.maxEntries} entries, ${cache.accountedBytes} of ${cache.maxBytes} bytes`)
}
}
import type { Client } from "@neontechspace/fluxerly"
export function logCacheUsage(client: Client) {
for (const [kind, cache] of Object.entries(client.diagnostics().caches)) {
if (!cache.configured) continue
console.info(
`${kind}: ${cache.retainedEntries} of ${cache.maxEntries} entries, ${cache.accountedBytes} of ${cache.maxBytes} bytes`,
)
}
}
After a lost connection, community, member, role, channel, emoji and sticker entries stay when the session resumes, except cached threads, which are removed. These entries are cleared when the shard has to start a new session.
A shard's first session clears them too, so entries read before connecting do not outlast the first READY.
A lost connection clears the message, user and direct-message entries it could have changed, even when the session resumes.
A process that resumes a session saved before a restart refills the community, role and channel caches through REST once every shard is ready, unless sharding is false.
Byte counts measure the stored JSON, not process memory
The defaults suit most bots: Startup may take 30 seconds with up to 3 attempts per shard. A process with several shards starts them one second apart and adds that second to the deadline for each shard after the first. After that, a lost connection is retried with a growing random delay, capped at 30 seconds unless Fluxer asks for a longer wait, and transient failures never make the bot give up. The connection option changes those values. The rest option sets how many API requests run at once (by default 4 for each shard, up to 64), how many may wait (default 256) and the default deadline for each request (30 seconds). When the queue is full, a new request fails with reason busy and the SDK logs rest at Warn. Raising rest does not raise Fluxer's rate limits, which still apply per route
The transport option is for advanced cases, such as sending traffic through a proxy or recording requests. The testing entry point uses it to connect a real client to an in-memory Fluxer
The Effect run accepts the same options, with Effect handlers and an Effect setup. The Effect API also accepts a connection that decides reconnect delays. See the Effect first bot guide