1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#![feature(test)]
#![feature(try_blocks)]
#![allow(where_clauses_object_safety)]
#![allow(clippy::identity_op)]
extern crate self as automate;
extern crate test;
#[macro_use]
extern crate automate_derive;
#[macro_use]
extern crate log;
pub mod json;
pub mod http;
pub mod gateway;
mod snowflake;
mod events;
mod macros;
mod errors;
mod logger;
pub use async_trait::async_trait;
pub use tokio;
pub use events::Listener;
pub use http::HttpAPI;
pub use gateway::{GatewayAPI, Session};
pub use json::Nullable;
pub use logger::setup_logging;
pub use snowflake::Snowflake;
pub use errors::Error;
use tokio::runtime::Runtime;
use std::sync::{Mutex, Arc};
use std::thread::JoinHandle;
use std::thread;
pub struct Discord {
http: HttpAPI,
listeners: Arc<Mutex<Vec<Box<dyn Listener + Send>>>>,
}
impl Discord {
pub fn new(token: &str) -> Discord {
Discord {
http: HttpAPI::new(token),
listeners: Arc::new(Mutex::new(Vec::new())),
}
}
pub fn with_listener(self, listener: Box<dyn Listener + Send>) -> Self {
self.listeners.lock().unwrap().push(listener);
self
}
pub async fn connect(self) -> Result<!, Error> {
GatewayAPI::connect(self.http.clone(), self.listeners.clone()).await
}
pub fn connect_blocking(self) -> Result<!, Error> {
Runtime::new().unwrap().block_on(self.connect())
}
pub fn connect_detached(self) -> JoinHandle<Result<!, Error>> {
thread::spawn(move || {
Runtime::new().unwrap().block_on(self.connect())
})
}
}