-module(discord_gleam@http@endpoints). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([me/1, send_message/3, send_direct_message/3, reply/3, kick_member/4, ban_member/4, delete_message/4, wipe_global_commands/2, wipe_guild_commands/3, register_global_command/3, register_guild_command/4, interaction_send_text/3]). -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/endpoints.gleam", 20). ?DOC(" Get the current user\n"). -spec me(binary()) -> {ok, discord_gleam@types@user:user()} | {error, discord_gleam@internal@error:discord_error()}. me(Token) -> Request = discord_gleam@http@request:new_auth( get, <<"/users/@me"/utf8>>, Token ), case gleam@hackney:send(Request) of {ok, Resp} -> case gleam@http@response:get_header(Resp, <<"content-type"/utf8>>) of {ok, <<"application/json"/utf8>>} -> discord_gleam@types@user:from_json_string( erlang:element(4, Resp) ); _ -> {error, {invalid_format, {decode_error, <<"application/json content-type"/utf8>>, <<"unknown"/utf8>>, []}}} end; {error, Err} -> {error, {http_error, Err}} end. -file("src/discord_gleam/http/endpoints.gleam", 45). ?DOC(" Send a message to a channel\n"). -spec send_message(binary(), binary(), discord_gleam@types@message:message()) -> nil. send_message(Token, Channel_id, Message) -> Data = discord_gleam@types@message:to_string(Message), logging:log(debug, <<"Sending message: "/utf8, Data/binary>>), Request = discord_gleam@http@request:new_auth_post( post, <<<<"/channels/"/utf8, Channel_id/binary>>/binary, "/messages"/utf8>>, Token, Data ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 200 -> logging:log(debug, <<"Message sent"/utf8>>), nil; _ -> logging:log( error, <<<<"Failed to send message (status: "/utf8, (erlang:integer_to_binary( erlang:element(2, Resp) ))/binary>>/binary, "):"/utf8>> ), gleam@io:debug(erlang:element(4, Resp)), nil end, nil; {error, Err} -> logging:log(error, <<"Failed to send message: "/utf8>>), gleam_stdlib:println(<<"- Error: "/utf8>>), gleam@io:debug(Err), nil end. -file("src/discord_gleam/http/endpoints.gleam", 94). ?DOC(" Creates a DM channel, then sends a message with `send_message()`.\n"). -spec send_direct_message( binary(), binary(), discord_gleam@types@message:message() ) -> nil. send_direct_message(Token, User_id, Message) -> Data = discord_gleam@types@message:to_string(Message), logging:log(debug, <<"Sending DM: "/utf8, Data/binary>>), Request = discord_gleam@http@request:new_auth_post( post, <<"/users/@me/channels"/utf8>>, Token, <<<<"{ \"recipient_id\": \""/utf8, User_id/binary>>/binary, "\" }"/utf8>> ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 200 -> Channel = discord_gleam@types@channel:string_to_data( erlang:element(4, Resp) ), case erlang:element(2, Channel) of <<"0"/utf8>> -> logging:log( error, <<"Failed to create DM channel, please make a github issue if this is unexpected"/utf8>> ); Value -> send_message(Token, Value, Message) end, nil; _ -> logging:log( error, <<<<"Failed to create DM channel (status: "/utf8, (erlang:integer_to_binary( erlang:element(2, Resp) ))/binary>>/binary, "):"/utf8>> ), gleam@io:debug(erlang:element(4, Resp)), nil end; {error, Err} -> logging:log(error, <<"Failed to create DM channel: "/utf8>>), gleam@io:debug(Err), nil end. -file("src/discord_gleam/http/endpoints.gleam", 158). ?DOC(" Reply to a message\n"). -spec reply(binary(), binary(), discord_gleam@types@reply:reply()) -> nil. reply(Token, Channel_id, Message) -> Data = discord_gleam@types@reply:to_string(Message), logging:log(debug, <<"Replying: "/utf8, Data/binary>>), Request = discord_gleam@http@request:new_auth_post( post, <<<<"/channels/"/utf8, Channel_id/binary>>/binary, "/messages"/utf8>>, Token, Data ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 200 -> logging:log(debug, <<"Reply sent"/utf8>>), nil; _ -> logging:log(error, <<"Failed to send reply"/utf8>>), gleam@io:debug(erlang:element(4, Resp)), nil end, nil; {error, Err} -> logging:log(error, <<"Failed to send reply: "/utf8>>), gleam@io:debug(Err), nil end. -file("src/discord_gleam/http/endpoints.gleam", 198). ?DOC(" Kick a member from a server\n"). -spec kick_member(binary(), binary(), binary(), binary()) -> {binary(), binary()}. kick_member(Token, Guild_id, User_id, Reason) -> Request = discord_gleam@http@request:new_auth_with_header( delete, <<<<<<"/guilds/"/utf8, Guild_id/binary>>/binary, "/members/"/utf8>>/binary, User_id/binary>>, Token, {<<"X-Audit-Log-Reason"/utf8>>, Reason} ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 204 -> logging:log(debug, <<"Kicked member"/utf8>>), {<<"OK"/utf8>>, erlang:element(4, Resp)}; _ -> logging:log(error, <<"Failed to kick member"/utf8>>), {<<"FAILED"/utf8>>, erlang:element(4, Resp)} end; {error, Err} -> logging:log(error, <<"Failed to kick member: "/utf8>>), gleam@io:debug(Err), {<<"FAILED"/utf8>>, <<"ERROR"/utf8>>} end. -file("src/discord_gleam/http/endpoints.gleam", 235). ?DOC(" Ban a member from a server\n"). -spec ban_member(binary(), binary(), binary(), binary()) -> {binary(), binary()}. ban_member(Token, Guild_id, User_id, Reason) -> Request = discord_gleam@http@request:new_auth_with_header( put, <<<<<<"/guilds/"/utf8, Guild_id/binary>>/binary, "/bans/"/utf8>>/binary, User_id/binary>>, Token, {<<"X-Audit-Log-Reason"/utf8>>, Reason} ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 204 -> logging:log(debug, <<"Banned member"/utf8>>), {<<"OK"/utf8>>, erlang:element(4, Resp)}; _ -> logging:log(error, <<"Failed to ban member"/utf8>>), {<<"FAILED"/utf8>>, erlang:element(4, Resp)} end; {error, Err} -> logging:log(error, <<"Failed to ban member: "/utf8>>), gleam@io:debug(Err), {<<"FAILED"/utf8>>, <<"ERROR"/utf8>>} end. -file("src/discord_gleam/http/endpoints.gleam", 272). ?DOC(" Delete a message by channel id and message id\n"). -spec delete_message(binary(), binary(), binary(), binary()) -> {binary(), binary()}. delete_message(Token, Channel_id, Message_id, Reason) -> Request = discord_gleam@http@request:new_auth_with_header( delete, <<<<<<"/channels/"/utf8, Channel_id/binary>>/binary, "/messages/"/utf8>>/binary, Message_id/binary>>, Token, {<<"X-Audit-Log-Reason"/utf8>>, Reason} ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 204 -> logging:log(debug, <<"Deleted Message"/utf8>>), {<<"OK"/utf8>>, erlang:element(4, Resp)}; _ -> logging:log(error, <<"Failed to delete message"/utf8>>), {<<"FAILED"/utf8>>, erlang:element(4, Resp)} end; {error, Err} -> logging:log(error, <<"Failed to delete message"/utf8>>), gleam@io:debug(Err), {<<"FAILED"/utf8>>, <<"ERROR"/utf8>>} end. -file("src/discord_gleam/http/endpoints.gleam", 309). ?DOC(" Wipes the global commands for the bot\n"). -spec wipe_global_commands(binary(), binary()) -> {binary(), binary()}. wipe_global_commands(Token, Client_id) -> Request = discord_gleam@http@request:new_auth_post( put, <<<<"/applications/"/utf8, Client_id/binary>>/binary, "/commands"/utf8>>, Token, <<"{}"/utf8>> ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 200 -> logging:log(debug, <<"Wiped global commands"/utf8>>), {<<"OK"/utf8>>, erlang:element(4, Resp)}; _ -> logging:log( error, <<"Failed to wipe global commands"/utf8>> ), {<<"FAILED"/utf8>>, erlang:element(4, Resp)} end; {error, Err} -> logging:log(error, <<"Failed to wipe global commands"/utf8>>), gleam@io:debug(Err), {<<"FAILED"/utf8>>, <<"ERROR"/utf8>>} end. -file("src/discord_gleam/http/endpoints.gleam", 344). ?DOC(" Wipes the guild commands for the bot\n"). -spec wipe_guild_commands(binary(), binary(), binary()) -> {binary(), binary()}. wipe_guild_commands(Token, Client_id, Guild_id) -> Request = discord_gleam@http@request:new_auth_post( put, <<<<<<<<"/applications/"/utf8, Client_id/binary>>/binary, "/guilds/"/utf8>>/binary, Guild_id/binary>>/binary, "/commands"/utf8>>, Token, <<"{}"/utf8>> ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 200 -> logging:log(debug, <<"Wiped guild commands"/utf8>>), {<<"OK"/utf8>>, erlang:element(4, Resp)}; _ -> logging:log(error, <<"Failed to wipe guild commands"/utf8>>), {<<"FAILED"/utf8>>, erlang:element(4, Resp)} end; {error, Err} -> logging:log(error, <<"Failed to wipe guild commands"/utf8>>), gleam@io:debug(Err), {<<"FAILED"/utf8>>, <<"ERROR"/utf8>>} end. -file("src/discord_gleam/http/endpoints.gleam", 380). ?DOC(" Register a new global slash command\n"). -spec register_global_command( binary(), binary(), discord_gleam@types@slash_command:slash_command() ) -> {binary(), binary()}. register_global_command(Token, Client_id, Command) -> Request = discord_gleam@http@request:new_auth_post( post, <<<<"/applications/"/utf8, Client_id/binary>>/binary, "/commands"/utf8>>, Token, discord_gleam@types@slash_command:command_to_string(Command) ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 201 -> logging:log( debug, <<"Added global command "/utf8, (erlang:element(2, Command))/binary>> ), {<<"OK"/utf8>>, erlang:element(4, Resp)}; 200 -> logging:log( debug, <<"Updated global command "/utf8, (erlang:element(2, Command))/binary>> ), {<<"OK"/utf8>>, erlang:element(4, Resp)}; _ -> logging:log( error, <<"Failed to add global command"/utf8, (erlang:element(2, Command))/binary>> ), {<<"FAILED"/utf8>>, erlang:element(4, Resp)} end; {error, Err} -> logging:log( error, <<"Failed to add global command"/utf8, (erlang:element(2, Command))/binary>> ), gleam@io:debug(Err), {<<"FAILED"/utf8>>, <<"ERROR"/utf8>>} end. -file("src/discord_gleam/http/endpoints.gleam", 423). ?DOC(" Register a new guild-specific slash command\n"). -spec register_guild_command( binary(), binary(), binary(), discord_gleam@types@slash_command:slash_command() ) -> {binary(), binary()}. register_guild_command(Token, Client_id, Guild_id, Command) -> Request = discord_gleam@http@request:new_auth_post( post, <<<<<<<<"/applications/"/utf8, Client_id/binary>>/binary, "/guilds/"/utf8>>/binary, Guild_id/binary>>/binary, "/commands"/utf8>>, Token, discord_gleam@types@slash_command:command_to_string(Command) ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 201 -> logging:log( debug, <<"Added guild command "/utf8, (erlang:element(2, Command))/binary>> ), {<<"OK"/utf8>>, erlang:element(4, Resp)}; 200 -> logging:log( debug, <<"Updated guild command "/utf8, (erlang:element(2, Command))/binary>> ), {<<"OK"/utf8>>, erlang:element(4, Resp)}; _ -> logging:log( error, <<"Failed to add guild command"/utf8, (erlang:element(2, Command))/binary>> ), {<<"FAILED"/utf8>>, erlang:element(4, Resp)} end; {error, Err} -> logging:log( error, <<"Failed to add guild command"/utf8, (erlang:element(2, Command))/binary>> ), gleam@io:debug(Err), {<<"FAILED"/utf8>>, <<"ERROR"/utf8>>} end. -file("src/discord_gleam/http/endpoints.gleam", 467). ?DOC(" Send a basic text reply to an interaction\n"). -spec interaction_send_text( discord_gleam@ws@packets@interaction_create:interaction_create(), binary(), boolean() ) -> {binary(), binary()}. interaction_send_text(Interaction, Message, Ephemeral) -> Request = discord_gleam@http@request:new_post( post, <<<<<<<<"/interactions/"/utf8, (erlang:element(4, erlang:element(5, Interaction)))/binary>>/binary, "/"/utf8>>/binary, (erlang:element(2, erlang:element(5, Interaction)))/binary>>/binary, "/callback"/utf8>>, discord_gleam@types@slash_command:make_basic_text_reply( Message, Ephemeral ) ), case gleam@hackney:send(Request) of {ok, Resp} -> case erlang:element(2, Resp) of 204 -> logging:log(debug, <<"Sent Interaction Response"/utf8>>), {<<"OK"/utf8>>, erlang:element(4, Resp)}; _ -> logging:log( error, <<"Failed to send Interaction Response"/utf8>> ), {<<"FAILED"/utf8>>, erlang:element(4, Resp)} end; {error, Err} -> logging:log( error, <<"Error when sending Interaction Response"/utf8>> ), gleam@io:debug(Err), {<<"FAILED"/utf8>>, <<"ERROR"/utf8>>} end.