Skip to main content

GatewayManager

Documentation


Documentation / @discordeno/gateway / GatewayManager

Interface: GatewayManager

Defined in: manager.ts:713

Extends​

Properties​

buckets​

buckets: Map<number, { leakyBucket: LeakyBucket; workers: object[]; }>

Defined in: manager.ts:715

The max concurrency buckets. Those will be created when the spawnShards (which calls prepareBuckets under the hood) function gets called.


cache​

cache: object

Defined in: manager.ts:888

This managers cache related settings.

requestMembers​

requestMembers: object

requestMembers.enabled​

enabled: boolean

Whether or not request member requests should be cached.

Default​
false
requestMembers.pending​

pending: Collection<string, RequestMemberRequest>

The pending requests.

Overrides​

CreateGatewayManagerOptions.cache


calculateShardId​

calculateShardId: (guildId, totalShards?) => number

Defined in: manager.ts:784

Calculates the number of shards based on the guild id and total shards.

Parameters​

guildId​

BigString

totalShards?​

number

Returns​

number


calculateTotalShards​

calculateTotalShards: () => number

Defined in: manager.ts:759

Determine max number of shards to use based upon the max concurrency.

Returns​

number


calculateWorkerId​

calculateWorkerId: (shardId) => number

Defined in: manager.ts:761

Determine the id of the worker which is handling a shard.

Parameters​

shardId​

number

Returns​

number


compress​

compress: boolean

Defined in: manager.ts:622

Whether incoming payloads are compressed using zlib.

Default​

false

Inherited from​

CreateGatewayManagerOptions.compress


connection​

connection: object

Defined in: manager.ts:617

Important data which is used by the manager to connect shards to the gateway.

sessionStartLimit​

sessionStartLimit: object

sessionStartLimit.maxConcurrency​

maxConcurrency: number

sessionStartLimit.remaining​

remaining: number

sessionStartLimit.resetAfter​

resetAfter: number

sessionStartLimit.total​

total: number

shards​

shards: number

url​

url: string

Inherited from​

CreateGatewayManagerOptions.connection


editBotStatus​

editBotStatus: (data) => Promise<void>

Defined in: manager.ts:811

Edits the bot status in all shards that this gateway manages.

Parameters​

data​

DiscordUpdatePresence

The status data to set the bots status to.

Returns​

Promise<void>

nothing


editShardStatus​

editShardStatus: (shardId, data) => Promise<void>

Defined in: manager.ts:819

Edits the bot's status on one shard.

Parameters​

shardId​

number

The shard id to edit the status for.

data​

DiscordUpdatePresence

The status data to set the bots status to.

Returns​

Promise<void>

nothing


events​

events: ShardEvents

Defined in: manager.ts:661

The events handlers

Inherited from​

CreateGatewayManagerOptions.events


firstShardId​

firstShardId: number

Defined in: manager.ts:577

Id of the first Shard which should get controlled by this manager.

Default​

0

Inherited from​

CreateGatewayManagerOptions.firstShardId


identify​

identify: (shardId) => Promise<void>

Defined in: manager.ts:778

Tell the manager to identify a Shard. If this Shard is not already managed this will also add the Shard to the manager.

Parameters​

shardId​

number

Returns​

Promise<void>


intents​

intents: number

Defined in: manager.ts:629

The calculated intent value of the events which the shard should receive.

Default​

0

Inherited from​

CreateGatewayManagerOptions.intents


joinVoiceChannel​

joinVoiceChannel: (guildId, channelId, options?) => Promise<void>

Defined in: manager.ts:800

Connects the bot user to a voice or stage channel.

This function sends the Update Voice State gateway command over the gateway behind the scenes.

Parameters​

guildId​

BigString

The ID of the guild the voice channel to leave is in.

channelId​

BigString

The ID of the channel you want to join.

options?​

AtLeastOne<Omit<UpdateVoiceState, "guildId" | "channelId">>

Returns​

Promise<void>

