Current section
Files
Jump to
Current section
Files
src/discord_gleam@http@interactions.erl
-module(discord_gleam@http@interactions).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/discord_gleam/http/interactions.gleam").
-export([send_response/2, edit_original/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/discord_gleam/http/interactions.gleam", 10).
?DOC(" Send a basic text reply to an interaction\n").
-spec send_response(
discord_gleam@ws@packets@interaction_create:interaction_create_packet_data(),
binary()
) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}.
send_response(Interaction, Data) ->
Request = discord_gleam@http@request:new_with_body(
post,
<<<<<<<<"/interactions/"/utf8,
(discord_gleam@discord@snowflake:to_string(
erlang:element(2, Interaction)
))/binary>>/binary,
"/"/utf8>>/binary,
(erlang:element(11, Interaction))/binary>>/binary,
"/callback"/utf8>>,
Data
),
case gleam@httpc:send(Request) of
{ok, Resp} ->
case erlang:element(2, Resp) of
204 ->
logging:log(debug, <<"Sent interaction response"/utf8>>),
{ok, nil};
429 ->
logging:log(
error,
<<"Failed to send interaction response: rate limited"/utf8>>
),
{error,
discord_gleam@http@request:extract_ratelimit_error(Resp)};
_ ->
logging:log(
error,
<<"Failed to send Interaction Response"/utf8>>
),
{error,
{api_error,
erlang:element(2, Resp),
erlang:element(4, Resp)}}
end;
{error, Err} ->
logging:log(
error,
<<"Error when sending Interaction Response"/utf8>>
),
{error, {http_error, Err}}
end.
-file("src/discord_gleam/http/interactions.gleam", 60).
?DOC(
" Edit the initial response to an interaction. \\\n"
" For example used after deffering the response, to then send a reply\n"
).
-spec edit_original(
discord_gleam@ws@packets@interaction_create:interaction_create_packet_data(),
binary()
) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}.
edit_original(Interaction, Data) ->
Request = discord_gleam@http@request:new_with_body(
patch,
<<<<<<<<"/webhooks/"/utf8,
(discord_gleam@discord@snowflake:to_string(
erlang:element(3, Interaction)
))/binary>>/binary,
"/"/utf8>>/binary,
(erlang:element(11, Interaction))/binary>>/binary,
"/messages/@original"/utf8>>,
Data
),
case gleam@httpc:send(Request) of
{ok, Resp} ->
case erlang:element(2, Resp) of
200 ->
logging:log(debug, <<"Edited interaction response"/utf8>>),
{ok, nil};
429 ->
logging:log(
error,
<<"Failed to edit interaction response: rate limited"/utf8>>
),
{error,
discord_gleam@http@request:extract_ratelimit_error(Resp)};
_ ->
logging:log(
error,
<<"Failed to edit Interaction Response"/utf8>>
),
{error,
{api_error,
erlang:element(2, Resp),
erlang:element(4, Resp)}}
end;
{error, Err} ->
logging:log(
error,
<<"Error when editing Interaction Response"/utf8>>
),
{error, {http_error, Err}}
end.