question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

djs v14 silently fails if initial gateway connection is ratelimited

See original GitHub issue

Which package is this bug report for?

discord.js

Issue description

Discord.js v14 silents fails if initial gateway connection API endpoint (/api/v9/gateway/bot) is ratelimited.

Steps to reproduce:

  1. connect to a server where the ip is currently ratelimited
{"code": 0, "message": "You are being blocked from accessing our API temporarily due to exceeding our rate limits frequently. Please read our docs at https://discord.com/developers/docs/topics/rate-limits to prevent this moving forward."}
  1. run a basic bot

Expected Result: It should throw an error or print something to the console Actual Result: the issue here is that discord.js doesn’t return anything to the user and the program just ends abruptly. Note: On discord.js v13, it doesn’t print anything to the console either (unless you’re logging debug events) but it waits out the timeout. Right now, djs v14 doesn’t debug anything and it doesn’t print anything to the console when its ratelimited (it just closes the program)

GIF EXAMPLE: OnUc3PL7o7

Code sample

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('interactionCreate', async interaction => {
  if (!interaction.isChatInputCommand()) return;

  if (interaction.commandName === 'ping') {
    await interaction.reply('Pong!');
  }
});

client.login(process.env['DISCORD_TOKEN']);

Package version

14.6.0

Node.js version

16.15.0

Operating system

Linux

Priority this issue should have

Low (slightly annoying)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds, GuildMembers, GuildMessages, MessageContent

I have tested this issue on a development release

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
didinelecommented, Oct 16, 2022

It doesn’t “fail”, it waits for the ratelimit and will eventually connect. This is intended default behavior. If you would like for it to throw, you can use the rejectOnRateLimit REST option.

0reactions
hackermondevcommented, Oct 22, 2022

@didinele I did some digging and I found what was causing the issue.

https://github.com/discordjs/discord.js/blob/4f3c13628ecaf8d22174a7a1e227bb7b8278dbb5/packages/rest/src/lib/handlers/SequentialHandler.ts#L463

This line basically is telling the program to wait for the ratelimit to finish. The ref option is set to true and that is what is causing the issue.

If we look back at node documentation (https://nodejs.org/api/timers.html#timerspromisessetintervaldelay-value-options), we can see what the ref option does.

ref <boolean> Set to false to indicate that the scheduled Timeout between iterations should not require the Node.js event loop to remain active. Default: true.

So, what is happening is that the timeouts don’t require the event loop to remain active and then because there’s nothing else going on (no open gateway connection), the program exists and the timeout doesn’t correctly work.

I’ve went ahead and created a pull request to fix this: #8779

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating from v13 to v14 - discord.js Guide
To check your Node version, use node -v in your terminal or command prompt, and if it's not high enough, update it! There...
Read more >
Bot no longer turning on (Discord.js v13) - Stack Overflow
This means your bot is rate-limited. There are a few solutions here: Wait a bit, so that the timeout completes; If you are...
Read more >
discord.js: Versions - Openbase
Rename Error to DiscordjsError internally (#8706) (aec44a0). Typings ... Properly reset the remaining ratelimit of the gateway connection after reset ...
Read more >
Discord.js v14 - Listening and Handling Events - YouTube
Join my Discord for more help: https://discord.gg/Gd5fAEJDQDCode: https://github.com/stuyy/ djs - v14 -tutorialSupport the Channel:Become a ...
Read more >
[NEW] How To Make A Discord Bot || Discord.JS v14 - YouTube
To being our How To Make A Discord Bot series, this video I will be teaching you how to setup a Discord Bot...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found