rustycord 0.1.5

A fast, lightweight, and feature-rich Discord bot library written in Rust.
Documentation
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
use std::{process::exit, time::Instant};

const GATEWAY_VERSION: i8 = 10;
const GATEWAY_BASE_URL: &str = "wss://gateway.discord.gg/";
/// The WebSocket connection to the Discord Gateway.
/// This will be used to interact with the Discord Gateway.
use crate::{
    client::Client,
    gateway::response::{DiscordOpCode, DiscordReceiveEvent, ReceiveEvent},
};
use futures::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use serde_json::from_str;
use tokio::net::TcpStream;

use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, WebSocketStream};

#[derive(Debug, Deserialize, Serialize)]
pub enum WebSocketReceiveData {}

/// Receive Message from the gateway
/// <https://discord.com/developers/docs/topics/gateway-events#receive-events>
///
#[derive(Serialize, Deserialize, Debug)]
pub struct ReceiveMessageOpcode {
    /// The event type.
    op: i8,
}

/// Identifies the client to the gateway.
/// <https://discord.com/developers/docs/topics/gateway-events#identify-identify-connection-properties>
#[derive(Debug, Serialize)]
pub struct IdentifyProperties {
    /// Your operating system.
    os: String,

    /// Your library name.
    browser: String,

    /// Your library name.
    device: String,
}

/// <https://discord.com/developers/docs/topics/gateway-events#activity-object-activity-types>
#[derive(Debug, Serialize, Clone)]
pub enum ActivityType {
    Game = 0,
    Streaming = 1,
    Listening = 2,
    Watching = 3,
    Custom = 4,
    Competing = 5,
}

/// The activity object that the user is doing.
/// <https://discord.com/developers/docs/topics/gateway-events#activity-object-activity-structure>
///
/// Note Bot users are only able to set name, state, type, and url.
#[derive(Debug, Serialize, Clone)]
pub struct Activity {
    /// The activity's name.
    pub name: String,

    /// Activity type.
    // #[serde(rename = "type")]
    pub r#type: u8,
    // Stream url, is validated when type is 1.
    pub url: Option<String>,

    // User's current party status
    pub state: Option<String>,
}

/// Sent by the client to indicate a presence or status update.
/// <https://discord.com/developers/docs/topics/gateway-events#update-presence>
#[derive(Debug, Serialize, Clone)]
pub struct PresenceUpdate {
    /// Unix time (in milliseconds) of when the client went idle, or null if the client is not idle.
    pub since: u128,

    /// Array of activity objects.
    pub activities: Vec<Activity>,

    /// The user's new status.
    pub status: String,

    /// Whether or not the client is afk.
    pub afk: bool,
}

/// The activity object that the user is doing.
#[derive(Debug, Serialize)]
#[serde(untagged)]

pub enum WebSocketMessageData {
    /// the heartbeat interval that the client should heartbeat to.
    /// <https://discord.com/developers/docs/topics/gateway-events#heartbeat/>
    Heartbeat(u64),

    /// Used to trigger the initial handshake with the gateway.
    /// <https://discord.com/developers/docs/topics/gateway-events#identify-identify-structure>
    Identify {
        /// authentication token
        token: String,
        /// Gateway Intents you wish to receive.
        intents: i32,

        /// The properties for the client.
        properties: IdentifyProperties,

        /// Whether this connection supports compression of packets.
        compress: bool,

        /// Value between 50 and 250, total number of members
        /// where the gateway will stop sending offline members
        /// in the guild member list.
        large_threshold: i32,

        /// Used for Guild Sharding.
        /// A list of two integers, [shard_id, num_shards].
        shard: Option<Vec<i32>>,

        /// Presence structure for initial presence information.
        presence: Option<PresenceUpdate>,
    },

