twilight_http/request/guild/user/
update_current_user_voice_state.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{Nullable, Request, TryIntoRequest},
5    response::{marker::EmptyBody, Response, ResponseFuture},
6    routing::Route,
7};
8use serde::Serialize;
9use std::future::IntoFuture;
10use twilight_model::id::{
11    marker::{ChannelMarker, GuildMarker},
12    Id,
13};
14
15#[derive(Serialize)]
16struct UpdateCurrentUserVoiceStateFields<'a> {
17    #[serde(skip_serializing_if = "Option::is_none")]
18    channel_id: Option<Id<ChannelMarker>>,
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<Nullable<&'a str>>,
23}
24
25/// Update the current user's voice state.
26#[must_use = "requests must be configured and executed"]
27pub struct UpdateCurrentUserVoiceState<'a> {
28    fields: UpdateCurrentUserVoiceStateFields<'a>,
29    guild_id: Id<GuildMarker>,
30    http: &'a Client,
31}
32
33impl<'a> UpdateCurrentUserVoiceState<'a> {
34    pub(crate) const fn new(http: &'a Client, guild_id: Id<GuildMarker>) -> Self {
35        Self {
36            fields: UpdateCurrentUserVoiceStateFields {
37                channel_id: None,
38                suppress: None,
39                request_to_speak_timestamp: None,
40            },
41            guild_id,
42            http,
43        }
44    }
45
46    /// Specify the ID of the stage channel which the user is currently connected to.
47    ///
48    /// # Caveats
49    ///
50    /// - `channel_id` must currently point to a stage channel.
51    /// - User must already be connected to this stage channel.
52    pub const fn channel_id(mut self, channel_id: Id<ChannelMarker>) -> Self {
53        self.fields.channel_id = Some(channel_id);
54
55        self
56    }
57
58    /// Set the user's request to speak.
59    ///
60    /// Set to an empty string to remove an already-present request.
61    ///
62    /// # Caveats
63    ///
64    /// - You are able to set `request_to_speak_timestamp` to any present or
65    ///   future time.
66    pub const fn request_to_speak_timestamp(mut self, request_to_speak_timestamp: &'a str) -> Self {
67        if request_to_speak_timestamp.is_empty() {
68            self.fields.request_to_speak_timestamp = Some(Nullable(None));
69        } else {
70            self.fields.request_to_speak_timestamp =
71                Some(Nullable(Some(request_to_speak_timestamp)));
72        }
73
74        self
75    }
76
77    /// Toggle the user's suppress state.
78    ///
79    /// # Caveats
80    ///
81    /// - You must have the `MUTE_MEMBERS` permission to unsuppress yourself.
82    /// - You can always suppress yourself.
83    pub const fn suppress(mut self) -> Self {
84        self.fields.suppress = Some(true);
85
86        self
87    }
88}
89
90impl IntoFuture for UpdateCurrentUserVoiceState<'_> {
91    type Output = Result<Response<EmptyBody>, Error>;
92
93    type IntoFuture = ResponseFuture<EmptyBody>;
94
95    fn into_future(self) -> Self::IntoFuture {
96        let http = self.http;
97
98        match self.try_into_request() {
99            Ok(request) => http.request(request),
100            Err(source) => ResponseFuture::error(source),
101        }
102    }
103}
104
105impl TryIntoRequest for UpdateCurrentUserVoiceState<'_> {
106    fn try_into_request(self) -> Result<Request, Error> {
107        Request::builder(&Route::UpdateCurrentUserVoiceState {
108            guild_id: self.guild_id.get(),
109        })
110        .json(&self.fields)
111        .build()
112    }
113}