Skip to main content

djs_rest/endpoints/
voice.rs

1//! [Voice endpoints](https://docs.discord.com/developers/resources/voice): voice regions and
2//! voice states. Joining voice channels goes through the gateway, not these endpoints.
3
4use djs_model::{
5    id::{GuildId, UserId},
6    rest::{ModifyCurrentUserVoiceState, ModifyUserVoiceState},
7    voice::{VoiceRegion, VoiceState},
8};
9
10use crate::client::boxed;
11use crate::{Error, Request, Rest, Route};
12
13impl Rest {
14    /// [List Voice Regions](https://docs.discord.com/developers/resources/voice#list-voice-regions):
15    /// returns the regions a voice or stage channel's `rtc_region` can be set to.
16    ///
17    /// discord.js counterpart: `Client#fetchVoiceRegions`.
18    ///
19    /// # Errors
20    ///
21    /// See [`Rest::request`].
22    pub fn list_voice_regions(
23        &self,
24    ) -> impl Future<Output = Result<Vec<VoiceRegion>, Error>> + Send + '_ {
25        boxed(async move { self.call(Request::new(Route::ListVoiceRegions)).await })
26    }
27
28    /// [Get Current User Voice State](https://docs.discord.com/developers/resources/voice#get-current-user-voice-state):
29    /// returns the bot's voice state in a guild.
30    ///
31    /// discord.js counterpart: `VoiceStateManager#fetch('@me')`.
32    ///
33    /// # Errors
34    ///
35    /// See [`Rest::request`]; `10065` Unknown Voice State when the bot is not in a voice
36    /// channel of the guild.
37    pub fn get_current_user_voice_state(
38        &self,
39        guild_id: GuildId,
40    ) -> impl Future<Output = Result<VoiceState, Error>> + Send + '_ {
41        boxed(async move {
42            self.call(Request::new(Route::GetCurrentUserVoiceState { guild_id }))
43                .await
44        })
45    }
46
47    /// [Get User Voice State](https://docs.discord.com/developers/resources/voice#get-user-voice-state):
48    /// returns a member's voice state. If they are in a voice channel, the bot needs the
49    /// permission to connect to it.
50    ///
51    /// discord.js counterpart: `VoiceStateManager#fetch`.
52    ///
53    /// # Errors
54    ///
55    /// See [`Rest::request`]; `10065` Unknown Voice State when the user is not in a voice
56    /// channel of the guild.
57    pub fn get_user_voice_state(
58        &self,
59        guild_id: GuildId,
60        user_id: UserId,
61    ) -> impl Future<Output = Result<VoiceState, Error>> + Send + '_ {
62        boxed(async move {
63            self.call(Request::new(Route::GetUserVoiceState { guild_id, user_id }))
64                .await
65        })
66    }
67
68    /// [Modify Current User Voice State](https://docs.discord.com/developers/resources/voice#modify-current-user-voice-state):
69    /// makes the bot a speaker or audience member of the stage it is in, or requests to
70    /// speak.
71    ///
72    /// discord.js counterpart: `VoiceState#edit`, `#setSuppressed`, `#setRequestToSpeak`.
73    ///
74    /// # Examples
75    ///
76    /// ```no_run
77    /// # async fn example(rest: djs_rest::Rest, guild_id: djs_model::id::GuildId, stage: djs_model::id::ChannelId) -> Result<(), djs_rest::Error> {
78    /// use djs_model::rest::ModifyCurrentUserVoiceState;
79    ///
80    /// // Become a speaker (needs MUTE_MEMBERS).
81    /// let mut body = ModifyCurrentUserVoiceState::default();
82    /// body.channel_id = Some(stage);
83    /// body.suppress = Some(false);
84    /// rest.modify_current_user_voice_state(guild_id, &body).await?;
85    /// # Ok(())
86    /// # }
87    /// ```
88    ///
89    /// # Errors
90    ///
91    /// See [`Rest::request`].
92    pub fn modify_current_user_voice_state<'a>(
93        &'a self,
94        guild_id: GuildId,
95        body: &'a ModifyCurrentUserVoiceState,
96    ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
97        boxed(async move {
98            self.request_ignoring_body(
99                Request::new(Route::ModifyCurrentUserVoiceState { guild_id }).json(body)?,
100            )
101            .await
102        })
103    }
104
105    /// [Modify User Voice State](https://docs.discord.com/developers/resources/voice#modify-user-voice-state):
106    /// makes a member a speaker or audience member of the stage they are in. Needs
107    /// `MUTE_MEMBERS`.
108    ///
109    /// discord.js counterpart: `VoiceState#edit`, `#setSuppressed`.
110    ///
111    /// # Errors
112    ///
113    /// See [`Rest::request`].
114    pub fn modify_user_voice_state<'a>(
115        &'a self,
116        guild_id: GuildId,
117        user_id: UserId,
118        body: &'a ModifyUserVoiceState,
119    ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
120        boxed(async move {
121            self.request_ignoring_body(
122                Request::new(Route::ModifyUserVoiceState { guild_id, user_id }).json(body)?,
123            )
124            .await
125        })
126    }
127}