    /// Sent by the client to indicate a presence or status update.
    /// <https://discord.com/developers/docs/topics/gateway-events#update-presence>
    PresenceUpdate(PresenceUpdate),
}
//  "properties": {"$os": "linux", "$browser": "rustycord", "$device": "rustycord"};

#[derive(Debug, Serialize)]
pub struct WebSocketMessage {
    op: u8,
    d: WebSocketMessageData,
}

pub struct RustycordWebSocketResponse {}

pub struct DiscordWebSocket(WebSocketStream<MaybeTlsStream<TcpStream>>);

/// The WebSocket connection to the Discord Gateway.
/// This is the main struct that will be used to interact with the Discord Gateway.
/// It will be used to send and receive messages from the gateway.
/// It will also be used to handle the connection to the gateway.
/// The connection will be maintained by sending heartbeats to the gateway every 5 seconds.
/// The connection will be authenticated by sending an identify message to the gateway.
/// The connection will be reconnected if the gateway disconnects.
/// The connection will be resumed if the gateway disconnects.
///
/// # Methods
///
/// * `connect` - Connect to the Discord Gateway using the provided client
/// * `recv` - Receive message from the gateway in json format
/// * `send_json` - Send message to the gateway in json format
/// * `send_heartbeat` - Send heartbeat to the gateway every 5 seconds
/// * `send_identify` - Send identify message to the gateway to authenticate the client
/// * `presence_update` - Send presence update to the gateway
///
#[allow(dead_code)]
impl DiscordWebSocket {
    /// All gateway events in Discord are tagged with an opcode that denotes the payload type.
    /// Your connection to our gateway may also sometimes close. When it does, you will
    /// receive a close code that tells you what happened.
    /// https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-gateway-opcodes
    ///

    /// An event was dispatched.
    /// Client Action: Receive only
    const DISPATCH: u8 = 0;

    /// Fired periodically by the client to keep the connection alive.
    /// Client Action: Send/Receive
    const HEARTBEAT: u8 = 1;

    /// Starts a new session during the initial handshake.
    /// Client Action: Send only
    const IDENTIFY: u8 = 2;

    /// Update the client's presence.
    /// Client Action: Send only
    const PRESENCE_UPDATE: u8 = 3;

    /// Used to join/leave or move between voice channels.
    /// Client Action: Send only
    const VOICE_STATE_UPDATE: u8 = 4;

    /// Resume a previous session that was disconnected.
    /// Client Action: Send only
    const RESUME: u8 = 6;

    /// You should attempt to reconnect and resume immediately.
    /// Client Action: Receive only
    const RECONNECT: u8 = 7;

    /// Request information about offline guild members in a large guild.
    /// Client Action: Send only
    const REQUEST_GUILD_MEMBERS: u8 = 8;

    /// The session has been invalidated. You should reconnect and identify/resume accordingly.
    /// Client Action: Receive only
    const INVALID_SESSION: u8 = 9;

    /// Sent immediately after connecting, contains the `heartbeat_interval` to use.
    /// Client Action: Receive only
    const HELLO: u8 = 10;

    /// Sent in response to receiving a heartbeat to acknowledge that it has been received.
    /// Client Action: Receive only
    const HEARTBEAT_ACK: u8 = 11;

    /// Connect to the Discord Gateway using the provided client
    ///
    /// # Arguments
    ///
    /// * `client` - the client to connect to the gateway
    pub(crate) async fn connect(shard_id: usize) -> Self {
        let gateway_url = format!("{}?v={}&encoding=json", GATEWAY_BASE_URL, GATEWAY_VERSION);
        // let (ws_stream, _) = connect_async(gateway_url).await.unwrap();
        match connect_async(gateway_url).await {
            Ok((ws_stream, _)) => {
                log::info!("🔌 {} -> Connected to The Discord", shard_id);
                Self(ws_stream)
            }
            Err(err) => {
                log::error!("Shard {} -> Got Error Message {}", shard_id, err);
                exit(2)
            }
        }
        // Self(ws_stream)
    }

