From c56dc42cd7855abab747c13fe25f54a98ddacc62 Mon Sep 17 00:00:00 2001 From: kozabrada123 <“kozabrada123@users.noreply.github.com”> Date: Mon, 22 May 2023 18:42:12 +0200 Subject: [PATCH] Add Voice StateUpdate and ServerUpdate events --- src/api/types.rs | 33 ++++++++++++++++++++++++++++++--- src/gateway.rs | 27 ++++++++++++++++++++------- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/api/types.rs b/src/api/types.rs index 138ccf9..3533705 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -1232,15 +1232,42 @@ pub struct ClientInfo { impl WebSocketEvent for SessionsReplace {} #[derive(Debug, Deserialize, Serialize, Default)] -/// See https://discord.com/developers/docs/topics/gateway-events#update-voice-state-gateway-voice-state-update-structure -pub struct GatewayVoiceStateUpdate { +/// See https://discord.com/developers/docs/topics/gateway-events#update-voice-state +/// +/// Sent to the server +/// +/// Not to be confused with [VoiceStateUpdate] +pub struct UpdateVoiceState { pub guild_id: String, pub channel_id: Option, pub self_mute: bool, pub self_deaf: bool, } -impl WebSocketEvent for GatewayVoiceStateUpdate {} +impl WebSocketEvent for UpdateVoiceState {} + +#[derive(Debug, Deserialize, Serialize, Default)] +/// See https://discord.com/developers/docs/topics/gateway-events#voice-state-update +/// +/// Received from the server +/// +/// Not to be confused with [UpdateVoiceState] +pub struct VoiceStateUpdate { + #[serde(flatten)] + pub state: VoiceState +} + +impl WebSocketEvent for VoiceStateUpdate {} + +#[derive(Debug, Deserialize, Serialize, Default)] +/// See https://discord.com/developers/docs/topics/gateway-events#voice-server-update +pub struct VoiceServerUpdate { + pub token: String, + pub guild_id: String, + pub endpoint: Option +} + +impl WebSocketEvent for VoiceServerUpdate {} #[derive(Debug, Deserialize, Serialize, Default)] /// See https://discord.com/developers/docs/topics/gateway-events#webhooks-update diff --git a/src/gateway.rs b/src/gateway.rs index 4f8eb02..486273c 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -74,7 +74,7 @@ impl GatewayHandle { self.send_json_event(3, to_send_value).await; } - /// Sends a Request Guild Members to the server + /// Sends a request guild members to the server pub async fn send_request_guild_members(&self, to_send: GatewayRequestGuildMembers) { let to_send_value = serde_json::to_value(&to_send).unwrap(); @@ -84,17 +84,17 @@ impl GatewayHandle { self.send_json_event(8, to_send_value).await; } - /// Sends a Request Guild Members to the server - pub async fn send_update_voice_state(&self, to_send: GatewayVoiceStateUpdate) { + /// Sends an update voice state to the server + pub async fn send_update_voice_state(&self, to_send: UpdateVoiceState) { let to_send_value = serde_json::to_value(&to_send).unwrap(); - println!("GW: Sending Voice State Update.."); + println!("GW: Sending Update Voice State.."); self.send_json_event(4, to_send_value).await; } - /// Sends a Call Sync + /// Sends a call sync to the server pub async fn send_call_sync(&self, to_send: CallSync) { let to_send_value = serde_json::to_value(&to_send).unwrap(); @@ -439,8 +439,14 @@ impl Gateway { let new_data: UserUpdate = serde_json::from_str(gateway_payload.d.unwrap().get()).unwrap(); self.events.lock().await.user.update.update_data(new_data).await; } - "VOICE_STATE_UPDATE" => {} - "VOICE_SERVER_UPDATE" => {} + "VOICE_STATE_UPDATE" => { + let new_data: VoiceStateUpdate = serde_json::from_str(gateway_payload.d.unwrap().get()).unwrap(); + self.events.lock().await.voice.state_update.update_data(new_data).await; + } + "VOICE_SERVER_UPDATE" => { + let new_data: VoiceServerUpdate = serde_json::from_str(gateway_payload.d.unwrap().get()).unwrap(); + self.events.lock().await.voice.server_update.update_data(new_data).await; + } "WEBHOOKS_UPDATE" => { let new_data: WebhooksUpdate = serde_json::from_str(gateway_payload.d.unwrap().get()).unwrap(); self.events.lock().await.webhooks.update.update_data(new_data).await; @@ -643,6 +649,7 @@ mod events { pub invite: Invite, pub integration: Integration, pub call: Call, + pub voice: Voice, pub webhooks: Webhooks, pub gateway_identify_payload: GatewayEvent, pub gateway_resume: GatewayEvent, @@ -740,6 +747,12 @@ mod events { pub delete: GatewayEvent } + #[derive(Default, Debug)] + pub struct Voice { + pub state_update: GatewayEvent, + pub server_update: GatewayEvent + } + #[derive(Default, Debug)] pub struct Webhooks { pub update: GatewayEvent,