Crates.io | spectacles-gateway |
lib.rs | spectacles-gateway |
version | 0.3.0 |
source | src |
created_at | 2019-03-11 04:14:01.753167 |
updated_at | 2019-04-13 21:37:41.260213 |
description | A distributed Discord gateway interface for Spectacles. |
homepage | |
repository | https://github.com/spec-tacles/spectacles-rs |
max_upload_size | |
id | 119998 |
size | 35,798 |
A rich Spectacles gateway client for Rust.
This crate allows you to interact with the Discord gateway. Please refer to the Discord Gateway Docs for more background on how to use this crate.
use std::env::var;
use spectacles_gateway::{ShardManager, ShardStrategy};
use spectacles_model::gateway::ReceivePacket;
use futures::future::Future;
fn main() {
let token = var("DISCORD_TOKEN").expect("No Discord Token was provided.");
// tokio.run() boostraps our Tokio application.
tokio::run(ShardManager::new(token, ShardStrategy::Recommended)
.map(|mut manager| {
// The start_spawn() method returns a tuple of async streams, for handling spawned shards and shard events.
let (spawner, events) = manager.start_spawn();
// Now, we poll the streams concurrently in separate threads.
tokio::spawn(spawner.for_each(|shard| {
println!("Shard {:?} has successfully spawned.", shard.lock().info);
Ok(())
}));
tokio::spawn(events.for_each(|event| {
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);
})
);
}