UNPKG

status-sharding

Version:

Welcome to Status Sharding! This package is designed to provide an efficient and flexible solution for sharding Discord bots, allowing you to scale your bot across multiple processes or workers.

105 lines (104 loc) 4.41 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getInfo = getInfo; exports.getDiscordVersion = getDiscordVersion; exports.detectLibraryFromClient = detectLibraryFromClient; exports.isCoreClient = isCoreClient; exports.isWorkerThread = isWorkerThread; exports.isChildProcess = isChildProcess; const worker_threads_1 = require("worker_threads"); function getInfo() { const clusterMode = process.env.CLUSTER_MANAGER_MODE; if (clusterMode !== 'worker' && clusterMode !== 'process') throw new Error('NO_CLUSTER_MANAGER_MODE | ClusterManager Mode is not defined in the environment variables.'); let data; if (clusterMode === 'process') { const shardList = []; for (const cl of process.env?.SHARD_LIST?.split(',') || []) { shardList.push(Number(cl)); } data = { ShardList: shardList, TotalShards: Number(process.env.TOTAL_SHARDS), ClusterCount: Number(process.env.CLUSTER_COUNT), ClusterId: Number(process.env.CLUSTER), ClusterManagerMode: clusterMode, ClusterQueueMode: process.env.CLUSTER_QUEUE_MODE, FirstShardId: shardList[0] ?? 0, LastShardId: shardList[shardList.length - 1] ?? 0, }; } else { data = { ShardList: worker_threads_1.workerData.SHARD_LIST, TotalShards: worker_threads_1.workerData.TOTAL_SHARDS, ClusterCount: worker_threads_1.workerData.CLUSTER_COUNT, ClusterId: worker_threads_1.workerData.CLUSTER, ClusterManagerMode: clusterMode, ClusterQueueMode: worker_threads_1.workerData.CLUSTER_QUEUE_MODE, FirstShardId: worker_threads_1.workerData.SHARD_LIST[0], LastShardId: worker_threads_1.workerData.SHARD_LIST[worker_threads_1.workerData.SHARD_LIST.length - 1], }; } return data; } async function getDiscordVersion(type) { try { const { version } = await Promise.resolve(`${type}`).then(s => __importStar(require(s))); const [major = 0, minor = 0, patch = 0] = version.split('.').map(Number); return { major, minor, patch, raw: version }; } catch (error) { throw new Error(`Failed to get version of ${type}: ${error.message}`); } } // eslint-disable-next-line @typescript-eslint/no-explicit-any function detectLibraryFromClient(client) { if (!client) return null; if (client.constructor?.name === 'Client' && 'guilds' in client && 'users' in client && 'channels' in client && typeof client.login === 'function' && !('api' in client)) { return 'discord.js'; } if (client.constructor?.name === 'Client' && 'api' in client && 'rest' in client && 'gateway' in client && typeof client.api === 'object') { return '@discordjs/core'; } if (client instanceof Object) { if ('guilds' in client && 'users' in client && !('api' in client)) return 'discord.js'; if ('api' in client || (client.client && 'api' in client.client)) return '@discordjs/core'; } return null; } function isCoreClient(client) { return detectLibraryFromClient(client) === '@discordjs/core'; } function isWorkerThread(process) { return 'threadId' in process; } function isChildProcess(process) { return 'pid' in process; }