    /// receive message from the gateway in json format
    pub async fn recv(&mut self) -> Result<(Option<ReceiveEvent>, bool), String> {
        log::trace!("🔄 Waiting for message from Discord gateway...");
        let message = self.0.next().await.unwrap().unwrap();
        let message = match message {
            Message::Text(text) => {
                log::trace!(
                    "📥 Received text message from gateway (length: {} chars)",
                    text.len()
                );
                text.to_string()
            }
            Message::Binary(data) => {
                log::debug!(
                    "📥 Received binary message from gateway (length: {} bytes)",
                    data.len()
                );
                String::from_utf8_lossy(&data).to_string()
            }
            Message::Close(close_frame) => {
                log::warn!("🔒 Received close frame from gateway: {:?}", close_frame);
                return Err("Connection closed by Discord".to_string());
            }
            _ => {
                log::debug!("📥 Received non-text message from gateway");
                "".to_string()
            }
        };
        if message.is_empty() {
            return Err("🔴 -> Error receiving message".to_string());
        }

        log::trace!("🔍 Parsing gateway message...");
        let _opcode = from_str::<ReceiveMessageOpcode>(message.as_str()).unwrap();

        let r_event = from_str::<DiscordReceiveEvent>(message.as_str()).unwrap();

        log::debug!("📨 Gateway event: {:?} (op: {})", r_event.t, _opcode.op);

        // change discord event to rustycord event
        let r_event = ReceiveEvent {
            t: r_event.t,
            s: r_event.s,
            op: DiscordOpCode::from(_opcode.op),
            d: r_event.d,
        };

        Ok((Some(r_event), true))
    }

    /// send message to the gateway in json format
    ///
    /// # Arguments
    ///
    /// * `message` - the message to send to the gateway
    ///
    /// # Example
    ///
    /// ```no_run
    /// use rustycord::gateway::gateway::WebSocketMessageData;
    ///
    /// // Example of creating message data for heartbeat
    /// let heartbeat_data = WebSocketMessageData::Heartbeat(251);
    /// println!("Created heartbeat message data: {:?}", heartbeat_data);
    /// ```
    pub async fn send_json(&mut self, message: WebSocketMessage) -> bool {
        let message = Message::Text(serde_json::to_string(&message).unwrap().into());
        match self.0.send(message).await {
            Ok(_) => true,

            Err(err) => {
                log::error!("Error sending message {}", err);
                false
            }
        }
    }

    /// send heartbeat to the gateway every 5 seconds
    pub async fn send_heartbeat(&mut self) -> bool {
        log::debug!("💓 Sending heartbeat to Discord gateway");
        let heartbeat = WebSocketMessage {
            op: Self::HEARTBEAT,
            d: WebSocketMessageData::Heartbeat(251),
        };
        let result = self.send_json(heartbeat).await;
        if result {
            log::trace!("💓 Heartbeat sent successfully");
        } else {
            log::error!("💔 Failed to send heartbeat");
        }
        result
    }

