This commit is contained in:
ITOH
2021-04-08 18:51:50 +02:00
parent dfc0cc4151
commit ee4be53a45
7 changed files with 13 additions and 14 deletions
+1 -3
View File
@@ -13,7 +13,6 @@ export let eventHandlers: EventHandlers = {};
export let botGatewayData: DiscordGetGatewayBot; export let botGatewayData: DiscordGetGatewayBot;
export let proxyWSURL = `wss://gateway.discord.gg`; export let proxyWSURL = `wss://gateway.discord.gg`;
export let lastShardId = 0;
export const identifyPayload = { export const identifyPayload = {
token: "", token: "",
@@ -58,8 +57,7 @@ export async function startBot(config: BotConfig) {
: next), : next),
0, 0,
); );
lastShardId = botGatewayData.shards; identifyPayload.shard = [0, botGatewayData.shards];
identifyPayload.shard = [0, lastShardId];
ws.spawnShards(); ws.spawnShards();
} }
+4 -9
View File
@@ -1,15 +1,10 @@
import { import { eventHandlers, setApplicationId, setBotId } from "../../bot.ts";
eventHandlers,
lastShardId,
setApplicationId,
setBotId,
} from "../../bot.ts";
import { cache, cacheHandlers } from "../../cache.ts"; import { cache, cacheHandlers } from "../../cache.ts";
import { initialMemberLoadQueue } from "../../structures/guild.ts"; import { initialMemberLoadQueue } from "../../structures/guild.ts";
import { structures } from "../../structures/mod.ts"; import { structures } from "../../structures/mod.ts";
import { delay } from "../../util/utils.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordReady } from "../../types/gateway/ready.ts"; import { DiscordReady } from "../../types/gateway/ready.ts";
import { delay, snakeKeysToCamelCase } from "../../util/utils.ts";
import { ws } from "../../ws/ws.ts"; import { ws } from "../../ws/ws.ts";
export async function handleReady( export async function handleReady(
@@ -73,7 +68,7 @@ async function loaded(shardId: number) {
shard.ready = true; shard.ready = true;
// If it is the last shard we can go full ready // If it is the last shard we can go full ready
if (shardId === lastShardId - 1) { if (shardId === ws.lastShardId - 1) {
// Still some shards are loading so wait another 2 seconds for them // Still some shards are loading so wait another 2 seconds for them
if (ws.shards.some((shard) => !shard.ready)) { if (ws.shards.some((shard) => !shard.ready)) {
setTimeout(() => loaded(shardId), 2000); setTimeout(() => loaded(shardId), 2000);
@@ -86,7 +81,7 @@ async function loaded(shardId: number) {
await Promise.allSettled( await Promise.allSettled(
members.map(async (member) => { members.map(async (member) => {
const memberStruct = await structures.createMemberStruct( const memberStruct = await structures.createMemberStruct(
member, snakeKeysToCamelCase(member),
guildId, guildId,
); );
+2 -1
View File
@@ -1,3 +1,4 @@
import { DiscordGatewayPayload } from "../types/gateway/gateway_payload.ts";
import { DiscordenoShard } from "./ws.ts"; import { DiscordenoShard } from "./ws.ts";
/** The handler for logging different actions happening inside the ws. User can override and put custom handling per event. */ /** The handler for logging different actions happening inside the ws. User can override and put custom handling per event. */
@@ -35,7 +36,7 @@ export function log(
): unknown; ): unknown;
export function log( export function log(
type: "INVALID_SESSION", type: "INVALID_SESSION",
data: { shardId: number; payload: DiscordPayload }, data: { shardId: number; payload: DiscordGatewayPayload },
): unknown; ): unknown;
export function log(type: "RAW", data: Record<string, unknown>): unknown; export function log(type: "RAW", data: Record<string, unknown>): unknown;
export function log(type: "RECONNECT", data: { shardId: number }): unknown; export function log(type: "RECONNECT", data: { shardId: number }): unknown;
+2 -1
View File
@@ -1,8 +1,9 @@
import { DiscordGatewayPayload } from "../types/gateway/gateway_payload.ts";
import { ws } from "./ws.ts"; import { ws } from "./ws.ts";
/** Handler for processing all dispatch payloads that should be sent/forwarded to another server/vps/process. */ /** Handler for processing all dispatch payloads that should be sent/forwarded to another server/vps/process. */
export async function handleDiscordPayload( export async function handleDiscordPayload(
data: DiscordPayload, data: DiscordGatewayPayload,
shardId: number, shardId: number,
) { ) {
await fetch(ws.url, { await fetch(ws.url, {
+1
View File
@@ -15,6 +15,7 @@ export async function identify(shardId: number, maxShards: number) {
sessionId: "", sessionId: "",
previousSequenceNumber: 0, previousSequenceNumber: 0,
resuming: false, resuming: false,
ready: false,
unavailableGuildIds: new Set(), unavailableGuildIds: new Set(),
heartbeat: { heartbeat: {
lastSentAt: 0, lastSentAt: 0,
+1
View File
@@ -28,6 +28,7 @@ export async function resume(shardId: number) {
sessionId, sessionId,
previousSequenceNumber, previousSequenceNumber,
resuming: false, resuming: false,
ready: false,
unavailableGuildIds: new Set(), unavailableGuildIds: new Set(),
heartbeat: { heartbeat: {
lastSentAt: 0, lastSentAt: 0,
+2
View File
@@ -116,6 +116,8 @@ export interface DiscordenoShard {
previousSequenceNumber: number | null; previousSequenceNumber: number | null;
/** Whether the shard is currently resuming. */ /** Whether the shard is currently resuming. */
resuming: boolean; resuming: boolean;
/** Whether the shard has received the ready event */
ready: boolean;
/** The list of guild ids that are currently unavailable due to an outage. */ /** The list of guild ids that are currently unavailable due to an outage. */
unavailableGuildIds: Set<string>; unavailableGuildIds: Set<string>;
heartbeat: { heartbeat: {