Skip to content

Commit

Permalink
[project] add 'gateway'
Browse files Browse the repository at this point in the history
Information about the gateway can be read about here:

<https://dawn.valley.cafe/chapter_1_crates/section_3_gateway.html>

Signed-off-by: Zeyla Hellyer <zeyla@hellyer.dev>
  • Loading branch information
zeylahellyer committed Oct 10, 2019
1 parent 82e6bf1 commit 644d2b6
Show file tree
Hide file tree
Showing 28 changed files with 3,074 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ members = [
"cache/trait",
"command-parser",
"dawn",
"gateway",
"gateway/examples/shard",
"http",
"http/examples/get-message",
"http/examples/proxy",
Expand Down
8 changes: 7 additions & 1 deletion dawn/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ version = "0.1.0"
[dependencies]
dawn-cache = { optional = true, path = "../cache/base" }
dawn-command-parser = { optional = true, path = "../command-parser" }
dawn-gateway = { optional = true, path = "../gateway" }
dawn-http = { optional = true, path = "../http" }
dawn-model = { optional = true, path = "../model" }

[dev-dependencies]
futures-preview = "0.3.0-alpha.19"
tokio = "0.2.0-alpha.6"

[features]
default = ["cache", "command-parser", "http", "model"]
default = ["cache", "command-parser", "gateway", "http", "model"]

cache = ["dawn-cache"]
command-parser = ["dawn-command-parser"]
gateway = ["dawn-gateway"]
http = ["dawn-http"]
model = ["dawn-model"]
46 changes: 25 additions & 21 deletions dawn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,42 @@
//!
//! ## Examples
//!
//! ```rust,ignore
//! use futures::StreamExt;
//! ```no_run
//! use dawn::{
//! gateway::{Config, Event, Shard},
//! gateway::shard::{Config, Event, Shard},
//! http::Client as HttpClient,
//! };
//! use futures::StreamExt;
//! use std::{
//! env,
//! error::Error,
//! };
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! let token = env::var("DISCORD_TOKEN")?;
//!
//! let http = HttpClient::new(&token);
//!
//! let config = Config::builder(&token).build();
//! let mut shard = Shard::new(config);
//! shard.connect().await?;
//! let mut events = shard.events();
//! let shard = Shard::new(token).await?;
//! let mut events = shard.events().await;
//!
//! while let Some(event) = events.next().await {
//! runtime::spawn(handle_event(event));
//! let http = http.clone();
//!
//! tokio::spawn(async {
//! let _ = handle_event(event, http);
//! });
//! }
//! # Ok(()) }
//!
//! async fn handle_event(event: Event) -> Result<(), Box<dyn Error>> {
//! async fn handle_event(event: Event, http: HttpClient) -> Result<(), Box<dyn Error>> {
//! match event {
//! Event::Connected(connected) => {
//! println!("Connected on shard {}", connected.shard_id);
//! Event::MessageCreate(msg) if msg.content == "!ping" => {
//! http.create_message(msg.channel_id).content("Pong!").await?;
//! },
//! Event::Message(msg) => {
//! if msg.content == "!ping" {
//! http.send_message(msg.channel_id).content("Pong!").await?;
//! }
//! Event::ShardConnected(connected) => {
//! println!("Connected on shard {}", connected.shard_id);
//! },
//! _ => {},
//! }
Expand All @@ -135,10 +138,10 @@
//! Maintaining a cache of guilds, users, channels, and more sent by the
//! gateway:
//!
//! ```rust,ignore
//! ```ignore
//! use futures::StreamExt;
//! use dawn::{
//! cache::InMemoryCache,
//! cache::{InMemoryCache, UpdateCache},
//! gateway::{Config, Event, Shard},
//! };
//! use std::{
Expand All @@ -148,10 +151,8 @@
//!
//! let token = env::var("DISCORD_TOKEN")?;
//!
//! let config = Config::builder(&token).build();
//! let mut shard = Shard::new(config);
//! shard.connect().await?;
//! let mut events = shard.events();
//! let shard = Shard::new(token).await?;
//! let mut events = shard.events().await;
//!
//! let cache = InMemoryCache::new();
//!
Expand Down Expand Up @@ -199,6 +200,9 @@ pub extern crate dawn_cache as cache;
#[cfg(feature = "command-parser")]
pub extern crate dawn_command_parser as command_parser;

#[cfg(feature = "gateway")]
pub extern crate dawn_gateway as gateway;

#[cfg(feature = "http")]
pub extern crate dawn_http as http;

Expand Down
39 changes: 39 additions & 0 deletions gateway/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
authors = ["Zeyla Hellyer <zeyla@hellyer.dev>"]
documentation = "https://docs.rs/dawn-gateway"
edition = "2018"
homepage = "https://dawn.valley.cafe"
include = ["src/*.rs", "Cargo.toml"]
keywords = ["discord", "discord-api", "dawn"]
license = "ISC"
name = "dawn-gateway"
publish = false
readme = "README.md"
repository = "https://github.com/dawn-rs/dawn.git"
version = "0.1.0"

[dependencies]
async-trait = "0.1"
bitflags = "1"
dawn-cache-trait = { optional = true, path = "../cache/trait" }
dawn-http = { path = "../http" }
dawn-model = { path = "../model" }
futures-channel-preview = "0.3.0-alpha.19"
futures-util-preview = "0.3.0-alpha.19"
log = "0.4"
serde = { features = ["derive"], version = "1" }
serde_json = "1"
snafu = "0.5"
tokio-executor = "0.2.0-alpha.6"
tokio-net = "0.2.0-alpha.6"
tokio-timer = "0.3.0-alpha.6"
tokio-tungstenite = { git = "https://github.com/dbcfd/tokio-tungstenite", branch = "tokio2" }
url = "2"

[dev-dependencies]
futures-preview = "0.3.0-alpha.19"
tokio = "0.2.0-alpha.6"

[features]
default = ["cache"]
cache = ["dawn-cache-trait"]
1 change: 1 addition & 0 deletions gateway/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
../LICENSE.md
11 changes: 11 additions & 0 deletions gateway/examples/shard/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
authors = ["Zeyla Hellyer <zeyla@hellyer.dev>"]
edition = "2018"
name = "dawn-gateway-example-shard"
version = "0.1.0"

[dependencies]
dawn-gateway = { path = "../.." }
futures-preview = "0.3.0-alpha.19"
pretty_env_logger = "0.3"
tokio = "0.2.0-alpha.6"
19 changes: 19 additions & 0 deletions gateway/examples/shard/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use dawn_gateway::Shard;
use futures::StreamExt;
use std::{env, error::Error};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
pretty_env_logger::init_timed();

let shard = Shard::new(env::var("DISCORD_TOKEN")?).await?;
println!("Created shard");

let mut events = shard.events().await;

while let Some(event) = events.next().await {
println!("Event: {:?}", event);
}

Ok(())
}
Loading

0 comments on commit 644d2b6

Please sign in to comment.