#discord #gateway #spectacles #shard #distributed #message

spectacles-gateway

A distributed Discord gateway interface for Spectacles

4 releases

0.3.0 Apr 13, 2019
0.2.3 Mar 24, 2019
0.2.1 Mar 17, 2019
0.2.0 Mar 11, 2019

#8 in #shard


Used in spectacles

MIT license

130KB
2.5K SLoC

crates-io-badge Downloads docs-badge

Spectacles Gateway

A rich Spectacles gateway client for Rust.

About

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.

Features

  • Asynchronous websocket message handling.
  • Zero-Downtime shard spawning.
  • Integrates seamlessly with the spectacles-brokers package.

Example - Basic Sharder

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);
        })
    );
}

Dependencies

~19–32MB
~554K SLoC