Discord.js: rateLimit event not fired for ratelimit received during login

Created on 3 Mar 2020  Â·  13Comments  Â·  Source: discordjs/discord.js

Please describe the problem you are having in as much detail as possible:

Related to the issue I encountered in #3890, it seems that not only does the client not back off when it gets a ratelimited response during login, but it also doesn't fire the documented rateLimit event.

Include a reproducible code sample here, if possible:

// This requires you be ratelimited
const client = new Client();
client.on('debug', (...args) => console.log('debug', ...args));
client.on('rateLimit', (...args) => console.log('rateLimit', ...args));
await client.login(token);
// Observe continual 'debug' logs
// Observe no 'rateLimit' logs

Logs from a slightly more fleshed out bot script:

 MNGR | 20/3/3 22:13:38  INFO  Launched shard 0
 S0 | 20/3/3 22:13:39  INFO  Logging in...
 S0 | 20/3/3 22:13:39  DEBUG  Provided token: [SNIP]
 S0 | 20/3/3 22:13:39  DEBUG  Preparing to connect to the gateway...
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
 S0 | 20/3/3 22:13:39  DEBUG  429 hit on route /gateway/bot
^C

Further details:

  • discord.js version: 12.0.1
  • Node.js version: 12.15.0
  • Operating system: Windows server 2016
  • Priority this issue should have – please be realistic and elaborate if possible: P2 - Less important than the client actually ignoring the ratelimits, but if you have a documented ratelimit event, it really should fire on ratelimits.
unverified REST bug

Most helpful comment

Hello, can this issue take a real priority ? Been few months now and today I can not even start my bot without being ratelimited every single time.

All 13 comments

Hello, can this issue take a real priority ? Been few months now and today I can not even start my bot without being ratelimited every single time.

i have same problem 130k guilds

I can verify this is true, debugged and got a 429 error on /gateway/bot

Ratelimited, no error, nothing.

I'm having this same problem.

I'm also having the same problem. It has been doing that since yesterday.

im having this as well

Same here. It's happening like _every_ day.

I get the same error every day

Since this is solved for me already - I would love to tell what was happening. I was using a many webhooks as logging methods and not all the hooks weren't coordinating the rate limits, when bot was getting more actively used, logs increased and it hit Cloudflare ban. And rebooting resulted in this 429 hit spam. I have now switched to LogDNA for logging and didn't hit that since months.
There can be more reasons for this like your host might be sharing the same IP with other clients (Glitch for example), or you have multiple bots hosted on same VPS.
The reason for this ratelimit's spam is that retry-after given in the headers needs to be multiplied by 1000 ONLY when a Cloudflare ban is hit for it to be accurate (yes, I've tried it, doesn't spam).
Issue: https://github.com/discordjs/discord.js/issues/3560

I have the same issue. I created and instantly deleted ~40 webhooks in the span of 2 minutes and got no error about a ratelimit, but couldn't create another webhook for ~10 minutes.

I'm having the same issue. Even though my bot is only in 2 guilds it would be nice if the rateLimit event fired for the /gateway/bot endpoint so I would have noticed this issue earlier.

Interestingly enough, fetching that endpoint on my machine works:


Details

$ curl 'https://discord.com/api/v7/gateway/bot' -H "Authorization: Bot $DISCORD_TOKEN" -i
HTTP/2 200 
date: Sun, 29 Nov 2020 01:43:48 GMT
content-type: application/json
set-cookie: __cfduid=dbefe132dc37aecffaede588ed6ce3e361606614227; expires=Tue, 29-Dec-20 01:43:47 GMT; path=/; domain=.discord.com; HttpOnly; SameSite=Lax; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-ratelimit-bucket: 41f9cd5d28af77da04563bcb1d67fdfd
x-ratelimit-limit: 2
x-ratelimit-remaining: 1
x-ratelimit-reset: 1606614233
x-ratelimit-reset-after: 5
x-envoy-upstream-service-time: 16
via: 1.1 google
cf-cache-status: DYNAMIC
cf-request-id: 06b344b3770000dfa1fd86a000000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=4nyJZ6f17SCC%2FeQaUQYOD%2BKxBvLjjkm8gvBRuSoE0IP8i7txCQETv%2F0TJLhyT6uRddE%2BMvPGXPIpXTHdJnee%2FVNCeInnPy%2Fo4DsbcA%3D%3D"}],"group":"cf-nel","max_age":604800}
nel: {"report_to":"cf-nel","max_age":604800}
x-content-type-options: nosniff
server: cloudflare
cf-ray: 5f98a3cbfa7edfa1-MEL
{"url": "wss://gateway.discord.gg", "shards": 1, "session_start_limit": {"total": 1000, "remaining": 1000, "reset_after": 0, "max_concurrency": 1}}

However, doing the same in production (on Repl.it) causes the rate limit:


Details

$ curl 'https://discord.com/api/v7/gateway/bot' -H "Authorization: Bot $DISCORD_TOKEN" -i
HTTP/2 429 
date: Sun, 29 Nov 2020 01:43:20 GMT
content-type: text/plain; charset=UTF-8
content-length: 16
retry-after: 53076
x-frame-options: SAMEORIGIN
cache-control: private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0
expires: Thu, 01 Jan 1970 00:00:01 GMT
set-cookie: __cfduid=de48236c1aad145f8d3a68997474fcd451606614200; expires=Tue, 29-Dec-20 01:43:20 GMT; path=/; domain=.discord.com; HttpOnly; SameSite=Lax; Secure
cf-request-id: 06b344498f0000a2d3fc911000000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=LSerPRg2C4CRkqet%2FFFUg5HrKG3CSi%2F%2FJvpwuQDRVU9hSBTdY364zG21MeeZEvKPac4vIc3aRD4czqCjIPPUAC2MITLuA0b%2FddHw1Q%3D%3D"}],"group":"cf-nel","max_age":604800}
nel: {"report_to":"cf-nel","max_age":604800}
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
server: cloudflare
cf-ray: 5f98a3227e27a2d3-ORD
error code: 1015

I made these requests within a minute, so this makes me think that Discord has different rate limits for different IP addresses, or something like that. After reading https://github.com/discordjs/discord.js/issues/3560#issuecomment-692330833, it seems like it's a Cloudflare ban/rate limit.

Discord.js version: 12.4.1
Node.js version: 12.18.3


As a temporary fix, I changed https://github.com/discordjs/discord.js/blob/12.5.1/src/client/websocket/WebSocketManager.js#L134-L140 to

    const {
      url: gatewayURL,
      shards: recommendedShards,
      session_start_limit: sessionStartLimit,
    } = {
      url: 'wss://gateway.discord.gg',
      shards: 1,
      session_start_limit: {total: 1000, remaining: 1000, reset_after: 0, max_concurrency: 1}
    }

That didn't work for me because my bot seems to be getting rate-limited on all the REST endpoints.

The rateLimit event only fires when discord.js delays a request to avoid a 429, not when it actually hits a 429.

How come the library is not considering cloudflare ratelimits?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smchase picture smchase  Â·  3Comments

Dmitry221060 picture Dmitry221060  Â·  3Comments

ghost picture ghost  Â·  3Comments

LLamaFTL picture LLamaFTL  Â·  3Comments

Alipoodle picture Alipoodle  Â·  3Comments