spectacles_gateway/lib.rs
1//! # Spectacles Gateway
2//! This library provides an interface for spawning Discord shards with the Discord Gateway.
3//! ## Getting Started
4//! This crate uses non-blocking, asynchronous I/O provided with the tokio runtime.
5//!
6//! To begin spawning shards, simply create a new [`ShardManager`], and choose a [`ShardStrategy`].
7//! ```rust, norun
8//! use spectacles_gateway::{ShardManager, ShardStrategy};
9//! use std::env::var;
10//! use tokio::prelude::*;
11//!
12//! fn main() {
13//! let token = var("DISCORD_TOKEN").expect("Failed to parse Discord token");
14//! // Creating a shard manager
15//! tokio::run(ShardManager::new(token, ShardStrategy::Recommended)
16//! .map(|mut manager| {
17//! // Here we obtain our two streams, responsible for spawned shards and events.
18//! let (spawner, events) = manager.start_spawn();
19//! // We poll each stream concurrently in separate threads.
20//! tokio::spawn(spawner.for_each(|shard| { // Freshly spawned shard
21//! println!("Shard {:?} has successfully spawned.", shard.lock().info);
22//!
23//! Ok(())
24//! }));
25//! tokio::spawn(events.for_each(|event| { // Event, which contains the shard it belongs to, as well as the Discord packet
26//! if let Some(evt) = event.packet.t {
27//! println!("Received event from Shard {:?}: {:?}", event.shard.lock().info, evt);
28//! };
29//!
30//! Ok(())
31//! }));
32//! })
33//! .map_err(|err| {
34//! eprintln!("Failed to bootstrap sharding manager. {:?}", err);
35//! })
36//! );
37//! }
38//! ```
39//!
40//! [`ShardManager`]: struct.ShardManager.html
41//! [`ShardStrategy`]: struct.ShardStrategy.html
42//!
43
44#[macro_use] extern crate log;
45
46#[warn(rust_2018_idioms)]
47
48pub use errors::{Error, Result};
49pub use manager::*;
50pub use shard::Shard;
51
52mod manager;
53mod shard;
54mod constants;
55mod errors;
56mod queue;