Skip to main content

djs_model/
voice.rs

1//! [Voice](https://docs.discord.com/developers/resources/voice): users' voice connection states
2//! and the available voice regions.
3
4use compact_str::CompactString;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8    guild::Member,
9    id::{ChannelId, GuildId, UserId},
10    util::Timestamp,
11};
12
13/// A user's [voice state](https://docs.discord.com/developers/resources/voice#voice-state-object):
14/// which voice channel they are in and whether they are muted, deafened, streaming, ….
15///
16/// Received in `GUILD_CREATE` (without `guild_id` and `member`), `VOICE_STATE_UPDATE` and from
17/// the voice state endpoints. Absent booleans are `false` (C-023; the docs' example omits
18/// `self_video`).
19///
20/// discord.js counterpart: `VoiceState`.
21#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
22#[non_exhaustive]
23pub struct VoiceState {
24    /// The guild (absent in `GUILD_CREATE`, where the guild is known).
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub guild_id: Option<GuildId>,
27    /// The connected channel (`None` once the user left).
28    pub channel_id: Option<ChannelId>,
29    /// The user.
30    pub user_id: UserId,
31    /// The user's member object (absent in `GUILD_CREATE`).
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub member: Option<Box<Member>>,
34    /// The voice session's ID.
35    pub session_id: CompactString,
36    /// Whether the server deafened the user.
37    #[serde(default)]
38    pub deaf: bool,
39    /// Whether the server muted the user.
40    #[serde(default)]
41    pub mute: bool,
42    /// Whether the user deafened themselves.
43    #[serde(default)]
44    pub self_deaf: bool,
45    /// Whether the user muted themselves.
46    #[serde(default)]
47    pub self_mute: bool,
48    /// Whether the user is streaming with "Go Live".
49    #[serde(default, skip_serializing_if = "core::ops::Not::not")]
50    pub self_stream: bool,
51    /// Whether the user's camera is on.
52    #[serde(default)]
53    pub self_video: bool,
54    /// Whether the user's permission to speak is denied (stage audience members).
55    #[serde(default)]
56    pub suppress: bool,
57    /// When the user asked to speak in a stage.
58    pub request_to_speak_timestamp: Option<Timestamp>,
59}
60
61/// A [voice region](https://docs.discord.com/developers/resources/voice#voice-region-object):
62/// a value for a voice channel's `rtc_region`.
63///
64/// discord.js counterpart: `VoiceRegion`.
65#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
66#[non_exhaustive]
67pub struct VoiceRegion {
68    /// The region's ID (the `rtc_region` value).
69    pub id: CompactString,
70    /// The region's name.
71    pub name: CompactString,
72    /// Whether this is the closest region to the current user's client.
73    pub optimal: bool,
74    /// Whether the region is deprecated (avoid switching to it).
75    pub deprecated: bool,
76    /// Whether this is a custom region (used for events).
77    pub custom: bool,
78}
79
80#[cfg(test)]
81mod tests {
82    use core::mem::size_of;
83
84    use super::*;
85
86    #[test]
87    fn sizes() {
88        assert!(size_of::<VoiceState>() <= 96, "{}", size_of::<VoiceState>());
89    }
90}