discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.2k stars 3.96k forks source link

onReady event never fires after a reconnection #7372

Closed Zamiell closed 2 years ago

Zamiell commented 2 years ago

Which package is this bug report for?

discord.js

Issue description

I have a pretty ordinary Discord.js bot:

function discordInit(discordToken: string) {
  discordClient = new Client({
    intents: [
      Intents.FLAGS.GUILDS,
      Intents.FLAGS.GUILD_MEMBERS,
      Intents.FLAGS.GUILD_MESSAGES,
      Intents.FLAGS.GUILD_VOICE_STATES,
    ],
  });

  discordClient.on("ready", onReady);
  discordClient.on("messageCreate", onMessageCreate);
  discordClient.on("voiceStateUpdate", onVoiceStateUpdate);

  await discordClient.login(discordToken);
}

I've installed all of the packages that are recommended in the documentation:

  "dependencies": {
    "@discordjs/voice": "^0.8.0",
    "bufferutil": "^4.0.6",
    "discord.js": "^13.6.0",
    "erlpack": "github:discord/erlpack",
    "utf-8-validate": "^5.0.8",
    "zlib-sync": "^0.1.7"
  },

Problem:

Sometimes, when I press Ctrl+C in my shell to stop my Discord.js bot, and then I re-run it (to apply new changes), the onReady event never fires, and the bot is stuck in limbo forever. I have found that I can work around this by completely shutting off the program, letting the bot sit completely disconnected, wait 20 seconds or so, and then starting it again. So it seems like rapid-reauthentication is failing, or something along these lines. (But I am not observing any error messages at all. It just sits there, waiting for the onReady event to fire, doing nothing.)

My first thought was that the connection was not being properly torn down, and in the docs it says that invoking the destroy() method properly logs out and so forth. So, I added the following code:

process.on("SIGINT", () => {
  console.log("SIGINT detected; shutting down.");
  client.destroy();
  process.exit();
});

However, this extra code does not make any difference, and I still observe the exact same issue.

Has anyone else experienced this issue? My fear is that when I deploy my bot into production, it will crash, and immediately attempt to reconnect, and then be stuck in limbo forever. I've used DiscordGo in the past and I've never had any issues like this in Go land.

Package version

14.3.2

Node.js version

16.13.1

Operating system

Windows

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

Guilds, GuildMembers, GuildMessages, GuildVoiceStates

I have tested this issue on a development release

No.

almeidx commented 2 years ago

1) Unless you're from the future, the package version you provided doesn't exist 2) The code sample you provided is not valid (using await outside of an async function), and the onReady, onMessageCreate, and onVoiceStateUpdate functions were not provided 3) Even after fixing the issues listed above, could not replicate. (using both the latest stable, v13.6.0, and the latest dev release 14.0.0-dev.1643889786.04502ce) 4) Try listening to the debug event and see if you can figure out your issue from there. discordClient.on('debug', console.log)

ArkanDash commented 2 years ago

I also have this is issue. When i try to debug. It says "Hit a 429 while executing a request".

PalmDevs commented 2 years ago

I also have this is issue. When i try to debug. It says "Hit a 429 while executing a request".

Maybe your login limit has been reached? Discord only allows you to connect to the gateway a thousand times a single day.