Skip to main content

twilight_http/request/guild/user/
update_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::{Request, TryIntoRequest},
7    routing::Route,
8};
9use serde::Serialize;
10use std::future::IntoFuture;
11use twilight_model::id::{
12    Id,
13    marker::{ChannelMarker, GuildMarker, UserMarker},
14};
15
16#[derive(Serialize)]
17struct UpdateUserVoiceStateFields {
18    channel_id: Id<ChannelMarker>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    suppress: Option<bool>,
21}
22
23/// Update another user's voice state.
24#[must_use = "requests must be configured and executed"]
25pub struct UpdateUserVoiceState<'a> {
26    fields: UpdateUserVoiceStateFields,
27    guild_id: Id<GuildMarker>,
28    http: &'a Client,
29    user_id: Id<UserMarker>,
30}
31
32impl<'a> UpdateUserVoiceState<'a> {
33    pub(crate) const fn new(
34        http: &'a Client,
35        guild_id: Id<GuildMarker>,
36        user_id: Id<UserMarker>,
37        channel_id: Id<ChannelMarker>,
38    ) -> Self {
39        Self {
40            fields: UpdateUserVoiceStateFields {
41                channel_id,
42                suppress: None,
43            },
44            guild_id,
45            http,
46            user_id,
47        }
48    }
49
50    /// Toggle the user's suppress state.
51    ///
52    /// # Caveats
53    ///
54    /// - You must have the [`MUTE_MEMBERS`] permission to use this method.
55    /// - When unsuppressed, non-bot users will have their
56    ///   `request_to_speak_timestamp` set to the current time. Bot users will
57    ///   not.
58    /// - When suppressed, the user will have their `request_to_speak_timestamp`
59    ///   removed.
60    ///
61    /// [`MUTE_MEMBERS`]: twilight_model::guild::Permissions::MUTE_MEMBERS
62    pub const fn suppress(mut self) -> Self {
63        self.fields.suppress = Some(true);
64
65        self
66    }
67}
68
69#[cfg(not(target_os = "wasi"))]
70impl IntoFuture for UpdateUserVoiceState<'_> {
71    type Output = Result<Response<EmptyBody>, Error>;
72
73    type IntoFuture = ResponseFuture<EmptyBody>;
74
75    fn into_future(self) -> Self::IntoFuture {
76        let http = self.http;
77
78        match self.try_into_request() {
79            Ok(request) => http.request(request),
80            Err(source) => ResponseFuture::error(source),
81        }
82    }
83}
84
85impl TryIntoRequest for UpdateUserVoiceState<'_> {
86    fn try_into_request(self) -> Result<Request, Error> {
87        Request::builder(&Route::UpdateUserVoiceState {
88            guild_id: self.guild_id.get(),
89            user_id: self.user_id.get(),
90        })
91        .json(&self.fields)
92        .build()
93    }
94}