serenity/model/
voice.rs

1//! Representations of voice information.
2
3use serde::de::{Deserialize, Deserializer};
4use serde::Serialize;
5
6use crate::model::guild::Member;
7use crate::model::id::{ChannelId, GuildId, UserId};
8use crate::model::Timestamp;
9
10/// Information about an available voice region.
11///
12/// [Discord docs](https://discord.com/developers/docs/resources/voice#voice-region-object).
13#[derive(Clone, Debug, Deserialize, Serialize)]
14#[non_exhaustive]
15pub struct VoiceRegion {
16    /// Whether it is a custom voice region, which is used for events.
17    pub custom: bool,
18    /// Whether it is a deprecated voice region, which you should avoid using.
19    pub deprecated: bool,
20    /// The internal Id of the voice region.
21    pub id: String,
22    /// A recognizable name of the location of the voice region.
23    pub name: String,
24    /// Whether the voice region is optimal for use by the current user.
25    pub optimal: bool,
26}
27
28/// A user's state within a voice channel.
29///
30/// [Discord docs](https://discord.com/developers/docs/resources/voice#voice-state-object).
31#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
32#[derive(Clone, Debug, Deserialize, Serialize)]
33#[serde(remote = "Self")]
34#[non_exhaustive]
35pub struct VoiceState {
36    pub channel_id: Option<ChannelId>,
37    pub deaf: bool,
38    pub guild_id: Option<GuildId>,
39    pub member: Option<Member>,
40    pub mute: bool,
41    pub self_deaf: bool,
42    pub self_mute: bool,
43    pub self_stream: Option<bool>,
44    pub self_video: bool,
45    pub session_id: String,
46    pub suppress: bool,
47    pub user_id: UserId,
48    /// When unsuppressed, non-bot users will have this set to the current time. Bot users will be
49    /// set to [`None`]. When suppressed, the user will have their
50    /// [`Self::request_to_speak_timestamp`] removed.
51    pub request_to_speak_timestamp: Option<Timestamp>,
52}
53
54// Manual impl needed to insert guild_id into Member
55impl<'de> Deserialize<'de> for VoiceState {
56    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
57        // calls #[serde(remote)]-generated inherent method
58        let mut state = Self::deserialize(deserializer)?;
59        if let (Some(guild_id), Some(member)) = (state.guild_id, state.member.as_mut()) {
60            member.guild_id = guild_id;
61        }
62        Ok(state)
63    }
64}
65
66impl Serialize for VoiceState {
67    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
68        // calls #[serde(remote)]-generated inherent method
69        Self::serialize(self, serializer)
70    }
71}