From 0afd92865045b1a80abcb4331a4b14f9c0b4cc78 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Mon, 21 Feb 2022 05:17:08 +0000 Subject: [PATCH] fix: resharder bugs from testing gamer --- bot.ts | 3 +++ gateway/resharder.ts | 28 ++++++++++++++++++---------- gateway/spawnShards.ts | 2 +- helpers/misc/getGatewayBot.ts | 11 +---------- transformers/gatewayBot.ts | 15 +++++++++++++++ 5 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 transformers/gatewayBot.ts diff --git a/bot.ts b/bot.ts index 79f8d7bfc..8f38cbece 100644 --- a/bot.ts +++ b/bot.ts @@ -66,6 +66,7 @@ import { transformVoiceRegion } from "./transformers/voiceRegion.ts"; import { transformWidget } from "./transformers/widget.ts"; import { transformStageInstance } from "./transformers/stageInstance.ts"; import { transformSticker } from "./transformers/sticker.ts"; +import { transformGatewayBot } from "./transformers/gatewayBot.ts"; export function createBot(options: CreateBotOptions): Bot { const bot = { @@ -326,6 +327,7 @@ export function createBaseHelpers(options: Partial) { export interface Transformers { snowflake: typeof snowflakeToBigint; + gatewayBot: typeof transformGatewayBot; channel: typeof transformChannel; guild: typeof transformGuild; user: typeof transformUser; @@ -394,6 +396,7 @@ export function createTransformers(options: Partial) { widget: options.widget || transformWidget, stageInstance: options.stageInstance || transformStageInstance, sticker: options.sticker || transformSticker, + gatewayBot: options.gatewayBot || transformGatewayBot }; } diff --git a/gateway/resharder.ts b/gateway/resharder.ts index 4eab11175..319480e5a 100644 --- a/gateway/resharder.ts +++ b/gateway/resharder.ts @@ -1,6 +1,6 @@ +import { transformGatewayBot } from "../transformers/gatewayBot.ts"; import { GetGatewayBot } from "../types/gateway/getGatewayBot.ts"; import { DiscordReady } from "../types/gateway/ready.ts"; -import { Collection } from "../util/collection.ts"; import { createGatewayManager, GatewayManager } from "./gateway_manager.ts"; /** The handler to automatically reshard when necessary. */ @@ -12,15 +12,23 @@ export async function resharder( const gateway = createGatewayManager({ ...oldGateway, - // IGNORE EVENTS FOR NOW - handleDiscordPayload: async function (_, data, shardId) { - if (data.t === "READY") { - const payload = data.d as DiscordReady; - await gateway.resharding.markNewGuildShardId(payload.guilds.map((g) => BigInt(g.id)), shardId); - } - }, }); + for (const key of Object.keys(oldGateway)) { + if (key === "handleDiscordPayload") { + gateway.handleDiscordPayload = async function (_, data, shardId) { + if (data.t === "READY") { + const payload = data.d as DiscordReady; + await gateway.resharding.markNewGuildShardId(payload.guilds.map((g) => BigInt(g.id)), shardId); + } + }; + continue; + } + + // USE ANY CUSTOMIZED HANDLERS FROM OLD GATEWAY + if (typeof key === "function") gateway[key] = oldGateway[key]; + } + // Begin resharding gateway.maxShards = results.shards; // FOR MANUAL SHARD CONTROL, OVERRIDE THIS SHARD ID! @@ -31,7 +39,7 @@ export async function resharder( gateway.sessionStartLimitResetAfter = results.sessionStartLimit.resetAfter; gateway.maxConcurrency = results.sessionStartLimit.maxConcurrency; // If more than 100K servers, begin switching to 16x sharding - if (gateway.maxShards && gateway.useOptimalLargeBotSharding) { + if (gateway.maxShards > 60 && gateway.useOptimalLargeBotSharding) { gateway.debug("[Resharding] Using optimal large bot sharding solution."); gateway.maxShards = Math.ceil( gateway.maxShards / @@ -115,7 +123,7 @@ export async function startReshardingChecks(gateway: GatewayManager) { headers: { Authorization: `Bot ${gateway.token}`, }, - }).then((res) => res.json())) as GetGatewayBot; + }).then((res) => res.json()).then((res) => transformGatewayBot(res))) as GetGatewayBot; const percentage = ((results.shards - gateway.maxShards) / gateway.maxShards) * 100; // Less than necessary% being used so do nothing diff --git a/gateway/spawnShards.ts b/gateway/spawnShards.ts index a5d2225e5..1b0ea07a7 100644 --- a/gateway/spawnShards.ts +++ b/gateway/spawnShards.ts @@ -15,7 +15,7 @@ export function prepareBuckets(gateway: GatewayManager, firstShardId: number, la // ORGANIZE ALL SHARDS INTO THEIR OWN BUCKETS for (let i = firstShardId; i < lastShardId; i++) { - gateway.debug(`1. Running for loop in spawnShards function.`); + gateway.debug(`1. Running for loop in spawnShards function for shardId ${i}.`); if (i >= gateway.maxShards) { continue; } diff --git a/helpers/misc/getGatewayBot.ts b/helpers/misc/getGatewayBot.ts index 2d0f8d451..f0362ae6e 100644 --- a/helpers/misc/getGatewayBot.ts +++ b/helpers/misc/getGatewayBot.ts @@ -5,14 +5,5 @@ import type { Bot } from "../../bot.ts"; export async function getGatewayBot(bot: Bot): Promise { const result = await bot.rest.runMethod(bot.rest, "get", bot.constants.endpoints.GATEWAY_BOT); - return { - url: result.url, - shards: result.shards, - sessionStartLimit: { - total: result.session_start_limit.total, - remaining: result.session_start_limit.remaining, - resetAfter: result.session_start_limit.reset_after, - maxConcurrency: result.session_start_limit.max_concurrency, - }, - }; + return bot.transformers.gatewayBot(result); } diff --git a/transformers/gatewayBot.ts b/transformers/gatewayBot.ts new file mode 100644 index 000000000..483060923 --- /dev/null +++ b/transformers/gatewayBot.ts @@ -0,0 +1,15 @@ +import { GetGatewayBot } from "../types/gateway/getGatewayBot.ts"; +import { SnakeCasedPropertiesDeep } from "../types/util.ts"; + +export function transformGatewayBot(payload: SnakeCasedPropertiesDeep): GetGatewayBot { + return { + url: payload.url, + shards: payload.shards, + sessionStartLimit: { + total: payload.session_start_limit.total, + remaining: payload.session_start_limit.remaining, + resetAfter: payload.session_start_limit.reset_after, + maxConcurrency: payload.session_start_limit.max_concurrency, + }, + }; +}