ShardManager
ShardManager from @lunibee/sharding manages multiple Gateway connections (shards) for bots serving more than 2,500 guilds. It supports automatic shard count discovery, per-shard info, and horizontal reconnect logic.
Import
Section titled “Import”import { ShardManager } from "@lunibee/sharding";// or via the main package:import { ShardManager } from "lunibee";import { ShardManager } from "@lunibee/sharding";// or via the main package:import { ShardManager } from "lunibee";Constructor
Section titled “Constructor”new ShardManager(options: ShardManagerOptions)new ShardManager(options )| Option | Type | Description |
|---|---|---|
token |
string |
Discord bot token. |
intents |
GatewayIntentResolvable |
Gateway intents for all shards. |
shardCount |
number | "auto" |
Number of shards. "auto" fetches Discord’s recommendation. |
presence |
GatewayPresence |
Initial presence for all shards. |
autoScale |
boolean |
Automatically rescale shards over time. |
Properties
Section titled “Properties”| Property | Type | Description |
|---|---|---|
shards |
Map<number, Gateway> |
Map of shard ID to Gateway instance. |
Methods
Section titled “Methods”connect()
Section titled “connect()”Starts all shards. Uses shardCount: "auto" to query Discord for the recommended count.
await manager.connect();await manager.connect();destroy()
Section titled “destroy()”Closes all shard connections and clears the shard map.
manager.destroy();manager.destroy();fetchRecommendedShardCount()
Section titled “fetchRecommendedShardCount()”Fetches the recommended shard count from Discord’s /gateway/bot endpoint.
const count = await manager.fetchRecommendedShardCount();console.log(`Recommended shards: ${count}`);const count = await manager.fetchRecommendedShardCount();console.log(`Recommended shards: ${count}`);getShardIdForGuild(guildId)
Section titled “getShardIdForGuild(guildId)”Calculates which shard handles a given guild ID using Discord’s formula:
shardId = (guildId >> 22) % totalShardsconst shardId = manager.getShardIdForGuild("123456789012345678");console.log(`Guild is on shard ${shardId}`);const shardId = manager.getShardIdForGuild("123456789012345678");console.log(`Guild is on shard ${shardId}`);broadcast(payload)
Section titled “broadcast(payload)”Sends a raw Gateway payload to all shards.
info()
Section titled “info()”Returns an array of ShardInfo objects:
const info = manager.info();for (const shard of info) { console.log(`Shard ${shard.id}: ${shard.state} — ping ${shard.ping}ms`);}const info = manager.info();for (const shard of info) { console.log(`Shard ${shard.id}: ${shard.state} — ping ${shard.ping}ms`);}ShardInfo shape:
interface ShardInfo { id: number; state: GatewayState; ping: number; guilds: number; // only available if guild count tracking is enabled} id; state: GatewayState; ping; guilds; // only available if guild count tracking is enabled}Example
Section titled “Example”import { ShardManager, IntentBits } from "lunibee";
const manager = new ShardManager({ token: process.env.DISCORD_TOKEN!, intents: [IntentBits.guilds, IntentBits.guildMessages], shardCount: "auto",});
await manager.connect();console.log(`Running ${manager.shards.size} shards.`);
// Graceful shutdownprocess.on("SIGINT", () => { manager.destroy(); process.exit(0);});import { ShardManager, IntentBits } from "lunibee";
const manager = new ShardManager({ token: process.env.DISCORD_TOKEN, intents: [IntentBits.guilds, IntentBits.guildMessages], shardCount: "auto",});
await manager.connect();console.log(`Running ${manager.shards.size} shards.`);
// Graceful shutdownprocess.on("SIGINT", () => { manager.destroy(); process.exit(0);});[!NOTE] Sharding is only required for bots in 2,500+ guilds. For smaller bots, use
Clientdirectly.
ClusterManager
Section titled “ClusterManager”For very large bots (100k+ guilds), use ClusterManager from @lunibee/sharding to distribute shards across multiple processes:
import { ClusterManager } from "@lunibee/sharding";
const cluster = new ClusterManager({ token: process.env.DISCORD_TOKEN!, script: "./dist/bot.js", // worker script path shardCount: "auto", clusterCount: 4, // number of CPU worker processes});
await cluster.connect();import { ClusterManager } from "@lunibee/sharding";
const cluster = new ClusterManager({ token: process.env.DISCORD_TOKEN, script: "./dist/bot.js", // worker script path shardCount: "auto", clusterCount , // number of CPU worker processes});
await cluster.connect();