NAME

Discord::Client::Gateway::Shards - Gateway shards in one process

SYNOPSIS

use Discord::Client;

# Use Discord's recommended shard count.
my $bot = Discord::Client->new(
    token => $token, loop => $loop, intents => ['GUILDS'], shards => 'auto',
);
$bot->run;

DESCRIPTION

Runs several Gateways in one process with shared REST discovery and an Identify scheduler. Startup waits for every shard to be ready. Each shard maintains its own heartbeat and Resume state. Separate processes need external Identify coordination.

Callbacks receive the originating child Discord::Client::Gateway. Each listener has independent failure reporting and uses the child's handler and queue capacity. A fatal child failure stops the manager; orderly close stops all children.

METHODS

new

new(token => $token, loop => $loop, shards => 'auto') returns a manager without I/O. shards defaults to auto, using the recommended count from /gateway/bot. A positive integer selects an explicit count. max_shards defaults to 1024. Other transport and intent options follow "new" in Discord::Client::Gateway.

connect

Discovers Gateway metadata and starts the children. Returns a Future resolving to the manager when all shards are ready. Cancelling it closes the manager.

completion

Returns the lifetime Future. It resolves on orderly shutdown and fails when a child fails fatally.

close

Closes children and owned resources and returns a Future waiting for cleanup. Repeated calls return the same Future. An injected REST client remains caller-owned.

on

on(EVENT => CODE) registers a Gateway event callback on every child and returns the manager. Listeners can be added before or after connecting.

send

send(opcode, data, guild_id => ID) returns a Future for the target guild's shard write. Use shard_id => N to select directly; the default is zero. Outbound restrictions follow "send" in Discord::Client::Gateway.

broadcast

broadcast(opcode, data) requires all shards ready and returns a Future waiting for all writes. Otherwise it fails with state.

shard_for_guild

shard_for_guild(guild_id) returns the shard number using Discord's routing rule, (guild_id >> 22) % shard_count, with arbitrary-precision arithmetic. Invalid IDs or an undiscovered count throw.

shards

Returns a copied hashref mapping shard IDs to child Gateway objects.

state

Returns ready when all children are ready, reconnecting during recovery, or the current startup/shutdown state.

loop

Returns the IO::Async::Loop.

identify_limiter

Returns the shared internal Identify scheduler. Its API is not a public contract.

stats

Returns a hashref with state, total shard count, child counters and Identify budget.

LICENSE

Apache License 2.0.