Rate limits are something that every Discord bot developer is likely to encounter.

If a bot sends excessive requests to the API, it will be subject to rate limits and may not work as intended.

This article explains the causes of rate limits, countermeasures, and avoidance measures provided by Discord.

Types of Rate Limits

Discord mainly has the following two types of rate limits:

1. API Rate Limits: There are limits on the number of requests to the API.

  • Limit: 50 requests per second
  • If Exceeded: HTTP 429 response code is returned.
  • Invalid Requests: Up to 10,000 per 10 minutes.

2. Gateway Rate Limits: There are limits on the websocket connection used to send and receive data with Discord.

  • Sharding: By opening multiple websocket connections, the load on one connection can be distributed.
  • Shard ID: Calculated using the formula: shard_id = (guild_id >> 22) % num_shards

Measures to Avoid Rate Limits

  • Using Application Commands: Interaction endpoints are not bound by rate limits, so you can avoid rate limits by migrating to application commands.
  • Sharding: You can avoid gateway rate limits by splitting the bot into multiple instances.
  • Error Handling: By handling errors from the API appropriately, you can prevent CloudFlare Bans.

Increasing Global Rate Limits

For large-scale bots (bots operating on more than 150,000 servers), you can apply to Discord to have your global rate limits increased.

Details on Sharding

For details on how to fine-tune and configure sharding, please refer to the official Discord documentation.

FAQs About Rate Limits

Q. What should I do if I hit a rate limit?

A. Reduce the request frequency or introduce sharding to distribute the load.

Q. What are application commands?

A. These are commands that users can execute by entering / , such as slash commands.

Q. How do I configure sharding?

A. Please refer to the official Discord documentation or the documentation for the library you are using for bot development.

By understanding rate limits and taking appropriate measures, you can develop bots that operate stably.