summaryrefslogtreecommitdiff
path: root/gateway/src/shard/actions.rs
blob: b6ef03826bf5decb68b8c29c511d9cebebb873d6 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::env;

use futures::SinkExt;
use common::log::{debug, error, info};
use serde::Serialize;
use serde_json::Value;
use std::fmt::Debug;

use crate::{
    error::GatewayError,
    payloads::{
        gateway::BaseMessage,
        opcodes::{
            identify::{Identify, IdentifyProprerties},
            presence::PresenceUpdate,
            resume::Resume,
            OpCodes,
        },
    },
};

use super::Shard;

/// Implement the available actions for nova in the gateway.
impl Shard {
    /// sends a message through the websocket
    pub async fn _send<T: Serialize + Debug>(
        &mut self,
        message: BaseMessage<T>,
    ) -> Result<(), GatewayError> {
        debug!("Senging message {:?}", message);
        if let Some(connection) = &mut self.connection {
            if let Err(e) = connection.conn.send(message).await {
                error!("failed to send message {:?}", e);
                Err(GatewayError::from(e))
            } else {
                Ok(())
            }
        } else {
            Err(GatewayError::from("no open connection".to_string()))
        }
    }

    pub async fn _identify(&mut self) -> Result<(), GatewayError> {
        if let Some(state) = self.state.clone() {
            info!("Using session");
            self._send(BaseMessage {
                t: None,
                sequence: None,
                op: OpCodes::Resume,
                data: Resume {
                    token: self.config.token.clone(),
                    seq: state.sequence,
                    session_id: state.session_id.clone(),
                },
            })
            .await
        } else {
            info!("Sending login");
            self._send(BaseMessage {
                t: None,
                sequence: None,
                op: OpCodes::Identify,
                data: Identify {
                    token: self.config.token.clone(),
                    intents: self.config.intents,
                    properties: IdentifyProprerties {
                        os: env::consts::OS.to_string(),
                        browser: "Nova".to_string(),
                        device: "Nova".to_string(),
                    },
                    shard: Some([0, 2]),
                    compress: Some(false),
                    large_threshold: Some(500),
                    presence: None,
                },
            })
            .await
        }
    }

    pub async fn _disconnect(&mut self) {}

    /// Updates the presence of the current shard.
    #[allow(dead_code)]
    pub async fn presence_update(&mut self, update: PresenceUpdate) -> Result<(), GatewayError> {
        self._send(BaseMessage {
            t: None,
            sequence: None,
            op: OpCodes::PresenceUpdate,
            data: update,
        })
        .await
    }
    /// Updates the voice status of the current shard in a certain channel.
    #[allow(dead_code)]
    pub async fn voice_state_update(&mut self) -> Result<(), GatewayError> {
        self._send(BaseMessage {
            t: None,
            sequence: None,
            op: OpCodes::VoiceStateUpdate,
            // todo: proper payload for this
            data: Value::Null,
        })
        .await
    }
    /// Ask discord for more informations about offline guild members.
    #[allow(dead_code)]
    pub async fn request_guild_members(&mut self) -> Result<(), GatewayError> {
        self._send(BaseMessage {
            t: None,
            sequence: None,
            op: OpCodes::RequestGuildMembers,
            // todo: proper payload for this
            data: Value::Null,
        })
        .await
    }

    pub async fn _send_heartbeat(&mut self) -> Result<(), GatewayError> {
        self._send(BaseMessage {
            t: None,
            sequence: None,
            op: OpCodes::Heartbeat,
            data: self.state.as_ref().unwrap().sequence
        }).await
    }
}