Files
discordeno/gateway/createShard.ts
Skillz4Killz a0a1554756 refactor: typings using ReturnType (#2105)
* fix: check new types idea

* fix: type errors

* fix: new style

* fix: more cleanup

* fix: more cleanup

* fix: cleanup audit logs

* fix: cleanup stickers

* fix: cleanup integrations

* fix: more cleanup

* fix: organize into 1 place

* fix: few errors

* fix: some broken import fixes

* fix: quite a lot of fixes across the board

* fix: more fixes for broken imports

* fix: more fixes for broken imports

* fix: handler imports

* fix: all remaining import errors

* fix: more errors needing fixes

* fix: clearing up transformers

* fix: few moer types

* fix: more cleanup of extra types

* fix: fmt

* fix: cleanup discordeno file

* Nuke Base Types (#2102)

* fix: cleanup snake stuff

* convert camelCase to snake_case (#2103)

* fix: add camelize

* fix: finalize remaining errors

* fix: imports in test

Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>
2022-03-14 22:11:22 -04:00

59 lines
2.2 KiB
TypeScript

import { GatewayCloseEventCodes } from "../types/shared.ts";
import { GatewayManager } from "./gateway_manager.ts";
export function createShard(gateway: GatewayManager, shardId: number) {
const socket = new WebSocket(`${gateway.urlWSS}/?v=9&encoding=json`);
socket.onerror = (errorEvent) => {
gateway.debug("GW ERROR", { shardId, error: errorEvent });
};
socket.onmessage = ({ data: message }) => gateway.handleOnMessage(gateway, message, shardId);
socket.onclose = async (event) => {
gateway.debug("GW CLOSED", { shardId, payload: event });
if (event.code === 3064 || event.reason === "Discordeno Testing Finished! Do Not RESUME!") {
return;
}
if (event.code === 3065 || ["Resharded!", "Resuming the shard, closing old shard."].includes(event.reason)) {
return gateway.debug("GW CLOSED_RECONNECT", { shardId, payload: event });
}
switch (event.code) {
// Discordeno tests finished
case 3061:
return;
case 3063: // Resharded
case 3064: // Resuming
case 3065: // Reidentifying
case 3066: // Missing ACK
// Will restart shard manually
return gateway.debug("GW CLOSED_RECONNECT", { shardId, payload: event });
case GatewayCloseEventCodes.UnknownOpcode:
case GatewayCloseEventCodes.DecodeError:
case GatewayCloseEventCodes.AuthenticationFailed:
case GatewayCloseEventCodes.AlreadyAuthenticated:
case GatewayCloseEventCodes.InvalidShard:
case GatewayCloseEventCodes.ShardingRequired:
case GatewayCloseEventCodes.InvalidApiVersion:
case GatewayCloseEventCodes.InvalidIntents:
case GatewayCloseEventCodes.DisallowedIntents:
throw new Error(event.reason || "Discord gave no reason! GG! You broke Discord!");
// THESE ERRORS CAN NO BE RESUMED! THEY MUST RE-IDENTIFY!
case GatewayCloseEventCodes.NotAuthenticated:
case GatewayCloseEventCodes.InvalidSeq:
case GatewayCloseEventCodes.RateLimited:
case GatewayCloseEventCodes.SessionTimedOut:
await gateway.identify(gateway, shardId, gateway.maxShards);
break;
default:
gateway.resume(gateway, shardId);
break;
}
};
return socket;
}