mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 17:30:07 +00:00
more work
This commit is contained in:
@@ -1,12 +1,27 @@
|
||||
import { RequestManager, DiscordBotGatewayData, spawnShards } from "../../mod.ts";
|
||||
import { delay } from "../../deps.ts";
|
||||
import { DiscordBotGatewayData, RequestManager } from "../../mod.ts";
|
||||
import { endpoints } from "../constants/discord.ts";
|
||||
import { ClientOptions } from "../types/options.ts";
|
||||
import { botGatewayData, identifyPayload } from "./client.ts";
|
||||
import { ClientOptions, EventHandlers } from "../types/options.ts";
|
||||
import { botGatewayData } from "./client.ts";
|
||||
|
||||
const botOptions = {
|
||||
token: "",
|
||||
eventHandlers: {}
|
||||
}
|
||||
workers: new Map<number, Worker>(),
|
||||
eventHandlers: {} as EventHandlers,
|
||||
botGatewayData: {} as DiscordBotGatewayData,
|
||||
customShards: [] as number[],
|
||||
shardsPerWorker: 25,
|
||||
identifyPayload: {
|
||||
token: "",
|
||||
compress: true,
|
||||
properties: {
|
||||
$os: "linux",
|
||||
$browser: "Discordeno",
|
||||
$device: "Discordeno",
|
||||
},
|
||||
intents: 0,
|
||||
shard: [0, 0],
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* This function should be used only by bot developers whose bots are in over 25,000 servers.
|
||||
@@ -14,30 +29,77 @@ const botOptions = {
|
||||
*
|
||||
* Advanced Devs: This function will allow you to have an insane amount of customization potential as when you get to large bots you need to be able to optimize every tiny detail to make you bot work the way you need.
|
||||
*/
|
||||
export async function startHugeBot(data: HugeBotOptions) {
|
||||
botOptions.token = `Bot ${data.token}`;
|
||||
if (data.eventHandlers) botOptions.eventHandlers = data.eventHandlers;
|
||||
export async function startBigBrainBot(data: BigBrainBotOptions) {
|
||||
botOptions.identifyPayload.token = `Bot ${data.token}`;
|
||||
if (data.eventHandlers) botOptions.eventHandlers = data.eventHandlers;
|
||||
if (data.shards) botOptions.customShards = data.shards;
|
||||
if (data.compress) botOptions.identifyPayload.compress = data.compress;
|
||||
if (data.shardsPerWorker) botOptions.shardsPerWorker = data.shardsPerWorker;
|
||||
|
||||
// Initial API connection to get info about bots connection
|
||||
botGatewayData = await RequestManager.get(
|
||||
botOptions.botGatewayData = await RequestManager.get(
|
||||
endpoints.GATEWAY_BOT,
|
||||
) as DiscordBotGatewayData;
|
||||
|
||||
identifyPayload.token = data.token;
|
||||
identifyPayload.intents = data.intents.reduce(
|
||||
botOptions.identifyPayload.intents = data.intents.reduce(
|
||||
(bits, next) => (bits |= next),
|
||||
0,
|
||||
);
|
||||
identifyPayload.shard = [0, botGatewayData.shards];
|
||||
botOptions.identifyPayload.shard = [0, botGatewayData.shards];
|
||||
|
||||
spawnShards(botGatewayData, identifyPayload);
|
||||
spawnBigBrainBotShards();
|
||||
}
|
||||
|
||||
export interface HugeBotOptions extends ClientOptions {
|
||||
/** This can be used to distribute your bot across different servers. For example, if you wanted 1 million shards per server you could control it using this. */
|
||||
shards: [number, number];
|
||||
/** This can be used to forward the ws handling to a proxy. */
|
||||
wsURL?: string;
|
||||
/** This can be used to forward the REST handling to a proxy. */
|
||||
restURL?: string;
|
||||
async function spawnBigBrainBotShards(shardID = 0, skipChecks = 0) {
|
||||
if (shardID >= botOptions.botGatewayData.shards) return;
|
||||
|
||||
// 25 shards but shards start at 0 so we use 24
|
||||
const workerID = shardID % 24;
|
||||
const worker = botOptions.workers.get(workerID)
|
||||
|
||||
// High max concurrency allows starting shards faster
|
||||
if (skipChecks) {
|
||||
// If the worker exists we just need to add
|
||||
if (worker) {
|
||||
addShardToWorker(workerID, shardID);
|
||||
} else {
|
||||
createShardWorker(workerID, shardID);
|
||||
}
|
||||
|
||||
spawnBigBrainBotShards(shardID + 1, skipChecks - 1);
|
||||
}
|
||||
|
||||
// Make sure we can create a shard or we are waiting for shards to connect still.
|
||||
if (createNextShard) {
|
||||
// !(shardid % botOptions.botGatewayData.session_start_limit.max_concurrency)
|
||||
createNextShard = false;
|
||||
if (botOptions.botGatewayData.shards >= 25) createShardWorker();
|
||||
// Start the next few shards based on max concurrency
|
||||
spawnBigBrainBotShards(shardID + 1, botOptions.botGatewayData.session_start_limit.max_concurrency);
|
||||
return;
|
||||
}
|
||||
|
||||
await delay(1000);
|
||||
spawnBigBrainBotShards(shardID);
|
||||
}
|
||||
|
||||
export function createShardWorker(workerID: number, shardID: number) {
|
||||
const path = new URL("./shard.ts", import.meta.url).toString();
|
||||
const shard = new Worker(path, { type: "module", deno: true });
|
||||
// Add to worker map
|
||||
botOptions.workers.set(workerID, shard)
|
||||
|
||||
}
|
||||
|
||||
export interface BigBrainBotOptions extends ClientOptions {
|
||||
/** This can be used to distribute your bot across different servers. For example, if you wanted 1 million shards per server you could control it using this. */
|
||||
shards?: [number, number];
|
||||
/** This can be used to forward the ws handling to a proxy. */
|
||||
wsURL?: string;
|
||||
/** This can be used to forward the REST handling to a proxy. */
|
||||
restURL?: string;
|
||||
/** This allows you to control how many shards per worker. For the times where you can optimize with more shards per worker as your bot has less tasks per shard.
|
||||
* @default 25
|
||||
*/
|
||||
shardsPerWorker?: number;
|
||||
}
|
||||
|
||||
@@ -57,6 +57,11 @@ export interface DiscordBotGatewayData {
|
||||
remaining: number;
|
||||
/** Milliseconds left until limit is reset. */
|
||||
reset_after: number;
|
||||
/** The number of identify requests allowed per 5 seconds.
|
||||
* So, if you had a max concurrency of 16, and 16 shards for example, you could start them all up at the same time.
|
||||
* Whereas if you had 32 shards, if you tried to start up shard 0 and 16 at the same time for example, it would not work. You can start shards 0-15 concurrently, then 16-31...
|
||||
* */
|
||||
max_concurrency: number;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user