djs v14 silently fails if initial gateway connection is ratelimited
See original GitHub issueWhich 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:
- 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."}
- 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:
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:
- Created a year ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.@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 totrue
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.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