Reading — step 1 of 5
Read
~2 min readCommands & Routing
Rate Limits
Every chat platform rate-limits API calls. Bots must respect:
- Global limits: total calls per second.
- Per-route limits: e.g., 5 messages per channel per 5 seconds.
- Per-resource limits: 50 messages per minute per channel.
Discord:
- Returns 429 with
retry_afterheader. x-ratelimit-remaining,x-ratelimit-resetheaders.- Per-bucket: routes share buckets.
Telegram:
- 30 messages per second to different users.
- 1 message per second per chat.
- 20 messages per minute per group.
Implementation:
python
Token bucket variant:
python
Defensive coding:
- Catch 429.
- Sleep retry_after.
- Retry once.
- Log if persists.
python
Spam prevention:
- Bots should not send many messages quickly.
- Even if API allows, users hate spam.
- Self-throttle: 1 message per 1-3 seconds for chatty actions.
Sharding (Discord):
- For very large bots (> 2500 servers), split into shards.
- Each shard handles subset of servers.
- Lower per-shard load.
Modern bot frameworks (discord.py, Bolt) handle rate limits automatically. For our toy: skip; focus on logic.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…