feat(Guild): get shard methods (#254)

* feat: guild shard

* fix: worker client

* fix: xd
This commit is contained in:
Marcos Susaña 2024-08-25 01:18:39 -04:00 committed by GitHub
parent a98306fd72
commit 498c66efdb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 3 deletions

View File

@ -104,6 +104,10 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
workerData = data;
}
get workerData() {
return workerData;
}
async start(options: Omit<DeepPartial<StartOptions>, 'httpConnection' | 'token' | 'connection'> = {}) {
const worker_threads = lazyLoadPackage<typeof import('node:worker_threads')>('node:worker_threads');

View File

@ -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.

View File

@ -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<APIPartialGuild> {}
@ -71,6 +73,35 @@ export class BaseGuild extends DiscordBase<APIPartialGuild> {
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;
}

View File

@ -5,6 +5,7 @@ import {
MergeOptions,
lazyLoadPackage,
type WatcherSendToShard,
calculateShardId,
} from '../../common';
import {
type GatewayUpdatePresence,
@ -81,7 +82,7 @@ export class ShardManager extends Map<number, Shard> {
}
calculateShardId(guildId: string) {
return Number((BigInt(guildId) >> 22n) % BigInt(this.options.info.shards ?? 1));
return calculateShardId(guildId, this.totalShards);
}
spawn(shardId: number) {