Remarks​

Requires the CONNECT permission.

Fires a Voice State Update gateway event.

See​

https://docs.discord.com/developers/events/gateway-events#update-voice-state


kill​

kill: (shardId) => Promise<void>

Defined in: manager.ts:780

Kill a shard. Close a shards connection to Discord's gateway (if any) and remove it from the manager.

Parameters​

shardId​

number

Returns​

Promise<void>


lastShardId​

lastShardId: number

Defined in: manager.ts:582

Id of the last Shard which should get controlled by this manager.

Default​

0

Inherited from​

CreateGatewayManagerOptions.lastShardId


leaveVoiceChannel​

leaveVoiceChannel: (guildId) => Promise<void>

Defined in: manager.ts:866

Leaves the voice channel the bot user is currently in.

This function sends the Update Voice State gateway command over the gateway behind the scenes.

Parameters​

guildId​

BigString

The ID of the guild the voice channel to leave is in.

Returns​

Promise<void>

Remarks​

Fires a Voice State Update gateway event.

See​

https://docs.discord.com/developers/events/gateway-events#update-voice-state


logger​

logger: Pick<typeof logger, "debug" | "info" | "warn" | "error" | "fatal">

Defined in: manager.ts:726

The logger for the gateway manager.

Overrides​

CreateGatewayManagerOptions.logger


makePresence​

makePresence: () => Promise<DiscordUpdatePresence | undefined>

Defined in: manager.ts:683

Make the presence for when the bot connects to the gateway

Returns​

Promise<DiscordUpdatePresence | undefined>

Remarks​

This function will be called each time a Shard is going to identify

Inherited from​

CreateGatewayManagerOptions.makePresence


prepareBuckets​

prepareBuckets: () => void

Defined in: manager.ts:763

Prepares all the buckets that are available for identifying the shards.

Returns​

void


properties​

properties: object

Defined in: manager.ts:631

Identify properties to use

browser​

browser: string

The "browser" where this shard is running on.

Default​
"Discordeno"

device​

device: string

The device on which the shard is running.

Default​
"Discordeno"

os​

os: string

Operating system the shard runs on.

Default​
"darwin" | "linux" | "windows"

Inherited from​

CreateGatewayManagerOptions.properties


requestChannelInfo​

requestChannelInfo: (guildId, options) => Promise<void>

Defined in: manager.ts:853

Requests ephemeral channel data for channels in a guild

Parameters​

guildId​

BigString

The ID of the guild to get the ephemeral channel data for.

options​

Omit<RequestChannelInfo, "guildId">

The parameters for the fetching of the ephemeral channel data.

Returns​

Promise<void>

  • This method returns nothing. The server will send a CHANNEL_INFO event

See​

https://docs.discord.com/developers/events/gateway-events#request-channel-info


requestIdentify​

requestIdentify: (shardId) => Promise<void>

Defined in: manager.ts:782

This function makes sure that the bucket is allowed to make the next identify request.

Parameters​

shardId​

number

Returns​

Promise<void>


requestMembers​

requestMembers: (guildId, options?) => Promise<object[]>

Defined in: manager.ts:843

Fetches the list of members for a guild over the gateway. If gateway.cache.requestMembers.enabled is not set, this function will return an empty array and you'll have to handle the GUILD_MEMBERS_CHUNK events yourself.

Parameters​

guildId​

BigString

The ID of the guild to get the list of members for.

options?​

Omit<RequestGuildMembers, "guildId">

The parameters for the fetching of the members.

Returns​

Promise<object[]>

Remarks​

If requesting the entire member list:

  • Requires the GUILD_MEMBERS intent.

If requesting presences (presences set to true):

  • Requires the GUILD_PRESENCES intent.

If requesting a prefix (query non-undefined):

  • Returns a maximum of 100 members.

If requesting a users by ID (userIds non-undefined):

  • Returns a maximum of 100 members.

Fires a Guild Members Chunk gateway event for every 1000 members fetched.

