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