跳到主要内容

Sharding

What is Sharding?

Sharding is the process of breaking up your bot into smaller pieces so that it has a better time handling events. As Discord's Documentation puts it, sharding is "a method of user-controlled guild sharding which allows for splitting events across several gateway connections."

Sharding is used only for large bots and takes up additional resources for managing the new processes. Pycord automatically shards your bot, so the only thing you need to do is use an AutoShardedClient or AutoShardedBot class instead of a plain Client or Bot class. Pretty cool, right?

Just because you can shard your bot doesn't mean you should. While Pycord makes it easy to shard your bot, it isn't necessary to shard unless your bot is large. Sharding a small bot isn't just useless, but harmful, as it uses extra resources and may slow down your bot. Discord themselves will let you know when your bot needs sharding, so you won't need to worry about this until they contact you.

How Do I Shard in Pycord?

Sharding in Pycord is straightforward.

Just replace your discord.Client, discord.Bot, commands.Bot objects with discord.AutoShardedClient, discord.AutoShardedBot, commands.AutoShardedBot respectively.

These new objects have been inherited from the original objects, so all the functions from those other types should be present.

If you want to specify the number of shards your bot uses, just add the shards parameter, along with the number of shards you want.

Why Should I Shard my Bot?

Sharding is very necessary for big bots. While your bot grows, it gets harder for your bot to control how many guilds it is in and what parts of your code should do what. This is exactly what sharding handles, it makes all of this easier for your bot. Remember, though, only shard your bot once it's big enough, and that's at least 1,000 guilds.