    /// send identify message to the gateway to authenticate the client
    ///
    /// # Arguments
    ///
    /// * `token` - the token of the bot
    /// * `compress` - whether the connection supports compression of packets
    /// * `large_threshold` - Value between 50 and 250, total number of members
    ///              where the gateway will stop sending offline members in
    ///             the guild member list (default 50)
    /// * `shard` - used for Guild Sharding. A list of two integers, [shard_id, num_shards].
    /// * `presence` - presence structure for initial presence information
    pub async fn send_identify(
        &mut self,
        token: String,
        intents: Option<i32>,
        compress: bool,
        large_threshold: Option<i32>,
        shard: Option<Vec<i32>>,
        presence: Option<PresenceUpdate>,
    ) {
        let int: i32 = match intents {
            Some(int) => {
                log::debug!("🎯 Using provided intents: {}", int);
                int
            }
            None => {
                log::warn!("No intents provided using default");
                1
            }
        };
        log::debug!("🎭 Setting up presence and activity...");
        let pre = presence.unwrap_or_else(|| PresenceUpdate {
            since: 0,
            activities: vec![Activity {
                name: "with Rust".to_string(),
                r#type: 0,
                url: Some("https://iamdhakrey.dev".to_string()),
                state: Some("Enjoying Rust".to_string()),
            }],
            status: "online".to_string(),
            afk: false,
        });
        let shr: Vec<i32> = shard.unwrap_or_else(|| vec![0, 1]);
        log::debug!("🗂️ Using shard configuration: {:?}", shr);
        let identify = WebSocketMessage {
            op: DiscordWebSocket::IDENTIFY,
            d: WebSocketMessageData::Identify {
                token: token.to_string(),
                intents: int,
                properties: IdentifyProperties {
                    os: "linux".to_string(),
                    browser: "rustycord".to_string(),
                    device: "rustycord".to_string(),
                },
                compress: compress,
                large_threshold: large_threshold.unwrap_or(50),
                shard: Some(shr),
                presence: Some(pre),
            },
        };
        log::info!("🔑 -> Sending Identification Message");
        self.send_json(identify).await;
    }

    /// send presence update to the gateway
    ///
    /// # Arguments
    ///
    /// * `presence` - presence structure for initial presence information
    async fn presence_update(&mut self, presence: PresenceUpdate) {
        let activities: Vec<Activity> = presence.activities.clone();
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis();
        let presence = WebSocketMessage {
            op: Self::PRESENCE_UPDATE,
            d: WebSocketMessageData::PresenceUpdate(PresenceUpdate {
                since: now,
                activities: activities,
                status: "online".to_string(),
                afk: false,
            }),
        };

        self.send_json(presence).await;
    }

    async fn resume(&mut self) {
        let resume = WebSocketMessage {
            op: Self::RESUME,
            d: WebSocketMessageData::Heartbeat(251),
        };
        self.send_json(resume).await;
    }
}

pub struct Manager {
    pub ws: DiscordWebSocket,
    last_heartbeat: Option<Instant>,
    last_heartbeat_ack: Option<Instant>,
    heartbeat_interval: Option<i8>,

    pub token: String,
    pub intents: i32,
    shard_id: usize,
    pub total_shards: usize,
    pub client: Option<Client>,
}

impl Manager {
    pub async fn new(token: String, intents: i32, shard_id: usize, total_shards: usize) -> Self {
        let ws = DiscordWebSocket::connect(shard_id).await;
        let last_heartbeat = None;
        let last_heartbeat_ack = None;
        let heartbeat_interval = 0;
        Self {
            ws,
            last_heartbeat,
            last_heartbeat_ack,
            heartbeat_interval: Some(heartbeat_interval),
            token,
            intents,
            shard_id,
            total_shards,
            client: None,
        }
    }

    pub fn set_client(&mut self, client: Client) {
        self.client = Some(client);
    }

    /// return false if heartbeat is not required to send to the gateway
    ///
    /// True - where heartbeat is required to send to the gateway
    /// True - where heartbeat interval is not set or last heartbeat ack is more than 15 seconds ago
    /// True - where last heartbeat is less than 5 seconds ago
    ///
    /// False - where last heartbeat is more than 5 seconds ago
    /// False - where heartbeat interval is set and last heartbeat ack is less than 15 seconds ago
    ///
    pub async fn is_required_heartbeat(&mut self) -> bool {
        if self.heartbeat_interval.is_none() {
            log::debug!("Need to send heartbeat");
            return true;
        }
        if self.last_heartbeat_ack.is_none() {
            log::debug!("Need to send heartbeat");
            return true;
        }
        if self.last_heartbeat.is_none() {
            log::debug!("Need to send heartbeat");
            return true;
        }
        if self.last_heartbeat.unwrap().elapsed().as_secs() < 5 {
            log::debug!("NO Need to send heartbeat");
            return false;
        }
        if self.last_heartbeat_ack.unwrap().elapsed().as_secs() > 15 {
            log::debug!("Need to send heartbeat");
            return true;
        }
        if self.last_heartbeat.unwrap().elapsed().as_secs() > 5 {
            log::debug!("Need to send heartbeat");
            return true;
        }
        log::debug!("Need to send heartbeat");
        true
    }

