Expand description
§Spectacles Gateway
This library provides an interface for spawning Discord shards with the Discord Gateway.
§Getting Started
This crate uses non-blocking, asynchronous I/O provided with the tokio runtime.
To begin spawning shards, simply create a new ShardManager
, and choose a ShardStrategy
.
use spectacles_gateway::{ShardManager, ShardStrategy};
use std::env::var;
use tokio::prelude::*;
fn main() {
let token = var("DISCORD_TOKEN").expect("Failed to parse Discord token");
// Creating a shard manager
tokio::run(ShardManager::new(token, ShardStrategy::Recommended)
.map(|mut manager| {
// Here we obtain our two streams, responsible for spawned shards and events.
let (spawner, events) = manager.start_spawn();
// We poll each stream concurrently in separate threads.
tokio::spawn(spawner.for_each(|shard| { // Freshly spawned shard
println!("Shard {:?} has successfully spawned.", shard.lock().info);
Ok(())
}));
tokio::spawn(events.for_each(|event| { // Event, which contains the shard it belongs to, as well as the Discord packet
if let Some(evt) = event.packet.t {
println!("Received event from Shard {:?}: {:?}", event.shard.lock().info, evt);
};
Ok(())
}));
})
.map_err(|err| {
eprintln!("Failed to bootstrap sharding manager. {:?}", err);
})
);
}
Structs§
- Event
Handler - A stream of incoming Discord events for a shard.
- Shard
- A Spectacles Gateway shard.
- Shard
Event - Information about a Discord Gateway event received for a shard.
- Shard
Manager - The central hub for all shards, where shards are spawned and maintained.
- Spawner
- A stream of shards being spawned and emitting the ready event.
Enums§
- Error
- Represents a global error which can occur throughout the library.
- Shard
Strategy - The strategy in which you would like to spawn shards.
Type Aliases§
- Manager
Shard - An alias for a shard spawned with the sharding manager.
- Result
- A modified result type which encompasses the global error type.
- Shard
Map - A collection of shards, keyed by shard ID.