Skip to main content

djs_model/rest/
soundboard.rs

1//! Bodies of the [soundboard endpoints](https://docs.discord.com/developers/resources/soundboard).
2
3use compact_str::CompactString;
4use serde::Serialize;
5
6use crate::{
7    id::{EmojiId, GuildId, SoundboardSoundId},
8    util::{Patch, SoundData},
9};
10
11/// The body of [Send Soundboard Sound](https://docs.discord.com/developers/resources/soundboard#send-soundboard-sound).
12///
13/// discord.js counterpart: the argument of `VoiceChannel#sendSoundboardSound`.
14///
15/// # Examples
16///
17/// ```
18/// use djs_model::{
19///     id::{GuildId, SoundboardSoundId},
20///     rest::SendSoundboardSound,
21/// };
22///
23/// // A sound of another guild needs that guild's ID.
24/// let mut body = SendSoundboardSound::new(SoundboardSoundId::new(1_270_000_000_000_000_001));
25/// body.source_guild_id = Some(GuildId::new(1_100_000_000_000_000_009));
26/// assert_eq!(
27///     serde_json::to_string(&body)?,
28///     r#"{"sound_id":"1270000000000000001","source_guild_id":"1100000000000000009"}"#
29/// );
30/// # Ok::<(), serde_json::Error>(())
31/// ```
32#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize)]
33#[non_exhaustive]
34pub struct SendSoundboardSound {
35    /// The sound to play.
36    pub sound_id: SoundboardSoundId,
37    /// The guild the sound belongs to, required for sounds of another guild (which also
38    /// needs the `USE_EXTERNAL_SOUNDS` permission); not needed for default sounds.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub source_guild_id: Option<GuildId>,
41}
42
43impl SendSoundboardSound {
44    /// Plays `sound_id`, a default sound or a sound of the voice channel's guild.
45    ///
46    /// discord.js counterpart: `VoiceChannel#sendSoundboardSound`.
47    #[must_use]
48    pub const fn new(sound_id: SoundboardSoundId) -> Self {
49        Self {
50            sound_id,
51            source_guild_id: None,
52        }
53    }
54}
55
56/// The body of [Create Guild Soundboard Sound](https://docs.discord.com/developers/resources/soundboard#create-guild-soundboard-sound).
57///
58/// The sound (MP3 or Ogg) may be at most 512 KB and 5.2 seconds long.
59///
60/// discord.js counterpart: `GuildSoundboardSoundCreateOptions`.
61///
62/// # Examples
63///
64/// ```
65/// use djs_model::{rest::CreateGuildSoundboardSound, util::SoundData};
66///
67/// let ogg = b"OggS\x00\x02...".to_vec();
68/// let mut body = CreateGuildSoundboardSound::new("tada", SoundData::new(ogg)?);
69/// body.volume = Some(0.5);
70/// body.emoji_name = Some("🎉".into());
71/// let json = serde_json::to_string(&body)?;
72/// assert!(json.starts_with(r#"{"name":"tada","sound":"data:audio/ogg;base64,"#));
73/// assert!(json.ends_with(r#","volume":0.5,"emoji_name":"🎉"}"#));
74/// # Ok::<(), Box<dyn std::error::Error>>(())
75/// ```
76#[derive(Clone, Debug, PartialEq, Serialize)]
77#[non_exhaustive]
78pub struct CreateGuildSoundboardSound {
79    /// The name (2–32 characters).
80    pub name: CompactString,
81    /// The sound.
82    pub sound: SoundData,
83    /// The volume, from 0 to 1 (Discord's default: 1).
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub volume: Option<f64>,
86    /// A custom emoji shown with the sound.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub emoji_id: Option<EmojiId>,
89    /// A Unicode emoji shown with the sound.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub emoji_name: Option<CompactString>,
92}
93
94impl CreateGuildSoundboardSound {
95    /// A sound named `name` playing `sound`, at full volume, without an emoji.
96    ///
97    /// discord.js counterpart: `GuildSoundboardSoundCreateOptions`.
98    #[must_use]
99    pub fn new(name: impl Into<CompactString>, sound: SoundData) -> Self {
100        Self {
101            name: name.into(),
102            sound,
103            volume: None,
104            emoji_id: None,
105            emoji_name: None,
106        }
107    }
108}
109
110/// The body of [Modify Guild Soundboard Sound](https://docs.discord.com/developers/resources/soundboard#modify-guild-soundboard-sound).
111///
112/// Every field is optional; the sound itself cannot be changed. [`Patch`] fields can be
113/// cleared.
114///
115/// discord.js counterpart: `GuildSoundboardSoundEditOptions`.
116///
117/// # Examples
118///
119/// ```
120/// use djs_model::{rest::ModifyGuildSoundboardSound, util::Patch};
121///
122/// let mut body = ModifyGuildSoundboardSound::default();
123/// body.volume = Patch::Set(0.25);
124/// body.emoji_name = Patch::Clear;
125/// assert_eq!(serde_json::to_string(&body)?, r#"{"volume":0.25,"emoji_name":null}"#);
126/// # Ok::<(), serde_json::Error>(())
127/// ```
128#[derive(Clone, Debug, Default, PartialEq, Serialize)]
129#[non_exhaustive]
130pub struct ModifyGuildSoundboardSound {
131    /// The new name (2–32 characters).
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub name: Option<CompactString>,
134    /// The volume, from 0 to 1.
135    #[serde(skip_serializing_if = "Patch::is_keep")]
136    pub volume: Patch<f64>,
137    /// A custom emoji shown with the sound.
138    #[serde(skip_serializing_if = "Patch::is_keep")]
139    pub emoji_id: Patch<EmojiId>,
140    /// A Unicode emoji shown with the sound.
141    #[serde(skip_serializing_if = "Patch::is_keep")]
142    pub emoji_name: Patch<CompactString>,
143}