See​

https://docs.discord.com/developers/events/gateway-events#request-guild-members


requestSoundboardSounds​

requestSoundboardSounds: (guildIds) => Promise<void>

Defined in: manager.ts:886

Used to request soundboard sounds for a list of guilds.

This function sends multiple (see remarks) Request Soundboard Sounds gateway command over the gateway behind the scenes.

Parameters​

guildIds​

BigString[]

The guilds to get the sounds from

Returns​

Promise<void>

Remarks​

Fires a Soundboard Sounds gateway event.

⚠️ Discord will send the Soundboard Sounds for each of the guild ids however you may not receive the same number of events as the ids passed to Request Soundboard Sounds for one of the following reasons:

  • The bot is not in the server provided
  • The shard the message has been sent from does not receive events for the specified guild

To avoid this Discordeno will automatically try to group the ids based on what shard they will need to be sent, but this involves sending multiple messages in multiple shards

See​

https://docs.discord.com/developers/events/gateway-events#request-soundboard-sounds


resharding​

resharding: object & object

Defined in: manager.ts:728

Everything related to resharding.

Type Declaration​

checkInterval​

checkInterval: number

The interval in milliseconds, of how often to check whether resharding is needed and reshard automatically. Set to -1 to disable auto resharding.

Default​
28800000 (8 hours)
enabled​

enabled: boolean

Whether or not automated resharding should be enabled.

Default​
true
getSessionInfo?​

optional getSessionInfo?: () => Promise<{ sessionStartLimit: { maxConcurrency: number; remaining: number; resetAfter: number; total: number; }; shards: number; url: string; }>

Handler to get shard count and other session info.

Returns​

Promise<{ sessionStartLimit: { maxConcurrency: number; remaining: number; resetAfter: number; total: number; }; shards: number; url: string; }>

shardsFullPercentage​

shardsFullPercentage: number

The % of how full a shard is when resharding should be triggered.

Remarks​

We use discord recommended shard value to get an approximation of the shard full percentage to compare with this value so the bot may not reshard at the exact percentage provided but may reshard when it is a bit higher than the provided percentage. For accurate calculation, you may override the checkIfReshardingIsNeeded function

Default​
80 as in 80%
updateGuildsShardId?​

optional updateGuildsShardId?: (guildIds, shardId) => Promise<void>

Handler to edit the shard id on any cached guilds.

Parameters​
guildIds​

string[]

shardId​

number

Returns​

Promise<void>

checkIfReshardingIsNeeded​

checkIfReshardingIsNeeded: () => Promise<{ info?: { sessionStartLimit: { maxConcurrency: number; remaining: number; resetAfter: number; total: number; }; shards: number; url: string; }; needed: boolean; }>

Handler to check if resharding is necessary.

Returns​

Promise<{ info?: { sessionStartLimit: { maxConcurrency: number; remaining: number; resetAfter: number; total: number; }; shards: number; url: string; }; needed: boolean; }>

checkIntervalId?​

optional checkIntervalId?: Timeout

The interval id of the check interval. This is used to clear the interval when the manager is shutdown.

onReshardingSwitch​

onReshardingSwitch: () => Promise<void>

Handle called when all the workers have finished preparing for the resharding.

This should make the new resharded shards become the active ones and shutdown the old ones

Returns​

Promise<void>

reshard​

reshard: (info) => Promise<void>

Handler to begin resharding.

Parameters​
info​

object & object

Returns​

Promise<void>

Remarks​

This function will resolve once the resharding is done. So when all the calls to tellWorkerToPrepare and onReshardingSwitch are done.

shards​

shards: Map<number, DiscordenoShard>

Holds the shards that resharding has created. Once resharding is done, this replaces the gateway.shards

tellWorkerToPrepare​

tellWorkerToPrepare: (workerId, shardId, bucketId) => Promise<void>

Handler to communicate to a worker that it needs to spawn a new shard and identify it for the resharding.

Parameters​
workerId​

