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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#[cfg(feature = "http")]
use super::Builder;
use super::CreateAttachment;
#[cfg(feature = "http")]
use crate::http::CacheHttp;
#[cfg(feature = "http")]
use crate::internal::prelude::*;
use crate::model::prelude::*;
/// A builder to create a soundboard sound.
///
/// [Discord docs](https://discord.com/developers/docs/resources/soundboard#soundboard-resource)
#[derive(Clone, Debug, Serialize)]
#[must_use]
pub struct CreateSoundboard<'a> {
name: String,
sound: String,
volume: f64,
#[serde(skip_serializing_if = "Option::is_none")]
emoji_id: Option<EmojiId>,
#[serde(skip_serializing_if = "Option::is_none")]
emoji_name: Option<String>,
#[serde(skip)]
audit_log_reason: Option<&'a str>,
}
impl<'a> CreateSoundboard<'a> {
/// Creates a new builder with the given data.
pub fn new(name: impl Into<String>, sound: &CreateAttachment) -> Self {
Self {
name: name.into(),
sound: sound.to_base64(),
volume: 1.0,
emoji_id: None,
emoji_name: None,
audit_log_reason: None,
}
}
/// Set the name of the soundboard sound, replacing the current value as set in [`Self::new`].
///
/// **Note**: Must be between 2 and 32 characters long.
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
/// Set the sound file. Replaces the current value as set in [`Self::new`].
///
/// **Note**: Must be audio that is encoded in MP3 or OGG, max 512 KB, max
/// duration 5.2 seconds.
pub fn sound(mut self, sound: &CreateAttachment) -> Self {
self.sound = sound.to_base64();
self
}
/// Set the volume of the soundboard sound.
///
/// **Note**: Must be between 0 to 1.
pub fn volume(mut self, volume: f64) -> Self {
self.volume = volume;
self
}
/// Set the ID of the custom emoji.
pub fn emoji_id(mut self, id: EmojiId) -> Self {
self.emoji_id = Some(id);
self
}
/// Set the Unicode character of the custom emoji.
pub fn emoji_name(mut self, name: String) -> Self {
self.emoji_name = Some(name);
self
}
/// Sets the request's audit log reason.
pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
self.audit_log_reason = Some(reason);
self
}
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl Builder for CreateSoundboard<'_> {
type Context<'ctx> = GuildId;
type Built = Soundboard;
/// Creates a new soundboard in the guild with the data set.
///
/// **Note**: Requires the [Create Guild Expressions] permission.
///
/// # Errors
///
/// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
/// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
///
/// [Create Guild Expressions]: Permissions::CREATE_GUILD_EXPRESSIONS
async fn execute(
self,
cache_http: impl CacheHttp,
ctx: Self::Context<'_>,
) -> Result<Self::Built> {
#[cfg(feature = "cache")]
crate::utils::user_has_guild_perms(
&cache_http,
ctx,
Permissions::CREATE_GUILD_EXPRESSIONS,
)?;
cache_http.http().create_guild_soundboard(ctx, &self, self.audit_log_reason).await
}
}