fix(ws): startBot botGatewayData & maxShard (#839)

* this is for u tri!

* fix more
This commit is contained in:
Skillz4Killz
2021-04-12 11:39:34 -04:00
committed by GitHub
parent f488a41493
commit f7024a5bf0
6 changed files with 35 additions and 62 deletions
+16 -29
View File
@@ -2,7 +2,6 @@ import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts";
import { rest } from "./rest/rest.ts"; import { rest } from "./rest/rest.ts";
import { EventHandlers } from "./types/discordeno/eventHandlers.ts"; import { EventHandlers } from "./types/discordeno/eventHandlers.ts";
import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.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 { baseEndpoints, GATEWAY_VERSION } from "./util/constants.ts";
import { ws } from "./ws/ws.ts"; import { ws } from "./ws/ws.ts";
@@ -13,7 +12,6 @@ export let applicationId = "";
export let eventHandlers: EventHandlers = {}; export let eventHandlers: EventHandlers = {};
export let botGatewayData: GetGatewayBot;
export let proxyWSURL = `wss://gateway.discord.gg`; export let proxyWSURL = `wss://gateway.discord.gg`;
export const identifyPayload = { export const identifyPayload = {
@@ -34,33 +32,26 @@ export async function startBot(config: BotConfig) {
ws.identifyPayload.token = `Bot ${config.token}`; ws.identifyPayload.token = `Bot ${config.token}`;
rest.token = `Bot ${config.token}`; rest.token = `Bot ${config.token}`;
ws.identifyPayload.intents = config.intents.reduce( ws.identifyPayload.intents = config.intents.reduce(
( (bits, next) =>
bits, (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next),
next, 0
) => (bits |= typeof next === "string"
? DiscordGatewayIntents[next]
: next),
0,
); );
// Initial API connection to get info about bots connection // 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 // 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.token = config.token;
identifyPayload.intents = config.intents.reduce( identifyPayload.intents = config.intents.reduce(
( (bits, next) =>
bits, (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next),
next, 0
) => (bits |= typeof next === "string"
? DiscordGatewayIntents[next]
: next),
0,
); );
identifyPayload.shard = [0, botGatewayData.shards]; identifyPayload.shard = [0, ws.botGatewayData.shards];
ws.spawnShards(); ws.spawnShards();
} }
@@ -104,20 +95,16 @@ export async function startBigBrainBot(data: BigBrainBotConfig) {
} }
identifyPayload.intents = data.intents.reduce( identifyPayload.intents = data.intents.reduce(
( (bits, next) =>
bits, (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next),
next, 0
) => (bits |= typeof next === "string"
? DiscordGatewayIntents[next]
: next),
0,
); );
// PROXY DOESNT NEED US SPAWNING SHARDS // PROXY DOESNT NEED US SPAWNING SHARDS
if (!data.wsPort) { if (!data.wsPort) {
// Initial API connection to get info about bots connection // Initial API connection to get info about bots connection
botGatewayData = await getGatewayBot(); ws.botGatewayData = await getGatewayBot();
proxyWSURL = botGatewayData.url; proxyWSURL = ws.botGatewayData.url;
ws.spawnShards(data.firstShardId); ws.spawnShards(data.firstShardId);
} }
} }
+2 -2
View File
@@ -1,11 +1,11 @@
import { rest } from "../../rest/rest.ts"; import { rest } from "../../rest/rest.ts";
import { DiscordGetGatewayBot, GetGatewayBot } from "../../types/gateway/get_gateway_bot.ts"; import { DiscordGetGatewayBot, GetGatewayBot } from "../../types/gateway/get_gateway_bot.ts";
import { endpoints } from "../../util/constants.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. */ /** Get the bots Gateway metadata that can help during the operation of large or sharded bots. */
export async function getGatewayBot() { export async function getGatewayBot() {
const result = await rest.runMethod("get", endpoints.GATEWAY_BOT); const result = await rest.runMethod("get", endpoints.GATEWAY_BOT);
return camelKeysToSnakeCase(result as DiscordGetGatewayBot) as GetGatewayBot; return snakeKeysToCamelCase(result as DiscordGetGatewayBot) as GetGatewayBot;
} }
+1 -1
View File
@@ -3,7 +3,7 @@ import { ws } from "./ws.ts";
export async function identify(shardId: number, maxShards: number) { export async function identify(shardId: number, maxShards: number) {
ws.log("IDENTIFYING", { shardId, maxShards }); ws.log("IDENTIFYING", { shardId, maxShards });
console.log("IDENTIFYING", { shardId, maxShards });
// CREATE A SHARD // CREATE A SHARD
const socket = await ws.createShard(shardId); const socket = await ws.createShard(shardId);
+1 -2
View File
@@ -1,10 +1,9 @@
import { getGatewayBot } from "../helpers/misc/get_gateway_bot.ts"; import { getGatewayBot } from "../helpers/misc/get_gateway_bot.ts";
import { camelKeysToSnakeCase } from "../util/utils.ts";
import { ws } from "./ws.ts"; import { ws } from "./ws.ts";
/** The handler to automatically reshard when necessary. */ /** The handler to automatically reshard when necessary. */
export async function resharder() { export async function resharder() {
ws.botGatewayData = camelKeysToSnakeCase(await getGatewayBot()); ws.botGatewayData = await getGatewayBot();
const percentage = const percentage =
((ws.botGatewayData.shards - ws.maxShards) / ws.maxShards) * 100; ((ws.botGatewayData.shards - ws.maxShards) / ws.maxShards) * 100;
+14 -27
View File
@@ -1,5 +1,6 @@
import { DiscordGatewayIntents } from "../types/gateway/gateway_intents.ts"; 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 { StartGatewayOptions } from "./start_gateway_options.ts";
import { ws } from "./ws.ts"; import { ws } from "./ws.ts";
@@ -19,37 +20,23 @@ export async function startGateway(options: StartGatewayOptions) {
ws.identifyPayload.compress = options.compress; ws.identifyPayload.compress = options.compress;
} }
if (options.reshard) ws.reshard = options.reshard; if (options.reshard) ws.reshard = options.reshard;
// TODO: Once an hour check if resharding is necessary // Once an hour check if resharding is necessary
// setInterval(ws.resharder, 1000 * 60 * 60); setInterval(ws.resharder, 1000 * 60 * 60);
ws.identifyPayload.intents = options.intents.reduce( ws.identifyPayload.intents = options.intents.reduce(
( (bits, next) =>
bits, (bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next),
next, 0
) => (bits |= typeof next === "string"
? DiscordGatewayIntents[next]
: next),
0,
); );
const data = (await fetch(`https://discord.com/api/gateway/bot`, { ws.botGatewayData = snakeKeysToCamelCase(
headers: { Authorization: ws.identifyPayload.token }, await fetch(`https://discord.com/api/gateway/bot`, {
}).then((res) => res.json())) as DiscordGetGatewayBot; headers: { Authorization: ws.identifyPayload.token },
}).then((res) => res.json())
) as GetGatewayBot;
ws.maxShards = options.maxShards || data.shards; ws.maxShards = options.maxShards || ws.botGatewayData.shards;
ws.lastShardId = options.lastShardId || data.shards - 1; ws.lastShardId = options.lastShardId || ws.botGatewayData.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.spawnShards(ws.firstShardId); ws.spawnShards(ws.firstShardId);
await ws.cleanupLoadingShards(); await ws.cleanupLoadingShards();
+1 -1
View File
@@ -22,7 +22,7 @@ export const ws = {
/** The percentage at which resharding should occur. */ /** The percentage at which resharding should occur. */
reshardPercentage: 80, reshardPercentage: 80,
/** The maximum shard Id number. Useful for zero-downtime updates or resharding. */ /** The maximum shard Id number. Useful for zero-downtime updates or resharding. */
maxShards: 1, maxShards: 0,
/** The amount of shards to load per cluster */ /** The amount of shards to load per cluster */
shardsPerCluster: 25, shardsPerCluster: 25,
/** The maximum amount of clusters to use for your bot. */ /** The maximum amount of clusters to use for your bot. */