twilight_http/request/guild/user/
update_user_voice_state.rs

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