refactor(ws/shard): rewrite the onclose event listener (#491)

This commit is contained in:
ayntee
2021-01-31 21:02:08 +04:00
committed by GitHub
parent 37ea1ef6ba
commit 5fe0453897

View File

@@ -157,55 +157,17 @@ export function createShard(
},
);
switch (code) {
case 4001:
throw new Error(
"[Unknown opcode] Sent an invalid Gateway opcode or an invalid payload for an opcode.",
);
case 4002:
throw new Error("[Decode error] Sent an invalid payload to API.");
case 4004:
throw new Error(
"[Authentication failed] The account token sent with your identify payload is incorrect.",
);
case 4005:
throw new Error(
"[Already authenticated] Sent more than one identify payload.",
);
case 4010:
throw new Error(
"[Invalid shard] Sent an invalid shard when identifying.",
);
case 4011:
throw new Error(
"[Sharding required] The session would have handled too many guilds - you are required to shard your connection in order to connect.",
);
case 4012:
throw new Error(
"[Invalid API version] Sent an invalid version for the gateway.",
);
case 4013:
throw new Error(
"[Invalid intent(s)] Sent an invalid intent for a Gateway Intent.",
);
case 4014:
throw new Error(
"[Disallowed intent(s)] Sent a disallowed intent for a Gateway Intent. You may have tried to specify an intent that you have not enabled or are not whitelisted for.",
);
case 4003:
case 4007:
case 4008:
case 4009:
eventHandlers.debug?.({
type: "wsReconnect",
data: { shardID: basicShard.id, code, reason, wasClean },
});
createShard(data, identifyPayload, false, shardID);
break;
default:
basicShard.needToResume = true;
resumeConnection(botGatewayData, identifyPayload, shardID);
break;
if ([4001, 4002, 4004, 4005, 4010, 4011, 4012, 4013, 4014].includes(code)) {
throw new Error(reason);
} else if ([4000, 4003, 4007, 4008, 4009].includes(code)) {
eventHandlers.debug?.({
type: "wsReconnect",
data: { shardID: basicShard.id, code, reason, wasClean },
});
createShard(data, identifyPayload, false, shardID);
} else {
basicShard.needToResume = true;
resumeConnection(botGatewayData, identifyPayload, shardID);
}
};
}