number

shardId​

number

bucketId​

number

Returns​

Promise<void>

Remarks​

This handler works in the same way as the tellWorkerToIdentify handler. So you should wait for the worker to have identified the shard before resolving the promise

Overrides​

CreateGatewayManagerOptions.resharding


sendPayload​

sendPayload: (shardId, payload) => Promise<void>

Defined in: manager.ts:768

Parameters​

shardId​

number

payload​

ShardSocketRequest

Returns​

Promise<void>


shards​

shards: Map<number, DiscordenoShard>

Defined in: manager.ts:724

The shards that are created.


shardsPerWorker​

shardsPerWorker: number

Defined in: manager.ts:597

The amount of shards to load per worker.

Default​

25

Inherited from​

CreateGatewayManagerOptions.shardsPerWorker


shutdown​

shutdown: (code, reason, clearReshardingInterval?) => Promise<void>

Defined in: manager.ts:767

Shutdown all shards.

Parameters​

code​

number

reason​

string

clearReshardingInterval?​

boolean

Returns​

Promise<void>


spawnShardDelay​

spawnShardDelay: number

Defined in: manager.ts:587

Delay in milliseconds to wait before spawning next shard. OPTIMAL IS ABOVE 5100. YOU DON'T WANT TO HIT THE RATE LIMIT!!!

Default​

5300

Inherited from​

CreateGatewayManagerOptions.spawnShardDelay


spawnShards​

spawnShards: () => Promise<void>

Defined in: manager.ts:765

Start identifying all the shards.

Returns​

Promise<void>


spreadShardsInRoundRobin​

spreadShardsInRoundRobin: boolean

Defined in: manager.ts:615

Whether to spread shards across workers in a round-robin manner.

Remarks​

By default, shards are assigned to workers in contiguous blocks based on shardsPerWorker. If any shard is left over, it will be assigned to the last worker. This means that if you have 3 workers and 40 shards while shardsPerWorker is 10, the first worker will get shards 0-9, the second worker will get shards 10-19, and so on, the last worker will get shards 20-39.

If this option is set to true, the shards will be assigned in a round-robin manner to spread more evenly across workers. For example, with 3 workers and 40 shards, the first shard will go to worker 0, the second shard to worker 1, the third shard to worker 2, the fourth shard to worker 0, and so on.

Default​

false

Inherited from​

CreateGatewayManagerOptions.spreadShardsInRoundRobin


tellWorkerToIdentify​

tellWorkerToIdentify: (workerId, shardId, bucketId) => Promise<void>

Defined in: manager.ts:776

Allows users to hook in and change to communicate to different workers across different servers or anything they like. For example using redis pubsub to talk to other servers.

Parameters​

workerId​

number

shardId​

number

bucketId​

number

Returns​

Promise<void>

Remarks​

This should wait for the worker to have identified the shard before resolving the returned promise


token​

token: string

Defined in: manager.ts:649

Bot token which is used to connect to Discord

Inherited from​

CreateGatewayManagerOptions.token


totalShards​

totalShards: number

Defined in: manager.ts:592

Total amount of shards your bot uses. Useful for zero-downtime updates or resharding.

Default​

1

Inherited from​

CreateGatewayManagerOptions.totalShards


totalWorkers​

totalWorkers: number

Defined in: manager.ts:602

The total amount of workers to use for your bot.

Default​

4

Inherited from​

CreateGatewayManagerOptions.totalWorkers


transportCompression​

transportCompression: TransportCompression | null

Defined in: manager.ts:624

What transport compression should be used

Inherited from​

CreateGatewayManagerOptions.transportCompression


url​

url: string

Defined in: manager.ts:654

The URL of the gateway which should be connected to.

Default​

"wss://gateway.discord.gg"

Inherited from​

CreateGatewayManagerOptions.url


version​

version: number

Defined in: manager.ts:659

The gateway version which should be used.

Default​

10

Inherited from​

CreateGatewayManagerOptions.version