From f7024a5bf0eea22b0dd5cacb80f624198090cdc2 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Mon, 12 Apr 2021 11:39:34 -0400 Subject: [PATCH] fix(ws): startBot botGatewayData & maxShard (#839) * this is for u tri! * fix more --- src/bot.ts | 45 ++++++++++------------------- src/helpers/misc/get_gateway_bot.ts | 4 +-- src/ws/identify.ts | 2 +- src/ws/resharder.ts | 3 +- src/ws/start_gateway.ts | 41 +++++++++----------------- src/ws/ws.ts | 2 +- 6 files changed, 35 insertions(+), 62 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 132d2d41b..465ca5668 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -2,7 +2,6 @@ import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts"; import { rest } from "./rest/rest.ts"; import { EventHandlers } from "./types/discordeno/eventHandlers.ts"; import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.ts"; -import { GetGatewayBot } from "./types/gateway/get_gateway_bot.ts"; import { baseEndpoints, GATEWAY_VERSION } from "./util/constants.ts"; import { ws } from "./ws/ws.ts"; @@ -13,7 +12,6 @@ export let applicationId = ""; export let eventHandlers: EventHandlers = {}; -export let botGatewayData: GetGatewayBot; export let proxyWSURL = `wss://gateway.discord.gg`; export const identifyPayload = { @@ -34,33 +32,26 @@ export async function startBot(config: BotConfig) { ws.identifyPayload.token = `Bot ${config.token}`; rest.token = `Bot ${config.token}`; ws.identifyPayload.intents = config.intents.reduce( - ( - bits, - next, - ) => (bits |= typeof next === "string" - ? DiscordGatewayIntents[next] - : next), - 0, + (bits, next) => + (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next), + 0 ); // Initial API connection to get info about bots connection - botGatewayData = await getGatewayBot(); + ws.botGatewayData = await getGatewayBot(); + ws.maxShards = ws.maxShards || ws.botGatewayData.shards; // Explicitly append gateway version and encoding - botGatewayData.url += `?v=${GATEWAY_VERSION}&encoding=json`; + ws.botGatewayData.url += `?v=${GATEWAY_VERSION}&encoding=json`; - proxyWSURL = botGatewayData.url; + proxyWSURL = ws.botGatewayData.url; identifyPayload.token = config.token; identifyPayload.intents = config.intents.reduce( - ( - bits, - next, - ) => (bits |= typeof next === "string" - ? DiscordGatewayIntents[next] - : next), - 0, + (bits, next) => + (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next), + 0 ); - identifyPayload.shard = [0, botGatewayData.shards]; + identifyPayload.shard = [0, ws.botGatewayData.shards]; ws.spawnShards(); } @@ -104,20 +95,16 @@ export async function startBigBrainBot(data: BigBrainBotConfig) { } identifyPayload.intents = data.intents.reduce( - ( - bits, - next, - ) => (bits |= typeof next === "string" - ? DiscordGatewayIntents[next] - : next), - 0, + (bits, next) => + (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next), + 0 ); // PROXY DOESNT NEED US SPAWNING SHARDS if (!data.wsPort) { // Initial API connection to get info about bots connection - botGatewayData = await getGatewayBot(); - proxyWSURL = botGatewayData.url; + ws.botGatewayData = await getGatewayBot(); + proxyWSURL = ws.botGatewayData.url; ws.spawnShards(data.firstShardId); } } diff --git a/src/helpers/misc/get_gateway_bot.ts b/src/helpers/misc/get_gateway_bot.ts index 46e0cdc7b..a9b539671 100644 --- a/src/helpers/misc/get_gateway_bot.ts +++ b/src/helpers/misc/get_gateway_bot.ts @@ -1,11 +1,11 @@ import { rest } from "../../rest/rest.ts"; import { DiscordGetGatewayBot, GetGatewayBot } from "../../types/gateway/get_gateway_bot.ts"; import { endpoints } from "../../util/constants.ts"; -import { camelKeysToSnakeCase } from "../../util/utils.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; /** Get the bots Gateway metadata that can help during the operation of large or sharded bots. */ export async function getGatewayBot() { const result = await rest.runMethod("get", endpoints.GATEWAY_BOT); - return camelKeysToSnakeCase(result as DiscordGetGatewayBot) as GetGatewayBot; + return snakeKeysToCamelCase(result as DiscordGetGatewayBot) as GetGatewayBot; } diff --git a/src/ws/identify.ts b/src/ws/identify.ts index 368c60c44..a31076a31 100644 --- a/src/ws/identify.ts +++ b/src/ws/identify.ts @@ -3,7 +3,7 @@ import { ws } from "./ws.ts"; export async function identify(shardId: number, maxShards: number) { ws.log("IDENTIFYING", { shardId, maxShards }); - + console.log("IDENTIFYING", { shardId, maxShards }); // CREATE A SHARD const socket = await ws.createShard(shardId); diff --git a/src/ws/resharder.ts b/src/ws/resharder.ts index 82519c408..04ddf4d2c 100644 --- a/src/ws/resharder.ts +++ b/src/ws/resharder.ts @@ -1,10 +1,9 @@ import { getGatewayBot } from "../helpers/misc/get_gateway_bot.ts"; -import { camelKeysToSnakeCase } from "../util/utils.ts"; import { ws } from "./ws.ts"; /** The handler to automatically reshard when necessary. */ export async function resharder() { - ws.botGatewayData = camelKeysToSnakeCase(await getGatewayBot()); + ws.botGatewayData = await getGatewayBot(); const percentage = ((ws.botGatewayData.shards - ws.maxShards) / ws.maxShards) * 100; diff --git a/src/ws/start_gateway.ts b/src/ws/start_gateway.ts index 09402a557..a67a46470 100644 --- a/src/ws/start_gateway.ts +++ b/src/ws/start_gateway.ts @@ -1,5 +1,6 @@ import { DiscordGatewayIntents } from "../types/gateway/gateway_intents.ts"; -import { DiscordGetGatewayBot } from "../types/gateway/get_gateway_bot.ts"; +import { GetGatewayBot } from "../types/gateway/get_gateway_bot.ts"; +import { snakeKeysToCamelCase } from "../util/utils.ts"; import { StartGatewayOptions } from "./start_gateway_options.ts"; import { ws } from "./ws.ts"; @@ -19,37 +20,23 @@ export async function startGateway(options: StartGatewayOptions) { ws.identifyPayload.compress = options.compress; } if (options.reshard) ws.reshard = options.reshard; - // TODO: Once an hour check if resharding is necessary - // setInterval(ws.resharder, 1000 * 60 * 60); + // Once an hour check if resharding is necessary + setInterval(ws.resharder, 1000 * 60 * 60); ws.identifyPayload.intents = options.intents.reduce( - ( - bits, - next, - ) => (bits |= typeof next === "string" - ? DiscordGatewayIntents[next] - : next), - 0, + (bits, next) => + (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next), + 0 ); - const data = (await fetch(`https://discord.com/api/gateway/bot`, { - headers: { Authorization: ws.identifyPayload.token }, - }).then((res) => res.json())) as DiscordGetGatewayBot; + ws.botGatewayData = snakeKeysToCamelCase( + await fetch(`https://discord.com/api/gateway/bot`, { + headers: { Authorization: ws.identifyPayload.token }, + }).then((res) => res.json()) + ) as GetGatewayBot; - ws.maxShards = options.maxShards || data.shards; - ws.lastShardId = options.lastShardId || data.shards - 1; - - // TODO: ALL THE FOLLOWING CAN BE REPLACED BY THIS 1 LINE - // ws.botGatewayData = snakeToCamel(await getGatewayBot()) - ws.botGatewayData.sessionStartLimit.total = data.session_start_limit.total; - ws.botGatewayData.sessionStartLimit.resetAfter = - data.session_start_limit.reset_after; - ws.botGatewayData.sessionStartLimit.remaining = - data.session_start_limit.remaining; - ws.botGatewayData.sessionStartLimit.maxConcurrency = - data.session_start_limit.max_concurrency; - ws.botGatewayData.shards = data.shards; - ws.botGatewayData.url = data.url; + ws.maxShards = options.maxShards || ws.botGatewayData.shards; + ws.lastShardId = options.lastShardId || ws.botGatewayData.shards - 1; ws.spawnShards(ws.firstShardId); await ws.cleanupLoadingShards(); diff --git a/src/ws/ws.ts b/src/ws/ws.ts index 167c90f4c..e37c8afaa 100644 --- a/src/ws/ws.ts +++ b/src/ws/ws.ts @@ -22,7 +22,7 @@ export const ws = { /** The percentage at which resharding should occur. */ reshardPercentage: 80, /** The maximum shard Id number. Useful for zero-downtime updates or resharding. */ - maxShards: 1, + maxShards: 0, /** The amount of shards to load per cluster */ shardsPerCluster: 25, /** The maximum amount of clusters to use for your bot. */