Skip to main content

botrs/models/gateway/
auth.rs

1use crate::intents::Intents;
2use crate::models::Snowflake;
3use serde::{Deserialize, Serialize};
4
5/// Hello payload from the gateway.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct Hello {
8    /// Heartbeat interval in milliseconds
9    pub heartbeat_interval: u64,
10}
11
12/// Identify payload for gateway authentication.
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub struct Identify {
15    /// Bot token
16    pub token: String,
17    /// Intent flags
18    pub intents: u32,
19    /// Shard information
20    pub shard: Option<[u32; 2]>,
21    /// Properties
22    pub properties: IdentifyProperties,
23}
24
25/// Properties for identify payload.
26#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
27pub struct IdentifyProperties {
28    /// Operating system
29    #[serde(rename = "$os", default, skip_serializing_if = "String::is_empty")]
30    pub os: String,
31    /// Browser/library name
32    #[serde(rename = "$browser", default, skip_serializing_if = "String::is_empty")]
33    pub browser: String,
34    /// Device name
35    #[serde(rename = "$device", default, skip_serializing_if = "String::is_empty")]
36    pub device: String,
37}
38
39/// Identify payload.
40#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
41pub struct WSIdentityData {
42    pub token: String,
43    pub intents: u32,
44    pub shard: Vec<u32>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub properties: Option<IdentifyProperties>,
47}
48
49impl From<Identify> for WSIdentityData {
50    fn from(identify: Identify) -> Self {
51        Self {
52            token: identify.token,
53            intents: identify.intents,
54            shard: identify.shard.map(Vec::from).unwrap_or_default(),
55            properties: Some(identify.properties),
56        }
57    }
58}
59
60impl From<WSIdentityData> for Identify {
61    fn from(data: WSIdentityData) -> Self {
62        Self {
63            token: data.token,
64            intents: data.intents,
65            shard: (data.shard.len() == 2).then(|| [data.shard[0], data.shard[1]]),
66            properties: data.properties.unwrap_or_default(),
67        }
68    }
69}
70
71impl WSIdentityData {
72    pub fn new(token: impl Into<String>, intents: Intents, shard: Option<[u32; 2]>) -> Self {
73        Self {
74            token: token.into(),
75            intents: intents.bits,
76            shard: shard.map(Vec::from).unwrap_or_default(),
77            properties: Some(IdentifyProperties::default()),
78        }
79    }
80}
81
82/// Resume payload for gateway reconnection.
83#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
84pub struct Resume {
85    /// Bot token
86    pub token: String,
87    /// Session ID
88    pub session_id: String,
89    /// Last sequence number
90    pub seq: u64,
91}
92
93/// Resume payload.
94#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
95pub struct WSResumeData {
96    pub token: String,
97    pub session_id: String,
98    pub seq: u32,
99}
100
101impl From<Resume> for WSResumeData {
102    fn from(resume: Resume) -> Self {
103        Self {
104            token: resume.token,
105            session_id: resume.session_id,
106            seq: resume.seq as u32,
107        }
108    }
109}
110
111impl From<WSResumeData> for Resume {
112    fn from(data: WSResumeData) -> Self {
113        Self {
114            token: data.token,
115            session_id: data.session_id,
116            seq: u64::from(data.seq),
117        }
118    }
119}
120
121/// Ready user object.
122#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
123pub struct WSUser {
124    pub id: Snowflake,
125    pub username: String,
126    #[serde(default)]
127    pub bot: bool,
128}
129
130/// Ready event data.
131#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
132pub struct Ready {
133    /// Gateway version
134    pub version: u32,
135    /// Session ID
136    pub session_id: String,
137    /// Bot information
138    pub user: crate::models::robot::Robot,
139    /// Shard information
140    pub shard: Option<[u32; 2]>,
141}
142
143/// Ready payload.
144#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
145pub struct WSReadyData {
146    pub version: u32,
147    pub session_id: String,
148    pub user: WSUser,
149    pub shard: Vec<u32>,
150}
151
152impl From<Ready> for WSReadyData {
153    fn from(ready: Ready) -> Self {
154        Self {
155            version: ready.version,
156            session_id: ready.session_id,
157            user: WSUser {
158                id: ready.user.id,
159                username: ready.user.username,
160                bot: ready.user.bot,
161            },
162            shard: ready.shard.map(Vec::from).unwrap_or_default(),
163        }
164    }
165}