Skip to main content

twilight_http/request/guild/user/
update_current_user_voice_state.rs

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