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 <awesome@stickz.dev>
This commit is contained in:
Fleny
2024-08-18 18:03:38 +02:00
committed by GitHub
co-authored by Awesome Stickz
parent a0510da166
commit b3a8c866b4
2 changed files with 21 additions and 1 deletions
+3
View File
@@ -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<void>
/** The handler to alert the gateway manager that this shard has received a READY event. */
shardIsReady?: () => Promise<void>
/** Function to create the bot status to send on Identify requests */
makePresence?: () => Promise<BotStatusUpdate | undefined>
}
export default DiscordenoShard
+18 -1
View File
@@ -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<typeof logger, 'debug' | 'info' | 'warn' | 'error' | 'fatal'>
/**
* 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<BotStatusUpdate | undefined>
/** Options related to resharding. */
resharding?: {
/**