disrust/
lib.rs

1pub mod channel;
2pub mod emoji;
3mod getter;
4pub mod guild;
5pub mod permissions;
6pub mod role;
7pub mod snowflake;
8use futures_util::Future;
9pub use gateway::Event;
10use gateway::Gateway;
11pub use gateway::Intent;
12pub use guild::Guild;
13use reqwest::{
14    Client, ClientBuilder,
15    header::{HeaderMap, HeaderValue},
16};
17use snowflake::Snowflake;
18mod gateway;
19pub mod user;
20pub struct Bot {
21    pub(crate) partial_guilds: Vec<Snowflake>,
22    guilds: Vec<Guild>,
23    token: String,
24}
25impl Bot {
26    /// Creates a new bot
27    pub fn new(token: &str) -> Self {
28        
29        Self {
30            partial_guilds: vec![],
31            guilds: vec![],
32            token: token.to_string(),
33        }
34    }
35    
36    /// Logs into a bot account using a token and starts an event loop to handle all events coming from the discord gateway
37    pub async fn login<F: Future>(&mut self, intents: Vec<Intent>, eh: fn(Event) -> F) -> ! {
38        let mut gateway = Gateway::connect().await;
39        gateway.authenticate(&self.token, intents);
40        gateway.start_event_loop(self, eh, create_client(&self.token)).await
41    }
42}
43fn create_client(token: &str) -> Client {
44    let mut headers = HeaderMap::new();
45    headers.insert(
46        "Authorization",
47        HeaderValue::from_str(&format!("Bot {}", token)).unwrap(),
48    );
49    ClientBuilder::default()
50            .default_headers(headers)
51            .build()
52            .unwrap()
53}