import { getGatewayBot } from "./api/handlers/gateway.ts";import { BotConfig, DiscordBotGatewayData, DiscordIdentify, EventHandlers, Intents,} from "./types/mod.ts";import { baseEndpoints, GATEWAY_VERSION } from "./util/constants.ts";import { spawnShards } from "./ws/shard_manager.ts";
export let authorization = "";export let restAuthorization = "";export let botID = "";export let applicationID = "";
export let eventHandlers: EventHandlers = {};
export let botGatewayData: DiscordBotGatewayData;export let proxyWSURL = `wss://gateway.discord.gg`;
export const identifyPayload: DiscordIdentify = { token: "", compress: true, properties: { $os: "linux", $browser: "Discordeno", $device: "Discordeno", }, intents: 0, shard: [0, 0],};
export interface IdentifyPayload { token: string; compress: boolean; properties: { $os: string; $browser: string; $device: string; }; intents: number; shard: [number, number];}
export async function startBot(config: BotConfig) { if (config.eventHandlers) eventHandlers = config.eventHandlers; authorization = `Bot ${config.token}`;
botGatewayData = await getGatewayBot();
botGatewayData.url += `?v=${GATEWAY_VERSION}&encoding=json`;
proxyWSURL = botGatewayData.url; identifyPayload.token = config.token; identifyPayload.intents = config.intents.reduce( (bits, next) => (bits |= typeof next === "string" ? Intents[next] : next), 0, ); identifyPayload.shard = [0, botGatewayData.shards];
await spawnShards(botGatewayData, identifyPayload, 0, botGatewayData.shards);}
export function updateEventHandlers(newEventHandlers: EventHandlers) { eventHandlers = { ...eventHandlers, ...newEventHandlers, };}
export function setBotID(id: string) { if (botID !== id) botID = id;}
export function setApplicationID(id: string) { if (applicationID !== id) applicationID = id;}
export async function startBigBrainBot(data: BigBrainBotConfig) { authorization = `Bot ${data.token}`; identifyPayload.token = `Bot ${data.token}`;
if (data.restAuthorization) restAuthorization = data.restAuthorization; if (data.restURL) baseEndpoints.BASE_URL = data.restURL; if (data.cdnURL) baseEndpoints.CDN_URL = data.cdnURL; if (data.wsURL) proxyWSURL = data.wsURL; if (data.eventHandlers) eventHandlers = data.eventHandlers; if (data.compress) { identifyPayload.compress = data.compress; }
identifyPayload.intents = data.intents.reduce( (bits, next) => (bits |= typeof next === "string" ? Intents[next] : next), 0, );
botGatewayData = await getGatewayBot();
if (!data.wsURL) proxyWSURL = botGatewayData.url; await spawnShards( botGatewayData, identifyPayload, data.firstShardID, data.lastShardID || (botGatewayData.shards >= 25 ? (data.firstShardID + 25) : botGatewayData.shards), );}
export interface BigBrainBotConfig extends BotConfig { firstShardID: number; lastShardID?: number; wsURL?: string; restURL?: string; cdnURL?: string; restAuthorization?: string;}