From 7eba95513da330015ad73479bdf95ceb13176497 Mon Sep 17 00:00:00 2001 From: Filip Hoffmann Date: Thu, 23 Jul 2026 22:43:39 +0200 Subject: [PATCH] soundboard sound endpoints --- src/grom.gleam | 176 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 160 insertions(+), 16 deletions(-) diff --git a/src/grom.gleam b/src/grom.gleam index 168d461..0dbd267 100644 --- a/src/grom.gleam +++ b/src/grom.gleam @@ -17237,8 +17237,8 @@ pub type SoundboardSoundContentType { OggSoundboardSound } -pub opaque type CreateSoundboardSound { - CreateSoundboardSound( +pub opaque type CreateGuildSoundboardSound { + CreateGuildSoundboardSound( name: String, data: BitArray, content_type: SoundboardSoundContentType, @@ -17248,30 +17248,41 @@ pub opaque type CreateSoundboardSound { } /// The data must be maximum 512 kB and maximum 5.2 seconds. -pub fn new_soundboard_sound( +pub fn new_guild_soundboard_sound( named name: String, data data: BitArray, content_type content_type: SoundboardSoundContentType, -) -> CreateSoundboardSound { - CreateSoundboardSound(name:, data:, content_type:, volume: None, emoji: None) +) -> CreateGuildSoundboardSound { + CreateGuildSoundboardSound( + name:, + data:, + content_type:, + volume: None, + emoji: None, + ) } /// The volume will be clamped between 0.0 and 1.0, so make sure to provide a value within that range. -pub fn new_soundboard_sound_with_volume( - create: CreateSoundboardSound, +pub fn new_guild_soundboard_sound_with_volume( + create: CreateGuildSoundboardSound, volume: Float, -) -> CreateSoundboardSound { - CreateSoundboardSound(..create, volume: Some(float.clamp(volume, 0.0, 1.0))) +) -> CreateGuildSoundboardSound { + CreateGuildSoundboardSound( + ..create, + volume: Some(float.clamp(volume, 0.0, 1.0)), + ) } -pub fn new_soundboard_sound_with_emoji( - create: CreateSoundboardSound, +pub fn new_guild_soundboard_sound_with_emoji( + create: CreateGuildSoundboardSound, emoji: SoundboardSoundEmoji, -) -> CreateSoundboardSound { - CreateSoundboardSound(..create, emoji: Some(emoji)) +) -> CreateGuildSoundboardSound { + CreateGuildSoundboardSound(..create, emoji: Some(emoji)) } -fn create_soundboard_sound_to_json(create: CreateSoundboardSound) -> Json { +fn create_guild_soundboard_sound_to_json( + create: CreateGuildSoundboardSound, +) -> Json { let content_type = case create.content_type { Mp3SoundboardSound -> "audio/mpeg" OggSoundboardSound -> "audio/ogg" @@ -17293,10 +17304,10 @@ fn create_soundboard_sound_to_json(create: CreateSoundboardSound) -> Json { pub fn new_guild_soundboard_sound_request( token token: Token, in_guild_with_id guild_id: Snowflake(Guild), - using create: CreateSoundboardSound, + using create: CreateGuildSoundboardSound, reason reason: Option(String), ) -> Request(String) { - let body = create |> create_soundboard_sound_to_json |> json.to_string + let body = create |> create_guild_soundboard_sound_to_json |> json.to_string new_request( token:, @@ -17306,3 +17317,136 @@ pub fn new_guild_soundboard_sound_request( |> request.set_body(body) |> request_with_reason(reason) } + +pub fn new_guild_soundboard_sound_response( + response: Response(String), +) -> Result(SoundboardSound, RestError) { + handle_response(response, decode_with: soundboard_sound_decoder()) +} + +pub opaque type ModifyGuildSoundboardSound { + ModifyGuildSoundboardSound( + name: Option(String), + volume: Option(Float), + emoji: Modification(SoundboardSoundEmoji), + ) +} + +pub fn new_modify_guild_soundboard_sound() -> ModifyGuildSoundboardSound { + ModifyGuildSoundboardSound(name: None, volume: None, emoji: Skip) +} + +pub fn modify_guild_soundboard_sound_name( + modify: ModifyGuildSoundboardSound, + new name: String, +) -> ModifyGuildSoundboardSound { + ModifyGuildSoundboardSound(..modify, name: Some(name)) +} + +/// The volume will be clamped between 0.0 and 1.0, so make sure to provide a value within that range. +pub fn modify_guild_soundboard_sound_volume( + modify: ModifyGuildSoundboardSound, + new volume: Float, +) -> ModifyGuildSoundboardSound { + ModifyGuildSoundboardSound( + ..modify, + volume: Some(float.clamp(volume, 0.0, 1.0)), + ) +} + +pub fn modify_guild_soundboard_sound_emoji( + modify: ModifyGuildSoundboardSound, + new emoji: SoundboardSoundEmoji, +) -> ModifyGuildSoundboardSound { + ModifyGuildSoundboardSound(..modify, emoji: Modify(emoji)) +} + +pub fn delete_guild_soundboard_sound_emoji( + modify: ModifyGuildSoundboardSound, +) -> ModifyGuildSoundboardSound { + ModifyGuildSoundboardSound(..modify, emoji: Delete) +} + +/// If the sound was created by the current user, requires the `AllowCreatingGuildExpressions` +/// or `AllowManagingGuildExpressions` permission. +/// +/// For all other sounds, requires the `AllowManagingGuildExpressions` permission. +pub fn modify_guild_soundboard_sound_request( + token token: Token, + in_guild_with_id guild_id: Snowflake(Guild), + sound_with_id sound_id: Snowflake(SoundboardSound), + using modify: ModifyGuildSoundboardSound, + reason reason: Option(String), +) -> Request(String) { + let body = modify |> modify_guild_soundboard_sound_to_json |> json.to_string + + new_request( + token:, + to: "/guilds/" + <> snowflake_to_string(guild_id) + <> "/soundboard-sounds/" + <> snowflake_to_string(sound_id), + method: http.Patch, + ) + |> request.set_body(body) + |> request_with_reason(reason) +} + +pub fn modify_guild_soundboard_sound_response( + response: Response(String), +) -> Result(SoundboardSound, RestError) { + handle_response(response, decode_with: soundboard_sound_decoder()) +} + +fn modify_guild_soundboard_sound_to_json( + modify: ModifyGuildSoundboardSound, +) -> Json { + [ + optional_to_json(modify.name, "name", json.string), + optional_to_json(modify.volume, "volume", json.float), + ..modification_guild_soundboard_sound_emoji_to_json(modify.emoji) + ] + |> list_to_json_object +} + +fn modification_guild_soundboard_sound_emoji_to_json( + emoji: Modification(SoundboardSoundEmoji), +) -> List(Result(#(String, Json), Nil)) { + case emoji { + Modify(UnicodeSoundboardSoundEmoji(character:)) -> [ + Ok(#("emoji_name", json.string(character))), + ] + Modify(CustomSoundboardSoundEmoji(id:)) -> [ + Ok(#("emoji_id", snowflake_to_json(id))), + ] + Delete -> [Ok(#("emoji_id", json.null())), Ok(#("emoji_name", json.null()))] + Skip -> [] + } +} + +/// If the sound was created by the current user, requires the `AllowCreatingGuildExpressions` +/// or `AllowManagingGuildExpressions` permission. +/// +/// For all other sounds, requires the `AllowManagingGuildExpressions` permission. +pub fn delete_guild_soundboard_sound_request( + token token: Token, + in_guild_with_id guild_id: Snowflake(Guild), + sound_with_id sound_id: Snowflake(SoundboardSound), + reason reason: Option(String), +) -> Request(String) { + new_request( + token:, + to: "/guilds/" + <> snowflake_to_string(guild_id) + <> "/soundboard-sounds/" + <> snowflake_to_string(sound_id), + method: http.Delete, + ) + |> request_with_reason(reason) +} + +pub fn delete_guild_soundboard_sound_response( + response: Response(String), +) -> Result(Nil, RestError) { + handle_no_content_response(response) +} -- 2.51.2