diff --git a/src/client/workerclient.ts b/src/client/workerclient.ts index 31d8767..c7f7146 100644 --- a/src/client/workerclient.ts +++ b/src/client/workerclient.ts @@ -104,6 +104,10 @@ export class WorkerClient extends BaseClient { workerData = data; } + get workerData() { + return workerData; + } + async start(options: Omit, 'httpConnection' | 'token' | 'connection'> = {}) { const worker_threads = lazyLoadPackage('node:worker_threads'); diff --git a/src/common/it/utils.ts b/src/common/it/utils.ts index 94db57f..e837046 100644 --- a/src/common/it/utils.ts +++ b/src/common/it/utils.ts @@ -11,8 +11,17 @@ import { type ObjectToSnake, } from '..'; import { type APIPartialEmoji, FormattingPatterns } from '../../types'; -import type { Cache } from '../..'; +import type { Cache } from '../../cache'; +/** + * Calculates the shard ID for a guild based on its ID. + * @param guildId The ID of the guild. + * @param shards The number of shards to calculate the ID for. + * @returns The shard ID. + */ +export function calculateShardId(guildId: string, shards?: number) { + return Number((BigInt(guildId) >> 22n) % BigInt(shards ?? 1)); +} /** * Resolves the color to a numeric representation. * @param color The color to resolve. diff --git a/src/structures/extra/BaseGuild.ts b/src/structures/extra/BaseGuild.ts index 7b44d05..159d0b6 100644 --- a/src/structures/extra/BaseGuild.ts +++ b/src/structures/extra/BaseGuild.ts @@ -1,6 +1,8 @@ -import type { ObjectToLower } from '../../common'; +import type { WorkerClient } from '../..'; +import { calculateShardId, type ObjectToLower } from '../../common'; import type { ImageOptions } from '../../common/types/options'; import { type APIPartialGuild, GuildFeature } from '../../types'; +import type { ShardManager } from '../../websocket'; import { DiscordBase } from './DiscordBase'; export interface BaseGuild extends ObjectToLower {} @@ -71,6 +73,35 @@ export class BaseGuild extends DiscordBase { return this.rest.cdn.banners(this.id).get(this.banner, options); } + /** + * Shard ID of the guild. + * @returns Shard ID or -1 if the client is not gateway based. + */ + get shardId() { + if ('gateway' in this.client) { + return (this.client.gateway as ShardManager).calculateShardId(this.id) as never; + } + if ('shards' in this.client) { + return calculateShardId(this.id, (this.client as WorkerClient).workerData.totalShards); + } + return -1; + } + + /** + * Shard of the guild. + * @returns Shard or undefined, if the client is not gateway based always undefined. + */ + get shard() { + if ('gateway' in this.client) { + return (this.client.gateway as ShardManager).get(this.shardId!) as never; + } + + if ('shards' in this.client) { + return (this.client as WorkerClient).shards.get(this.shardId!); + } + return undefined; + } + toString(): string { return this.name; } diff --git a/src/websocket/discord/sharder.ts b/src/websocket/discord/sharder.ts index e6d226e..d0ee6c7 100644 --- a/src/websocket/discord/sharder.ts +++ b/src/websocket/discord/sharder.ts @@ -5,6 +5,7 @@ import { MergeOptions, lazyLoadPackage, type WatcherSendToShard, + calculateShardId, } from '../../common'; import { type GatewayUpdatePresence, @@ -81,7 +82,7 @@ export class ShardManager extends Map { } calculateShardId(guildId: string) { - return Number((BigInt(guildId) >> 22n) % BigInt(this.options.info.shards ?? 1)); + return calculateShardId(guildId, this.totalShards); } spawn(shardId: number) {