From 2dadf7881b69daa276773cd6bcbee19f6cbbc719 Mon Sep 17 00:00:00 2001 From: fhoffmann Date: Wed, 16 Sep 2026 22:30:26 +0200 Subject: [PATCH] responding to autocomplete request interactions --- src/grom.gleam | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/grom.gleam b/src/grom.gleam index b447664..af44f2a 100644 --- a/src/grom.gleam +++ b/src/grom.gleam @@ -26380,3 +26380,87 @@ pub fn respond_to_message_component_interaction_response( ) -> Result(Nil, RestError) { handle_no_content_response_bits(response) } + +pub opaque type AutocompleteRequestInteractionResponse { + AutocompleteRequestInteractionResponseString( + choices: List(StringSlashCommandOptionChoice), + ) + AutocompleteRequestInteractionResponseInt( + choices: List(IntSlashCommandOptionChoice), + ) + AutocompleteRequestInteractionResponseFloat( + choices: List(FloatSlashCommandOptionChoice), + ) +} + +/// You can provide up to 25 choices. +pub fn respond_to_string_autocomplete_request_interaction( + choices: List(StringSlashCommandOptionChoice), +) -> AutocompleteRequestInteractionResponse { + AutocompleteRequestInteractionResponseString(choices) +} + +/// You can provide up to 25 choices. +pub fn respond_to_int_autocomplete_request_interaction( + choices: List(IntSlashCommandOptionChoice), +) -> AutocompleteRequestInteractionResponse { + AutocompleteRequestInteractionResponseInt(choices) +} + +/// You can provide up to 25 choices. +pub fn respond_to_float_autocomplete_request_interaction( + choices: List(FloatSlashCommandOptionChoice), +) -> AutocompleteRequestInteractionResponse { + AutocompleteRequestInteractionResponseFloat(choices) +} + +pub fn autocomplete_request_interaction_response_to_json( + response: AutocompleteRequestInteractionResponse, +) -> Json { + [ + #("type", json.int(8)), + #( + "data", + json.object([ + #("choices", case response { + AutocompleteRequestInteractionResponseString(choices:) -> + json.array(choices, string_slash_command_option_choice_to_json) + AutocompleteRequestInteractionResponseInt(choices:) -> + json.array(choices, int_slash_command_option_choice_to_json) + AutocompleteRequestInteractionResponseFloat(choices:) -> + json.array(choices, float_slash_command_option_choice_to_json) + }), + ]), + ), + ] + |> json.object +} + +pub fn respond_to_autocomplete_request_interaction_request( + token token: Token, + id interaction_id: Snowflake(Interaction), + interaction_token interaction_token: AutocompleteRequestInteractionToken, + with response: AutocompleteRequestInteractionResponse, +) -> Request(String) { + let body = + response + |> autocomplete_request_interaction_response_to_json + |> json.to_string + + new_request( + token:, + to: "/interactions/" + <> snowflake_to_string(interaction_id) + <> "/" + <> interaction_token.token + <> "/callback", + method: http.Post, + ) + |> request.set_body(body) +} + +pub fn respond_to_autocomplete_request_interaction_response( + response: Response(String), +) -> Result(Nil, RestError) { + handle_no_content_response(response) +} -- 2.54.0