Skip to main content

GatewayManager

Documentation


Documentation / @discordeno/gateway / GatewayManager

Interface: GatewayManager

Defined in: gateway/src/manager.ts:714

Extends

Properties

buckets

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

Defined in: gateway/src/manager.ts:716

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: gateway/src/manager.ts:875

This managers cache related settings.

requestMembers

requestMembers: object

requestMembers.enabled

requestMembers.enabled: boolean

Whether or not request member requests should be cached.

Default
false
requestMembers.pending

requestMembers.pending: Collection<string, RequestMemberRequest>

The pending requests.

Overrides

Required.cache


calculateShardId()

calculateShardId: (guildId, totalShards?) => number

Defined in: gateway/src/manager.ts:785

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: gateway/src/manager.ts:760

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

Returns

number


calculateWorkerId()

calculateWorkerId: (shardId) => number

Defined in: gateway/src/manager.ts:762

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

Parameters

shardId

number

Returns

number


compress

compress: boolean

Defined in: gateway/src/manager.ts:623

Whether incoming payloads are compressed using zlib.

Default

false

Inherited from

Required.compress


connection

connection: object

Defined in: gateway/src/manager.ts:618

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

sessionStartLimit

sessionStartLimit: object

sessionStartLimit.maxConcurrency

sessionStartLimit.maxConcurrency: number

sessionStartLimit.remaining

sessionStartLimit.remaining: number

sessionStartLimit.resetAfter

sessionStartLimit.resetAfter: number

sessionStartLimit.total

sessionStartLimit.total: number

shards

shards: number

url

url: string

Inherited from

Required.connection


editBotStatus()

editBotStatus: (data) => Promise<void>

Defined in: gateway/src/manager.ts:808

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: gateway/src/manager.ts:816

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: gateway/src/manager.ts:662

The events handlers

Inherited from

Required.events


firstShardId

firstShardId: number

Defined in: gateway/src/manager.ts:573

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

Default

0

Inherited from

Required.firstShardId


identify()

identify: (shardId) => Promise<void>

Defined in: gateway/src/manager.ts:779

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: gateway/src/manager.ts:630

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

Default

0

Inherited from

Required.intents


joinVoiceChannel()

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

Defined in: gateway/src/manager.ts:801

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://discord.com/developers/docs/topics/gateway#update-voice-state


kill()

kill: (shardId) => Promise<void>

Defined in: gateway/src/manager.ts:781

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: gateway/src/manager.ts:578

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

Default

0

Inherited from

Required.lastShardId


leaveVoiceChannel()

leaveVoiceChannel: (guildId) => Promise<void>

Defined in: gateway/src/manager.ts:853

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://discord.com/developers/docs/topics/gateway#update-voice-state


logger

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

Defined in: gateway/src/manager.ts:727

The logger for the gateway manager.

Overrides

Required.logger


makePresence()

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

Defined in: gateway/src/manager.ts:684

Make the presence for when the bot connects to the gateway

Returns

Promise<undefined | DiscordUpdatePresence>

Remarks

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

Inherited from

Required.makePresence


preferSnakeCase

preferSnakeCase: boolean

Defined in: gateway/src/manager.ts:588

Whether to send the discord packets in snake case form.

Default

false

Inherited from

Required.preferSnakeCase


prepareBuckets()

prepareBuckets: () => void

Defined in: gateway/src/manager.ts:764

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

Returns

void


properties

properties: object

Defined in: gateway/src/manager.ts:632

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

Required.properties


requestIdentify()

requestIdentify: (shardId) => Promise<void>

Defined in: gateway/src/manager.ts:783

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: gateway/src/manager.ts:840

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://discord.com/developers/docs/topics/gateway#request-guild-members


requestSoundboardSounds()

requestSoundboardSounds: (guildIds) => Promise<void>

Defined in: gateway/src/manager.ts:873

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://discord.com/developers/docs/topics/gateway-events#request-soundboard-sounds


resharding

resharding: object & object

Defined in: gateway/src/manager.ts:729

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>

Type declaration

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

Required.resharding


sendPayload()

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

Defined in: gateway/src/manager.ts:769

Parameters

shardId

number

payload

ShardSocketRequest

Returns

Promise<void>


shards

shards: Map<number, DiscordenoShard>

Defined in: gateway/src/manager.ts:725

The shards that are created.


shardsPerWorker

shardsPerWorker: number

Defined in: gateway/src/manager.ts:598

The amount of shards to load per worker.

Default

25

Inherited from

Required.shardsPerWorker


shutdown()

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

Defined in: gateway/src/manager.ts:768

Shutdown all shards.

Parameters

code

number

reason

string

clearReshardingInterval?

boolean

Returns

Promise<void>


spawnShardDelay

spawnShardDelay: number

Defined in: gateway/src/manager.ts:583

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

Required.spawnShardDelay


spawnShards()

spawnShards: () => Promise<void>

Defined in: gateway/src/manager.ts:766

Start identifying all the shards.

Returns

Promise<void>


spreadShardsInRoundRobin

spreadShardsInRoundRobin: boolean

Defined in: gateway/src/manager.ts:616

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

Required.spreadShardsInRoundRobin


tellWorkerToIdentify()

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

Defined in: gateway/src/manager.ts:777

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: gateway/src/manager.ts:650

Bot token which is used to connect to Discord

Inherited from

Required.token


totalShards

totalShards: number

Defined in: gateway/src/manager.ts:593

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

Default

1

Inherited from

Required.totalShards


totalWorkers

totalWorkers: number

Defined in: gateway/src/manager.ts:603

The total amount of workers to use for your bot.

Default

4

Inherited from

Required.totalWorkers


transportCompression

transportCompression: null | TransportCompression

Defined in: gateway/src/manager.ts:625

What transport compression should be used

Inherited from

Required.transportCompression


url

url: string

Defined in: gateway/src/manager.ts:655

The URL of the gateway which should be connected to.

Default

"wss://gateway.discord.gg"

Inherited from

Required.url


version

version: number

Defined in: gateway/src/manager.ts:660

The gateway version which should be used.

Default

10

Inherited from

Required.version