Wait for a random amount of time between 1 and 5 seconds before resuming

This commit is contained in:
ITOH
2021-04-15 09:44:33 +02:00
parent 6e404876fd
commit 81a3bc3c26

View File

@@ -4,6 +4,7 @@ import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts";
import { DiscordGatewayPayload } from "../types/gateway/gateway_payload.ts";
import { DiscordHello } from "../types/gateway/hello.ts";
import { DiscordReady } from "../types/gateway/ready.ts";
import { delay } from "../util/utils.ts";
import { decompressWith } from "./deps.ts";
import { identify } from "./identify.ts";
import { resume } from "./resume.ts";
@@ -66,6 +67,9 @@ export async function handleOnMessage(message: any, shardId: number) {
case DiscordGatewayOpcodes.InvalidSession:
ws.log("INVALID_SESSION", { shardId, payload: messageData });
// We need to wait for a random amount of time between 1 and 5
await delay(Math.floor((Math.random() * 4 + 1) * 1000));
// When d is false we need to reidentify
if (!messageData.d) {
await identify(shardId, ws.maxShards);