    pub async fn handle_heartbeat(&mut self) {
        if self.is_required_heartbeat().await {
            self.ws.send_heartbeat().await;
            self.last_heartbeat = Some(Instant::now());
        }
    }

    pub async fn handle_event(&mut self) {
        match self.ws.recv().await {
            Ok((Some(event), _)) => {
                log::debug!(
                    "Shard {} received event: {:?} op: {:?}",
                    self.shard_id,
                    event.t,
                    event.op
                );
                let event_clone = event.clone();
                if DiscordOpCode::from(event_clone.op) == DiscordOpCode::HeartbeatAck {
                    self.last_heartbeat_ack = Some(Instant::now())
                } else {
                    self.dispatch(event_clone).await
                }
            }
            Ok((None, _)) => {
                log::warn!("Shard {} received None event", self.shard_id);
            }
            Err(err) => {
                log::error!("Shard {} error receiving event: {:?}", self.shard_id, err);
            }
        }
    }
    /// Dispatch events to handlers
    async fn dispatch(&self, event: ReceiveEvent) {
        if let Some(client) = &self.client {
            if let Err(e) = client.event_dispatcher.dispatch_event(&event, client).await {
                log::error!("Error dispatching event: {:?}", e);
            }
        } else {
            log::warn!("No client available for event dispatching");
        }
    }

    // async fn event_handler(&self, event: ReceiveEvent) {
    //     log::debug!("Shard {} - Event {:?}", self.shard_id, event.t);
    //     if event.t == GatewayReceiveEventName::READY {
    //         self.ready().await;
    //     } else if event.t == GatewayReceiveEventName::MESSAGE_CREATE {
    //         self.message_create(event).await
    //     }
    // }

    // /// Handle READY event
    // async fn ready(&self) {
    //     log::info!("🟢 Shard {} is ready", self.shard_id);
    // }

    // /// Handle Message Create Event
    // async fn message_create(&self, event: ReceiveEvent) {
    //     if let Some(data) = event.d.clone() {
    //         log::debug!("Shard {} - Message Data: {:?}", self.shard_id, data);
    //         let message_result =
    //             serde_json::from_str::<ChannelMessage>(&serde_json::to_string(&data).unwrap());
    //         match message_result {
    //             Ok(message) => {
    //                 log::info!("Shard {} - 📩 Message: {:?}", self.shard_id, message);
    //             }
    //             Err(err) => log::error!("Shard {} - Error parsing message: {}", self.shard_id, err),
    //         }
    //     }
    // }
    /// Send identify message with shard information
    pub async fn send_identify(
        &mut self,
        token: String,
        intents: i32,
        compress: bool,
        large_threshold: Option<i32>,
        shard: Option<Vec<i32>>,
        presence: Option<PresenceUpdate>,
    ) {
        let identify = WebSocketMessage {
            op: DiscordWebSocket::IDENTIFY,
            d: WebSocketMessageData::Identify {
                token: token.to_string(),
                intents,
                properties: IdentifyProperties {
                    os: "linux".to_string(),
                    browser: "rustycord".to_string(),
                    device: "rustycord".to_string(),
                },
                compress,
                large_threshold: large_threshold.unwrap_or(50),
                shard,
                presence,
            },
        };
        log::info!(
            "🔑 Shard {} - Sending Identification Message",
            self.shard_id
        );
        self.ws.send_json(identify).await;
    }
}