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 { 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);
}
}
+2 -2
View File
@@ -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;
}
+1 -1
View File
@@ -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);
+1 -2
View File
@@ -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;
+13 -26
View File
@@ -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`, {
ws.botGatewayData = snakeKeysToCamelCase(
await fetch(`https://discord.com/api/gateway/bot`, {
headers: { Authorization: ws.identifyPayload.token },
}).then((res) => res.json())) as DiscordGetGatewayBot;
}).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();
+1 -1
View File
@@ -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. */