Discord bots2 minutes to read

When does my Discord bot need sharding, and how do I host the shards?

Check Discord’s guild threshold, start a discord.js shard manager on Bothosting, and keep background jobs from running once per shard.

Count Discord servers and check the Gateway recommendation

Discord requires Gateway sharding when a bot is in 2,500 or more guilds. A guild is a Discord server. A bot serving one community with 100,000 members still counts as one guild. Ask Discord’s Get Gateway Bot endpoint for the recommended shard count and the session start limits for your application. Discord Gateway sharding rules.

Each shard handles Gateway events for a subset of guilds. If a bot in ten servers is slow because a command waits on a database query, measure that query first. Dividing the Gateway connections leaves the underlying query in place.

Start the manager as the deployment entry point

For a discord.js bot on one Bothosting deployment, keep the existing client and login in bot.cjs. Add the file below beside it, keep discord.js in your production dependencies, and set BOT_TOKEN in Variables. Set MAIN_FILE to manager.cjs and clear any older RUN_COMMAND that launches the client directly.

manager.cjs
const { ShardingManager } = require('discord.js');
const manager = new ShardingManager('./bot.cjs', {
  token: process.env.BOT_TOKEN,
  totalShards: 'auto',
});

manager.on('shardCreate', shard => console.log('Starting shard', shard.id));
manager.spawn().catch(error => {
  console.error(error.message);
  process.exitCode = 1;
});

The manager starts a copy of the client for each shard. Stop the previous standalone bot during cutover. If you later divide shards across deployments, assign each a distinct set of shard IDs and configure the same total on all of them. Starting an identical automatic manager on every deployment duplicates ownership. discord.js ShardingManager reference.

What happens to timers, caches and memory?

Every client loads your startup code. A timer that sends a daily announcement can now send it once per shard. Give that job one owner, and store shared settings and job completion records somewhere all shards can read. A client’s in-memory guild cache only describes its own connection.

Watch total deployment memory after all shards are ready and again during a busy period. On one deployment the manager and its children share a memory allocation. For a restart, check Discord’s remaining session starts and identify concurrency before retrying repeatedly. Record each ready shard ID and test a guild command on guilds served by different shards. See how to size a deployment from measured usage.