djs_rest/endpoints/soundboard.rs
1//! [Soundboard endpoints](https://docs.discord.com/developers/resources/soundboard): playing
2//! sounds in voice channels, Discord's default sounds and a guild's custom sounds.
3
4use djs_model::{
5 id::{ChannelId, GuildId, SoundboardSoundId},
6 rest::{CreateGuildSoundboardSound, ModifyGuildSoundboardSound, SendSoundboardSound},
7 soundboard::SoundboardSound,
8};
9
10use super::Items;
11use crate::client::boxed;
12use crate::{Error, Request, Rest, Route};
13
14/// Playing sounds and the default sounds.
15impl Rest {
16 /// [Send Soundboard Sound](https://docs.discord.com/developers/resources/soundboard#send-soundboard-sound):
17 /// plays a sound in a voice channel.
18 ///
19 /// The bot must be connected to the channel, neither muted, deafened nor suppressed,
20 /// and needs `SPEAK` and `USE_SOUNDBOARD` (plus `USE_EXTERNAL_SOUNDS` for a sound of
21 /// another guild).
22 ///
23 /// discord.js counterpart: `VoiceChannel#sendSoundboardSound`.
24 ///
25 /// # Examples
26 ///
27 /// ```no_run
28 /// # async fn example(rest: djs_rest::Rest, channel_id: djs_model::id::ChannelId) -> Result<(), djs_rest::Error> {
29 /// use djs_model::rest::SendSoundboardSound;
30 ///
31 /// let sounds = rest.list_default_soundboard_sounds().await?;
32 /// if let Some(sound) = sounds.iter().find(|sound| sound.name == "quack") {
33 /// rest.send_soundboard_sound(channel_id, &SendSoundboardSound::new(sound.sound_id))
34 /// .await?;
35 /// }
36 /// # Ok(())
37 /// # }
38 /// ```
39 ///
40 /// # Errors
41 ///
42 /// See [`Rest::request`]; `10097` Unknown Sound; `50013` Missing Permissions.
43 pub fn send_soundboard_sound<'a>(
44 &'a self,
45 channel_id: ChannelId,
46 body: &'a SendSoundboardSound,
47 ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
48 boxed(async move {
49 self.request_ignoring_body(
50 Request::new(Route::SendSoundboardSound { channel_id }).json(body)?,
51 )
52 .await
53 })
54 }
55
56 /// [List Default Soundboard Sounds](https://docs.discord.com/developers/resources/soundboard#list-default-soundboard-sounds):
57 /// returns the sounds every user can play.
58 ///
59 /// discord.js counterpart: `Client#fetchDefaultSoundboardSounds`.
60 ///
61 /// # Errors
62 ///
63 /// See [`Rest::request`].
64 pub fn list_default_soundboard_sounds(
65 &self,
66 ) -> impl Future<Output = Result<Vec<SoundboardSound>, Error>> + Send + '_ {
67 boxed(async move {
68 self.call(Request::new(Route::ListDefaultSoundboardSounds))
69 .await
70 })
71 }
72}
73
74/// Guild soundboard sounds.
75impl Rest {
76 /// [List Guild Soundboard Sounds](https://docs.discord.com/developers/resources/soundboard#list-guild-soundboard-sounds):
77 /// returns every custom sound of a guild. [`SoundboardSound::user`] is included with the
78 /// `CREATE_GUILD_EXPRESSIONS` or `MANAGE_GUILD_EXPRESSIONS` permission. Discord wraps the
79 /// list in an object; it is unwrapped.
80 ///
81 /// discord.js counterpart: `GuildSoundboardSoundManager#fetch()`.
82 ///
83 /// # Errors
84 ///
85 /// See [`Rest::request`].
86 pub fn list_guild_soundboard_sounds(
87 &self,
88 guild_id: GuildId,
89 ) -> impl Future<Output = Result<Vec<SoundboardSound>, Error>> + Send + '_ {
90 boxed(async move {
91 let list: Items<SoundboardSound> = self
92 .request(Request::new(Route::ListGuildSoundboardSounds { guild_id }))
93 .await?;
94 Ok(list.items)
95 })
96 }
97
98 /// [Get Guild Soundboard Sound](https://docs.discord.com/developers/resources/soundboard#get-guild-soundboard-sound):
99 /// returns one custom sound of a guild.
100 ///
101 /// discord.js counterpart: `GuildSoundboardSoundManager#fetch(id)`.
102 ///
103 /// # Errors
104 ///
105 /// See [`Rest::request`]; `10097` Unknown Sound.
106 pub fn get_guild_soundboard_sound(
107 &self,
108 guild_id: GuildId,
109 sound_id: SoundboardSoundId,
110 ) -> impl Future<Output = Result<SoundboardSound, Error>> + Send + '_ {
111 boxed(async move {
112 self.call(Request::new(Route::GetGuildSoundboardSound {
113 guild_id,
114 sound_id,
115 }))
116 .await
117 })
118 }
119
120 /// [Create Guild Soundboard Sound](https://docs.discord.com/developers/resources/soundboard#create-guild-soundboard-sound):
121 /// uploads a custom sound (MP3 or Ogg, at most 512 KB and 5.2 seconds). Needs
122 /// `CREATE_GUILD_EXPRESSIONS`.
123 ///
124 /// `reason` is shown in the audit log (D-086).
125 ///
126 /// discord.js counterpart: `GuildSoundboardSoundManager#create`.
127 ///
128 /// # Examples
129 ///
130 /// ```no_run
131 /// # async fn example(rest: djs_rest::Rest, guild_id: djs_model::id::GuildId) -> Result<(), Box<dyn std::error::Error>> {
132 /// use djs_model::{rest::CreateGuildSoundboardSound, util::SoundData};
133 ///
134 /// let sound = SoundData::new(std::fs::read("tada.mp3")?)?;
135 /// let mut body = CreateGuildSoundboardSound::new("tada", sound);
136 /// body.emoji_name = Some("🎉".into());
137 /// rest.create_guild_soundboard_sound(guild_id, &body, None).await?;
138 /// # Ok(())
139 /// # }
140 /// ```
141 ///
142 /// # Errors
143 ///
144 /// See [`Rest::request`]; `30045` when the guild has no free sound slot.
145 pub fn create_guild_soundboard_sound<'a>(
146 &'a self,
147 guild_id: GuildId,
148 body: &'a CreateGuildSoundboardSound,
149 reason: Option<&'a str>,
150 ) -> impl Future<Output = Result<SoundboardSound, Error>> + Send + 'a {
151 boxed(async move {
152 self.call(
153 Request::new(Route::CreateGuildSoundboardSound { guild_id })
154 .json(body)?
155 .reason(reason),
156 )
157 .await
158 })
159 }
160
161 /// [Modify Guild Soundboard Sound](https://docs.discord.com/developers/resources/soundboard#modify-guild-soundboard-sound):
162 /// changes a sound's name, volume or emoji. Needs `MANAGE_GUILD_EXPRESSIONS`, or
163 /// `CREATE_GUILD_EXPRESSIONS` for sounds the bot created.
164 ///
165 /// `reason` is shown in the audit log (D-086).
166 ///
167 /// discord.js counterpart: `SoundboardSound#edit`, `GuildSoundboardSoundManager#edit`.
168 ///
169 /// # Errors
170 ///
171 /// See [`Rest::request`]; `10097` Unknown Sound.
172 pub fn modify_guild_soundboard_sound<'a>(
173 &'a self,
174 guild_id: GuildId,
175 sound_id: SoundboardSoundId,
176 body: &'a ModifyGuildSoundboardSound,
177 reason: Option<&'a str>,
178 ) -> impl Future<Output = Result<SoundboardSound, Error>> + Send + 'a {
179 boxed(async move {
180 self.call(
181 Request::new(Route::ModifyGuildSoundboardSound { guild_id, sound_id })
182 .json(body)?
183 .reason(reason),
184 )
185 .await
186 })
187 }
188
189 /// [Delete Guild Soundboard Sound](https://docs.discord.com/developers/resources/soundboard#delete-guild-soundboard-sound):
190 /// deletes a custom sound. Needs `MANAGE_GUILD_EXPRESSIONS`, or
191 /// `CREATE_GUILD_EXPRESSIONS` for sounds the bot created.
192 ///
193 /// `reason` is shown in the audit log (D-086).
194 ///
195 /// discord.js counterpart: `SoundboardSound#delete`, `GuildSoundboardSoundManager#delete`.
196 ///
197 /// # Errors
198 ///
199 /// See [`Rest::request`]; `10097` Unknown Sound.
200 pub fn delete_guild_soundboard_sound<'a>(
201 &'a self,
202 guild_id: GuildId,
203 sound_id: SoundboardSoundId,
204 reason: Option<&'a str>,
205 ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
206 boxed(async move {
207 self.call(
208 Request::new(Route::DeleteGuildSoundboardSound { guild_id, sound_id })
209 .reason(reason),
210 )
211 .await
212 })
213 }
214}