Current section
Files
Jump to
Current section
Files
src/discord_gleam@http@applications.erl
-module(discord_gleam@http@applications).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/discord_gleam/http/applications.gleam").
-export([wipe_global_commands/2, wipe_guild_commands/3, register_global_command/3, register_guild_command/4]).
-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/applications.gleam", 10).
?DOC(" Wipes the global commands for the bot\n").
-spec wipe_global_commands(
binary(),
discord_gleam@discord@snowflake:snowflake(discord_gleam@discord@snowflake:application())
) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}.
wipe_global_commands(Token, Client_id) ->
Request = discord_gleam@http@request:new_auth_with_body(
put,
<<<<"/applications/"/utf8,
(discord_gleam@discord@snowflake:to_string(Client_id))/binary>>/binary,
"/commands"/utf8>>,
Token,
<<"[]"/utf8>>
),
case gleam@httpc:send(Request) of
{ok, Resp} ->
case erlang:element(2, Resp) of
200 ->
logging:log(debug, <<"Wiped global commands"/utf8>>),
{ok, nil};
429 ->
logging:log(
error,
<<"Failed to wipe global commands: rate limited"/utf8>>
),
{error,
discord_gleam@http@request:extract_ratelimit_error(Resp)};
_ ->
logging:log(
error,
<<"Failed to wipe global commands"/utf8>>
),
{error,
{api_error,
erlang:element(2, Resp),
erlang:element(4, Resp)}}
end;
{error, Err} ->
logging:log(error, <<"Failed to wipe global commands"/utf8>>),
{error, {http_error, Err}}
end.
-file("src/discord_gleam/http/applications.gleam", 57).
?DOC(" Wipes the guild commands for the bot\n").
-spec wipe_guild_commands(
binary(),
discord_gleam@discord@snowflake:snowflake(discord_gleam@discord@snowflake:application()),
discord_gleam@discord@snowflake:snowflake(discord_gleam@discord@snowflake:guild())
) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}.
wipe_guild_commands(Token, Client_id, Guild_id) ->
Request = discord_gleam@http@request:new_auth_with_body(
put,
<<<<<<<<"/applications/"/utf8,
(discord_gleam@discord@snowflake:to_string(Client_id))/binary>>/binary,
"/guilds/"/utf8>>/binary,
(discord_gleam@discord@snowflake:to_string(Guild_id))/binary>>/binary,
"/commands"/utf8>>,
Token,
<<"[]"/utf8>>
),
case gleam@httpc:send(Request) of
{ok, Resp} ->
case erlang:element(2, Resp) of
200 ->
logging:log(debug, <<"Wiped guild commands"/utf8>>),
{ok, nil};
429 ->
logging:log(
error,
<<"Failed to wipe guild commands: rate limited"/utf8>>
),
{error,
discord_gleam@http@request:extract_ratelimit_error(Resp)};
_ ->
logging:log(error, <<"Failed to wipe guild commands"/utf8>>),
{error,
{api_error,
erlang:element(2, Resp),
erlang:element(4, Resp)}}
end;
{error, Err} ->
logging:log(error, <<"Failed to wipe guild commands"/utf8>>),
{error, {http_error, Err}}
end.
-file("src/discord_gleam/http/applications.gleam", 108).
?DOC(" Register a new global slash command\n").
-spec register_global_command(
binary(),
discord_gleam@discord@snowflake:snowflake(discord_gleam@discord@snowflake:application()),
discord_gleam@types@slash_command:slash_command()
) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}.
register_global_command(Token, Client_id, Command) ->
Request = discord_gleam@http@request:new_auth_with_body(
post,
<<<<"/applications/"/utf8,
(discord_gleam@discord@snowflake:to_string(Client_id))/binary>>/binary,
"/commands"/utf8>>,
Token,
discord_gleam@types@slash_command:command_to_string(Command)
),
case gleam@httpc: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, nil};
200 ->
logging:log(
debug,
<<"Updated global command "/utf8,
(erlang:element(2, Command))/binary>>
),
{ok, nil};
429 ->
logging:log(
error,
<<<<"Failed to add global command "/utf8,
(erlang:element(2, Command))/binary>>/binary,
": rate limited"/utf8>>
),
{error,
discord_gleam@http@request:extract_ratelimit_error(Resp)};
_ ->
logging:log(
error,
<<"Failed to add global command "/utf8,
(erlang:element(2, Command))/binary>>
),
{error,
{api_error,
erlang:element(2, Resp),
erlang:element(4, Resp)}}
end;
{error, Err} ->
logging:log(
error,
<<"Failed to add global command "/utf8,
(erlang:element(2, Command))/binary>>
),
{error, {http_error, Err}}
end.
-file("src/discord_gleam/http/applications.gleam", 168).
?DOC(" Register a new guild-specific slash command\n").
-spec register_guild_command(
binary(),
discord_gleam@discord@snowflake:snowflake(discord_gleam@discord@snowflake:application()),
discord_gleam@discord@snowflake:snowflake(discord_gleam@discord@snowflake:guild()),
discord_gleam@types@slash_command:slash_command()
) -> {ok, nil} | {error, discord_gleam@internal@error:discord_error()}.
register_guild_command(Token, Client_id, Guild_id, Command) ->
Request = discord_gleam@http@request:new_auth_with_body(
post,
<<<<<<<<"/applications/"/utf8,
(discord_gleam@discord@snowflake:to_string(Client_id))/binary>>/binary,
"/guilds/"/utf8>>/binary,
(discord_gleam@discord@snowflake:to_string(Guild_id))/binary>>/binary,
"/commands"/utf8>>,
Token,
discord_gleam@types@slash_command:command_to_string(Command)
),
case gleam@httpc: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, nil};
200 ->
logging:log(
debug,
<<"Updated guild command "/utf8,
(erlang:element(2, Command))/binary>>
),
{ok, nil};
429 ->
logging:log(
error,
<<<<"Failed to add guild command "/utf8,
(erlang:element(2, Command))/binary>>/binary,
": rate limited"/utf8>>
),
{error,
discord_gleam@http@request:extract_ratelimit_error(Resp)};
_ ->
logging:log(
error,
<<"Failed to add guild command "/utf8,
(erlang:element(2, Command))/binary>>
),
{error,
{api_error,
erlang:element(2, Resp),
erlang:element(4, Resp)}}
end;
{error, Err} ->
logging:log(
error,
<<"Failed to add guild command "/utf8,
(erlang:element(2, Command))/binary>>
),
{error, {http_error, Err}}
end.