From b3a8c866b49cd1b6128eb460d71ea384970c2220 Mon Sep 17 00:00:00 2001 From: Fleny Date: Sun, 18 Aug 2024 18:03:38 +0200 Subject: [PATCH] feat(gateway): Expose `makePresence` from `GatewayManager` (#3851) * Expose makePresence from GatewayManager * Pass down the makePresence method on resharding shards * inline makePresence option --------- Co-authored-by: Awesome Stickz --- packages/gateway/src/Shard.ts | 3 +++ packages/gateway/src/manager.ts | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/gateway/src/Shard.ts b/packages/gateway/src/Shard.ts index af7b24e8a..44e218a6d 100644 --- a/packages/gateway/src/Shard.ts +++ b/packages/gateway/src/Shard.ts @@ -58,6 +58,7 @@ export class DiscordenoShard { if (options.requestIdentify) this.requestIdentify = options.requestIdentify if (options.shardIsReady) this.shardIsReady = options.shardIsReady + if (options.makePresence) this.makePresence = options.makePresence this.bucket = new LeakyBucket({ max: this.calculateSafeRequests(), @@ -611,6 +612,8 @@ export interface ShardCreateOptions { requestIdentify?: () => Promise /** The handler to alert the gateway manager that this shard has received a READY event. */ shardIsReady?: () => Promise + /** Function to create the bot status to send on Identify requests */ + makePresence?: () => Promise } export default DiscordenoShard diff --git a/packages/gateway/src/manager.ts b/packages/gateway/src/manager.ts index 18dc2dbff..4b7115cbd 100644 --- a/packages/gateway/src/manager.ts +++ b/packages/gateway/src/manager.ts @@ -11,7 +11,14 @@ import { } from '@discordeno/types' import { Collection, delay, logger } from '@discordeno/utils' import Shard from './Shard.js' -import { type ShardEvents, ShardSocketCloseCodes, type ShardSocketRequest, type StatusUpdate, type UpdateVoiceState } from './types.js' +import { + type BotStatusUpdate, + type ShardEvents, + ShardSocketCloseCodes, + type ShardSocketRequest, + type StatusUpdate, + type UpdateVoiceState, +} from './types.js' export function createGatewayManager(options: CreateGatewayManagerOptions): GatewayManager { const connectionOptions = options.connection ?? { @@ -54,6 +61,7 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate }, }, logger: options.logger ?? logger, + makePresence: options.makePresence ?? (() => Promise.resolve(undefined)), resharding: { enabled: options.resharding?.enabled ?? true, shardsFullPercentage: options.resharding?.shardsFullPercentage ?? 80, @@ -168,6 +176,7 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate gateway.logger.debug(`[Shard] Resolving shard identify request`) gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency)!.identifyRequests.shift()?.() }, + makePresence: gateway.makePresence, }) if (gateway.preferSnakeCase) { @@ -389,6 +398,7 @@ export function createGatewayManager(options: CreateGatewayManagerOptions): Gate gateway.logger.debug(`[Shard] Resolving shard identify request`) gateway.buckets.get(shardId % gateway.connection.sessionStartLimit.maxConcurrency)!.identifyRequests.shift()?.() }, + makePresence: gateway.makePresence, }) if (this.preferSnakeCase) { @@ -648,6 +658,13 @@ export interface CreateGatewayManagerOptions { * @default logger // The logger exported by `@discordeno/utils` */ logger?: Pick + /** + * Make the presence for when the bot connects to the gateway + * + * @remarks + * This function will be called each time a Shard is going to identify + */ + makePresence?: () => Promise /** Options related to resharding. */ resharding?: { /**