Files
discordeno/src/ws/shard_manager.ts
T
TriForMine 484f86638f refactor: resolve async promises, fixed typos, and used inline variable for return (#299)
* Added await in async function, fixed typos and used inline variable for return

* Added await in async function, fixed typos and used inline variable for return

* Revert "Added await in async function, fixed typos and used inline variable for return"

This reverts commit f31caf5d

* Added await in async function, fixed typos and used inline variable for return

* Fixes format

* Fixes format 2

* Fixes format 4475757

* Fixes format 878757854786312378657865785785785785

* Change return to await

* Fixing more issues

* Fixing even more issues

* Fixing even more issues +

* Fixes format
2020-12-30 12:31:11 +04:00

108 lines
2.7 KiB
TypeScript

import { controllers } from "../api/controllers/mod.ts";
import { Guild } from "../api/structures/guild.ts";
import { eventHandlers, IdentifyPayload } from "../bot.ts";
import {
DiscordBotGatewayData,
DiscordPayload,
FetchMembersOptions,
GatewayOpcode,
} from "../types/mod.ts";
import { cache } from "../util/cache.ts";
import { BotStatusRequest, delay } from "../util/utils.ts";
import {
botGatewayStatusRequest,
createShard,
requestGuildMembers,
} from "./mod.ts";
let createNextShard = true;
/** This function is meant to be used on the ready event to alert the library to start the next shard. */
export function allowNextShard(enabled = true) {
createNextShard = enabled;
}
export async function spawnShards(
data: DiscordBotGatewayData,
payload: IdentifyPayload,
shardID: number,
lastShardID: number,
skipChecks?: number,
) {
// All shards on this worker have started! Cancel out.
if (shardID >= lastShardID) return;
if (skipChecks) {
payload.shard = [
shardID,
data.shards > lastShardID ? data.shards : lastShardID,
];
// Start The shard
await createShard(data, payload, false, shardID);
// Spawn next shard
await spawnShards(
data,
payload,
shardID + 1,
lastShardID,
skipChecks - 1,
);
return;
}
// Make sure we can create a shard or we are waiting for shards to connect still.
if (createNextShard) {
createNextShard = false;
// Start the next few shards based on max concurrency
await spawnShards(
data,
payload,
shardID,
lastShardID,
data.session_start_limit.max_concurrency,
);
return;
}
await delay(1000);
await spawnShards(data, payload, shardID, lastShardID, skipChecks);
}
export async function handleDiscordPayload(
data: DiscordPayload,
shardID: number,
) {
eventHandlers.raw?.(data);
await eventHandlers.dispatchRequirements?.(data, shardID);
switch (data.op) {
case GatewayOpcode.HeartbeatACK:
// In case the user wants to listen to heartbeat responses
return eventHandlers.heartbeat?.();
case GatewayOpcode.Dispatch:
if (!data.t) return;
// Run the appropriate controller for this event.
return controllers[data.t]?.(data, shardID);
default:
return;
}
}
export async function requestAllMembers(
guild: Guild,
resolve: Function,
options?: FetchMembersOptions,
) {
const nonce = `${guild.id}-${Date.now()}`;
cache.fetchAllMembersProcessingRequests.set(nonce, resolve);
return requestGuildMembers(guild.id, guild.shardID, nonce, options);
}
export function sendGatewayCommand(type: "EDIT_BOTS_STATUS", payload: object) {
if (type === "EDIT_BOTS_STATUS") {
botGatewayStatusRequest(payload as BotStatusRequest);
}
return;
}