serenity/builder/
edit_voice_state.rs

1#[cfg(feature = "http")]
2use super::Builder;
3#[cfg(feature = "http")]
4use crate::http::CacheHttp;
5#[cfg(feature = "http")]
6use crate::internal::prelude::*;
7use crate::model::prelude::*;
8
9/// A builder which edits a user's voice state, to be used in conjunction with
10/// [`GuildChannel::edit_voice_state`].
11///
12/// Discord docs:
13/// - [current user](https://discord.com/developers/docs/resources/guild#modify-current-user-voice-state)
14/// - [other users](https://discord.com/developers/docs/resources/guild#modify-user-voice-state)
15#[derive(Clone, Debug, Default, Serialize)]
16#[must_use]
17pub struct EditVoiceState {
18    channel_id: Option<ChannelId>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    suppress: Option<bool>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    request_to_speak_timestamp: Option<Option<Timestamp>>,
23}
24
25impl EditVoiceState {
26    /// Equivalent to [`Self::default`].
27    pub fn new() -> Self {
28        Self::default()
29    }
30
31    /// Whether to suppress the user. Setting this to false will invite a user to speak.
32    ///
33    /// **Note**: Requires the [Mute Members] permission to suppress another user or unsuppress the
34    /// current user. This is not required if suppressing the current user.
35    ///
36    /// [Mute Members]: Permissions::MUTE_MEMBERS
37    pub fn suppress(mut self, deafen: bool) -> Self {
38        self.suppress = Some(deafen);
39        self
40    }
41
42    /// Requests or clears a request to speak. Passing `true` is equivalent to passing the current
43    /// time to [`Self::request_to_speak_timestamp`].
44    ///
45    /// **Note**: Requires the [Request to Speak] permission.
46    ///
47    /// [Request to Speak]: Permissions::REQUEST_TO_SPEAK
48    pub fn request_to_speak(mut self, request: bool) -> Self {
49        self.request_to_speak_timestamp = Some(request.then(Timestamp::now));
50        self
51    }
52
53    /// Sets the current bot user's request to speak timestamp. This can be any present or future
54    /// time.
55    ///
56    /// **Note**: Requires the [Request to Speak] permission.
57    ///
58    /// [Request to Speak]: Permissions::REQUEST_TO_SPEAK
59    pub fn request_to_speak_timestamp(mut self, timestamp: impl Into<Timestamp>) -> Self {
60        self.request_to_speak_timestamp = Some(Some(timestamp.into()));
61        self
62    }
63}
64
65#[cfg(feature = "http")]
66#[async_trait::async_trait]
67impl Builder for EditVoiceState {
68    type Context<'ctx> = (GuildId, ChannelId, Option<UserId>);
69    type Built = ();
70
71    /// Edits the given user's voice state in a stage channel. Providing a [`UserId`] will edit
72    /// that user's voice state, otherwise the current user's voice state will be edited.
73    ///
74    /// **Note**: Requires the [Request to Speak] permission. Also requires the [Mute Members]
75    /// permission to suppress another user or unsuppress the current user. This is not required if
76    /// suppressing the current user.
77    ///
78    /// # Errors
79    ///
80    /// Returns [`Error::Http`] if the user lacks permission, or if invalid data is given.
81    ///
82    /// [Request to Speak]: Permissions::REQUEST_TO_SPEAK
83    /// [Mute Members]: Permissions::MUTE_MEMBERS
84    async fn execute(
85        mut self,
86        cache_http: impl CacheHttp,
87        ctx: Self::Context<'_>,
88    ) -> Result<Self::Built> {
89        let (guild_id, channel_id, user_id) = ctx;
90
91        self.channel_id = Some(channel_id);
92        if let Some(user_id) = user_id {
93            cache_http.http().edit_voice_state(guild_id, user_id, &self).await
94        } else {
95            cache_http.http().edit_voice_state_me(guild_id, &self).await
96        }
97    }
98}