feat: workerClient.calculateShardId

This commit is contained in:
MARCROCK22 2025-05-07 18:41:46 -04:00
parent b4324f9487
commit 23c9c2a710
2 changed files with 22 additions and 3 deletions

View File

@ -1,7 +1,14 @@
import { type UUID, randomUUID } from 'node:crypto';
import { ApiHandler, Logger } from '..';
import { WorkerAdapter } from '../cache';
import { type DeepPartial, LogLevels, type MakeRequired, type When, lazyLoadPackage } from '../common';
import {
type DeepPartial,
LogLevels,
type MakeRequired,
type When,
calculateShardId,
lazyLoadPackage,
} from '../common';
import { EventHandler } from '../events';
import type { GatewayDispatchPayload, GatewaySendPayload } from '../types';
import { Shard, type ShardManagerOptions, ShardSocketCloseCodes, type WorkerData, properties } from '../websocket';
@ -377,12 +384,18 @@ export class WorkerClient<Ready extends boolean = boolean> extends BaseClient {
return this.onPacket(packet, shardId);
};
}
workerData.totalShards = data.totalShards;
workerData.shards = [...this.shards.keys()];
this.resharding.clear();
}
break;
}
}
calculateShardId(guildId: string) {
return calculateShardId(guildId, this.workerData.totalShards);
}
private generateNonce(): UUID {
const uuid = randomUUID();
if (this.promises.has(uuid)) return this.generateNonce();
@ -553,7 +566,7 @@ export function generateShardInfo(shard: Shard): WorkerShardInfo {
};
}
interface WorkerClientOptions extends BaseClientOptions {
export interface WorkerClientOptions extends BaseClientOptions {
commands?: NonNullable<Client['options']>['commands'];
handlePayload?: ShardManagerOptions['handlePayload'];
gateway?: ClientOptions['gateway'];

View File

@ -288,6 +288,7 @@ export class WorkerManager extends Map<
for (const [id] of this.entries()) {
this.postMessage(id, {
type: 'CONNECT_ALL_SHARDS_RESHARDING',
totalShards: this.options.totalShards,
} satisfies ConnnectAllShardsResharding);
}
this.forEach(w => {
@ -656,7 +657,12 @@ export type ManagerSpawnShardsResharding = CreateManagerMessage<
Pick<ShardOptions, 'info' | 'properties' | 'compress'>
>;
export type DisconnectAllShardsResharding = CreateManagerMessage<'DISCONNECT_ALL_SHARDS_RESHARDING'>;
export type ConnnectAllShardsResharding = CreateManagerMessage<'CONNECT_ALL_SHARDS_RESHARDING'>;
export type ConnnectAllShardsResharding = CreateManagerMessage<
'CONNECT_ALL_SHARDS_RESHARDING',
{
totalShards: number;
}
>;
export type ManagerSendPayload = CreateManagerMessage<
'SEND_PAYLOAD',
GatewaySendPayload & { shardId: number; nonce: string }