mirror of
https://github.com/tiramisulabs/seyfert.git
synced 2025-07-04 14:06:07 +00:00
feat(Guild): get shard methods (#254)
* feat: guild shard * fix: worker client * fix: xd
This commit is contained in:
parent
a98306fd72
commit
498c66efdb
@ -104,6 +104,10 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
|
|||||||
workerData = data;
|
workerData = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get workerData() {
|
||||||
|
return workerData;
|
||||||
|
}
|
||||||
|
|
||||||
async start(options: Omit<DeepPartial<StartOptions>, 'httpConnection' | 'token' | 'connection'> = {}) {
|
async start(options: Omit<DeepPartial<StartOptions>, 'httpConnection' | 'token' | 'connection'> = {}) {
|
||||||
const worker_threads = lazyLoadPackage<typeof import('node:worker_threads')>('node:worker_threads');
|
const worker_threads = lazyLoadPackage<typeof import('node:worker_threads')>('node:worker_threads');
|
||||||
|
|
||||||
|
@ -11,8 +11,17 @@ import {
|
|||||||
type ObjectToSnake,
|
type ObjectToSnake,
|
||||||
} from '..';
|
} from '..';
|
||||||
import { type APIPartialEmoji, FormattingPatterns } from '../../types';
|
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.
|
* Resolves the color to a numeric representation.
|
||||||
* @param color The color to resolve.
|
* @param color The color to resolve.
|
||||||
|
@ -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 { ImageOptions } from '../../common/types/options';
|
||||||
import { type APIPartialGuild, GuildFeature } from '../../types';
|
import { type APIPartialGuild, GuildFeature } from '../../types';
|
||||||
|
import type { ShardManager } from '../../websocket';
|
||||||
import { DiscordBase } from './DiscordBase';
|
import { DiscordBase } from './DiscordBase';
|
||||||
|
|
||||||
export interface BaseGuild extends ObjectToLower<APIPartialGuild> {}
|
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);
|
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 {
|
toString(): string {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
MergeOptions,
|
MergeOptions,
|
||||||
lazyLoadPackage,
|
lazyLoadPackage,
|
||||||
type WatcherSendToShard,
|
type WatcherSendToShard,
|
||||||
|
calculateShardId,
|
||||||
} from '../../common';
|
} from '../../common';
|
||||||
import {
|
import {
|
||||||
type GatewayUpdatePresence,
|
type GatewayUpdatePresence,
|
||||||
@ -81,7 +82,7 @@ export class ShardManager extends Map<number, Shard> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
calculateShardId(guildId: string) {
|
calculateShardId(guildId: string) {
|
||||||
return Number((BigInt(guildId) >> 22n) % BigInt(this.options.info.shards ?? 1));
|
return calculateShardId(guildId, this.totalShards);
|
||||||
}
|
}
|
||||||
|
|
||||||
spawn(shardId: number) {
|
spawn(shardId: number) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user