โ”FAQ

Some of eternal questions answered here.

How many shards should I have per cluster?

Depends on your resources, more shards per clusters results in lower memory but increased CPU.

How do I get each shard data?

You can use .broadcastEval method where available:

const res = await client.cluster.broadcastEval((c) => {
  return {
    clusterId: c.cluster.id,
    shardIds: [...c.cluster.ids.keys()],
    totalGuilds: c.guilds.cache.size,
    totalMembers: c.guilds.cache.map((g) => g.memberCount).reduce((a, b) => a + b, 0),
    ping: c.ws.ping,
    uptime: c.uptime,
    memoryUsage: Object.fromEntries(Object.entries(process.memoryUsage()).map((d) => {
      d[1] = Math.floor(d[1] / 1024 / 1024 * 100) / 100; // in megabytes
      return d;
    })),
    allGuildsData: c.guilds.cache.map((guild) => {
      return {
        id: guild.id,
        name: guild.name,
        ownerId: guild.ownerId,
        memberCount: guild.memberCount,
        channels: guild.channels.cache.map((c) => {
          return { id: c.id, name: c.name } // It's important not to return the entire CLASS
        }),
        }
    }),
    perShardData: [...c.cluster.ids.keys()].map((shardId) => {
      return {
        shardId: shardId,
        ping: c.ws.shards.get(shardId)?.ping,
        uptime: Date.now() - (c.ws.shards.get(shardId)?.connectedAt || 0),
        guilds: c.guilds.cache.filter((x) => x.shardId === shardId).size,
        members: c.guilds.cache.filter((x) => x.shardId === shardId).map((g) => g.memberCount).reduce((a, b) => a + b, 0),
      }
    }),
  }
});

Last updated

Was this helpful?